Linux Commands

How Do You List Only Files Not Directories in Linux?

You must have prior knowledge on how to list all the directories and folders that are residing in Linux systems. You may have used the “ls’ command to do this while working on any distribution of the Linux operating system. In this tutorial, we will be covering the topic of the process of listing only the files from a specific directory or folder in a Linux system. If you want to learn about listing files, go through each step of this guide.

Example of “ls” Command:

First of all, for those who don’t have prior knowledge about the “ls” command in listing the files and folders, here is a simple illustration of it. Open the command-line shell and write the ‘ls” command to list only directories. The output will show only the directories but not the files.

$ ls

To show the list of all files and folders in a Linux system, try the “ls” command along with the flag ‘-a” as shown below. It will show all the hidden and primary files and folders.

$ ls -a

List Files Using “Grep” Command:

First, we will use the grep command within the “ls” list command to list all the files residing in the particular folder. Try the below “ls” command along with the “-la” flag to list all the regular files, e.g., hidden or not. The expression ‘^-‘ had been used to search for regular files. You will see a list of regular files created in the home directory of the user “aqsayasin”.

$ ls –al | grep ‘^-‘

Instead of listing only the regular files, you can also list other types of files using the “grep’ and “ls” command with a little modification. Change the symbolic characters to list all other types of files as shown below. As we don’t have other types of files yet so it only shows the regular files.

$ ls –al | grep ‘^[-l]

You can also use the below “ls” command to list the files in the current location.

$ ls –lAh | grep –v ‘^d’

List Files Using “Run-parts Regex” Command:

The run-parts command is mostly used for Debian and Ubuntu systems. This command prints all the matching file names if provided. So first of all we will be using this command without giving an absolute path same as below. And the output shows the names of files residing in the home directory of the Linux system.

$ run-parts –list –regex . .

Again, using the above command with a little alteration, it will show the absolute path along with the names of files using the absolute path in commandas shown below. The output is showing the names of files and their location.

$ run-part –list –regex . “$PWD

List Files Using “find” Command:

Another new method to list the files in a terminal is by using the “find” command. Our first method will be using the “find” keyword along with the “maxdepth flag keyword within the command. The keyword “-maxdepth” along with the number “1” means we will be looking for the files only in the current directory. The dot can be replaced by a path to examine files within that directory. The keyword “f” defines the file type. The output shows the names of files.

$ find . –maxdepth 1type f

Let’s replace the dot with a directory path to look for some files in the folder “Documents”. Now, we will use the same command with a little alteration. We will be using the type “d” flag in it to fetch all types of files except directories. The exclamation mark is used for the “not” directories type. Execute the below command in the terminal shell to list files along with their paths.

$ find /home/aqsayasin/Documents/ -maxdepth 1 !type d

The above same output can be generated using the same command with a little alteration at the “-type” flag location. We will be using the “f” keyword here which identifies the file format in the given location to be shown in the list.

$ find /home/aqsayasin/Documents/ -maxdepth 1type f

Another way of listing files is by using the “-ls” flag along with the same old command. This time “-ls” command had been used to list all the privileges and additional information regarding the files along with their names. By executing the below command you will get the list of files along with the information about their owners, groups, the date on which they are created, and many more.

$ find. –maxdepth 1 !type d -ls

The “find” command has found many variations in it. We can use “find” in many ways to list the files only. This time we will be using it to list the files according to the sorting order of the date on which these are created. For this purpose, we will be using “-hltrF” along with the “-exec” flag within this command as shown below:

$ find /home/aqsayasin/Documents/ -maxdepth 1 !type d –exec ls –hltrF {} +

You can see a list of files residing in the directory “Documents” according to the sorting order of their creation date.

We will now sort and list these files according to their size. For this purpose, we will be using the “-lSF” in the same command to sort all files according to their sizes.

$ find /home/aqsayasin/Documents/ -maxdepth 1type f –exec ls –Lsf – color {} +

If you don’t want to list the hidden files, you can do so by the below command.

$ find /home/aqsayasin/Documents/ -maxdepth 1 !type d ! name ‘.*-exec ls –hltrF {} +

Conclusion:

We have done almost all the methods to list only the files in the command shell. For better results, please keep practicing.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.