CentOS

How do I Find a File List in Linux/Ubuntu?

Running specific commands in the Terminal of Ubuntu sometimes involve the name of a file. Thus, knowing the name of the file is essential for the user to use it in commands. Ubuntu Operating System makes it easy to list the files in the terminal with various commands. In Terminal, listing files can be more flexible as you can list specific files, specific types of files, or files in a specific directory.

In this article, we will discuss the different methodologies, each having different approaches to listing files in Ubuntu.

How do I Find a File List in Linux/Ubuntu?

File List means listing all the files in a specific directory or listing files of a specific type. Files can be listed in Ubuntu using any of the commands among “ls”, “find”, and “locate”. All three methods for listing files in Ubuntu are discussed in detail below.

Method 1: Find a File List in Ubuntu using the “ls” Command

The “ls” is the most common command used to display files and directories in a specific directory. The “ls” command can be used in numerous methodologies discussed in the following sections:

  • Listing Files in the Current Directory.
  • Listing Files in Another Directory.
  • Listing Files in the Users’ Home Directory.
  • Listing Only Directories.
  • Listing Files and Directories including Subdirectories.
  • Listing Files and Only the Directories having Sub-Directories.
  • Listing Files with Size.
  • Listing Files with File Details.
  • Listing Files with Sorted Date and Time.
  • Listing Files with Sorted File Size.

List Files in the Current Directory Using the “ls” Command

The basic syntax to list all the files in the current working directory is:

ls

In our case, our current working Directory is “Documents”. To list all the files inside the “Documents” Directory, the command used is

ls

All the files and folders inside the Documents Directory will be listed in the Terminal:

List Files in Another Directory using the “ls” Command

The “ls” command can also be used to list the files inside other directories. To list files in another directory the “ls” command is followed by the path to the directory:

ls <directory-path>

In our case, using the previous example we have a “MultDir” Directory inside the Documents Directory. To list the files inside the “MultDir” Directory, the command used is

ls MultDir

All the files and folders inside the MultDir will be displayed in the Terminal:

List Files in the User’s Home Directory using the “ls” Command

The “ls” command can also list all the files inside the Home Directory of the current User. It can be used anywhere followed by the “~” symbol:

ls ~

In our case, the user is “taha” and to list all the files inside the Home Directory of “taha”, the command will be:

ls ~

The “ls” command, in this case, can be used in any directory. In our case, we used the “ls ~” command from the Documents Directory and it still returned the list of files inside the Home Directory:

List only Directories using the “ls” Command

Instead of Listing all the files and Directories, the “ls” command can be used to list only the directories inside the current working directory using the “-d” flag with ls:

ls -d */

In our case, only the Directories inside the Documents Directory will be listed:

List All Files and Directories With Subdirectories Using the “ls” Command

The “ls” command is used with an asterisk to list all the files and the directories with subdirectories as well:

ls *

All the files and directories having sub-directories will be displayed. In our case, it will display all the files and directories with subdirectories inside the Documents Directory:

List All Files, Directories, and Sub-Directories using the “ls” Command

The “-R” flag can also be used to find the files recursively, i.e. list all files, folders, and sub-directories:

ls -R

All the files, folders, and sub-directories will be listed:

List Files with Sizes Using the “ls” Command

To list the files with their respective sizes, the command used is:

ls -s

The “-s” will ensure to list all the files and directories along with their sizes:

List Files With File Details Using the “ls” Command

The “ls” command can also be used to list the files and directories along with details:

ls -l

The “-l” flag will display the details of files including the file permissions, owner, group, size, and previous modification date of the file. In our case, it will display the files along with details inside the Documents Directory:

List Files with Sorted Data and Time using the “ls” Command

The files listed can also be sorted. To sort the files using the “ls” command with date and time, the command used is:

ls -t

In our case, to list the files with details and in sorted Date and Time, the command will be:

ls -l -t

The listed files will be sorted according to the last modified date in descending order:

The sorted list of files can also be displayed in ascending order using the “r” flag:

ls -l -rt

List Files With Sorted File Size Using the “ls” Command

The files listed can also be sorted with respect to Size using the command:

ls -S

In our case, to list the files with details and in sorted Size, the command will be:

ls -l -S

The listed files will be sorted according to the size in descending order:

To list in ascending order, the “r” flag can be used to reverse it:

ls -l -rS

