How to List Only Files Not Directories in Linux?

In Linux, we use the “ls” command for listing the content of the directories or folders but if the user wants to display only files this command doesn’t work. However, it can be filtered out for specific files such as “ls .pdf,” but what if the user wants to display all types of files? In this situation, Linux provides various methods for displaying only files in the directory or folder.

This article will demonstrate the methods to list only files in Linux. The content for the post is:

Method 1: List Only Files Using ls Utility

The combination of the “grep” and the “ls” utility through pipe(|) command can be used to display the only files for the specified directory. The output of the “ls” command will be sent to the “grep” command as an input then the “v” flag will be used for non-matching. Here the “d” option is used for the directory. So the grep command will not display the directories, and as a result, you will get the output for only files:

$ ls -l | grep -v '^d'

All files in the home directory will be displayed.

Users can save this command as an alias with a variable name in the .bashrc file, and that variable can be used as a command for displaying files only. Open .bashrc in the nano editor and paste the given line:

$ nano .bashrc #alias alias lf="ls -l | egrep -v '^d'"

Save the file by pressing “Ctrl+O” and exit from the editor by pressing “Ctrl+X”.

Run the source command for executing the new changes in the .bashrc file:

$ source ~/.bashrc

The “.bashrc” file will be executed.

Now run the “lf” as a command for listing the files:

$ lf

All the files will be listed in the directory.

Let’s move toward method 2.

Method 2: List Only Files Using find Utility

The second method for listing the files only uses the find utility. The “find” is a utility for searching files or directories in Linux. We can use this utility for finding and listing the files. Run the given command in the terminal with the “maxdepth 1” parameter, which means searching the files into one subdirectory. Then the “ls” command will list that files:

$ find . -maxdepth 1 -type f -ls

All files will be listed, which can be seen in the above image.

Note: To know in-depth knowledge of the find command, check out our latest article on the find command.

Method 3: List Only Files Using run-parts regex Utility

The last method to list the only files in Linux is using the “run-parts” utility. This utility is used for displaying all executable files available in the directory. Use the “run-parts” command with the “-list” flag that will display the name of all the valid files, and the “regex” option is used for specifying the pattern:

$ run-parts --list --regex . .

All the files will be listed in the directory.

Conclusion

There are three possible methods to list only the files in the directory. The first method combines the “ls” and “grep” utilities. The second method is to use the “find” utility, while the third method is to use the “run-parts” utility with the “list” flag. This write-up has illustrated the three most efficient methods for displaying only files in Linux.