The listed files will now be in ascending order(small size to large size)

Method 2: Find a File List in Ubuntu Using the “find” Command

The “find” command is useful in searching for files based on a given condition. To search for files based on a given condition, the “find” command along with the “path” to a directory is used:

find <path>

Similar to the “ls” command, the “find” command can also be used in a variety of ways discussed in the sections below:

  • List Files in a Specific Directory.
  • List Files and Directories having Sub-Directories.
  • List Files with a Query.
  • List Files and Directories that are Empty.
  • List Files based on Date and Time.
  • List Files based on Size.
  • List Files based on Permissions.

List Files in a Specific Directory Using the “find” Command

To list all the files in a specific directory, the “find” command will be used:

find <path-to-directory>

In our case, to list all the files in the “MultDir” Directory, the “find” command will be used as follows:

find Documents/MultDir/

The MultDir exists in the Documents Directory and the files listed with the “find” command are:

List Files and Directories With Subdirectories Using the “find” Command

The “find” command to list all the files recursively is:

find .

The “.” will list all the files and files in subdirectories. In our case, working in the current Documents Directory, the Documents Directory files and Sub-Directories will be listed:

List Files With a Query Using the “find” Command

The “find” command can also list files according to specific keywords. In our case, to list all the files having the “file” name, the “find” command will be:

find -iname "file*"

The files having the string “file” in their name will be displayed:

List Files and Directories that are Empty using the “find” Command

To display all the files that are empty, the “find” command will be used as follows:

find -empty

In our case, it will display all the empty files inside the Documents Directory and Sub-Directories:

List Files based on Date and Time using the “find” Command

Files can also be filtered according to the Date and Time in which those files were last accessed or modified. Three different options can be used with the “find” command:

  • Find File based on Last Modification (-mtime <date>)
  • Find File based on Last Access (-atime <date>)
  • Find File based on Last Attributes Change (-ctime <date>)

In our case, to list the files that were modified in the last 1 day, the “find” command will be:

find . -mtime -1

Only 1 file was modified in the last 1 day:

To list files accessed in the last 2 days, the “find” command will be:

find . -atime -2

The files accessed in the last 2 days will be displayed:

To list the files that were changed in the last 5 days, the “find” command will be:

find . -ctime -5

This will display the files that were changed in the last 5 days:

List Files Based on Size Using the “find” Command

Files can be listed in the terminal based on the size of the file using the find command:

find <path> -size <size-value>

In our case, to list the files greater than 1MB in the Downloads Directory, the command used will be:

find . -size +1M

Only one file is greater than 1MB in our directory and it will be displayed:

List Files Based on Permissions Using the “find” Command

Files can also be listed using the “perm” command with the “type” command for file or folder. The “perm” command will take the numeric number for permission and the “type” command will either take the “f” value for a file or “d” for a directory.

In our case, to list the files in the Documents Directory having the read, write, and execute permission for every user, the command used will be:

find . -type f -perm 777

The “777” indicates the files have the “rwe” permissions meaning any user can read the file, edit the file, and can also access or execute the file. The “f” value indicates only files will be listed:

Method 3: Find a File List in Ubuntu Using the “locate” Command

Locate is another useful command that can be used to search for a specific file, multiple files, or files of any specific type. By default, the “locate” command is not installed in Ubuntu. To install “locate”, the command used is:

sudo apt-get install locate

This will install the necessary locate packages:

The locate command can be used in multiple scenarios:

List Specific Files using the “locate” Command

To list files according to a specific file name, the “locate” command used is:

locate -i <fileName>

In our case, to list the files with the name “firefox”, the below command will be used:

locate -i firefox

It will return all the files with the name “firefox”:

List Specific Files With a Query Using the “locate” Command

Specific files having the same extension can also be listed using the “locate” Command. In our case, to locate the files with the “.iso” extension, the “locate” command will be used:

locate -i *.iso

The files having the “.iso” extensions will be displayed:

Conclusion

Listing Files can be executed or carried out easily using the Command Line in Ubuntu. The “ls”, “find”, and “locate” are useful commands in Ubuntu that offer multiple approaches to listing files and directories. With these commands, listing files in a specific directory, listing only directories, listing sub-directories, or listing specific types of files can be accomplished with no sweat. This article explains all the different methodologies and briefly discusses the different approaches to listing files in Ubuntu.

Similar Posts