Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 21, 2024
File management is one of the core aspects of Linux systems, and it involves creating, organizing, storing, and maintaining files and directories. Consequently, it can be crucial to distinguish regular files from other file types (e.g., symbolic links, block special file, character special file) and directories to perform backups, bulk file processing, or file integrity checks (using md5sum or sha256sum).
In this tutorial, we’ll learn how to display only regular files excluding directories from the current directory in Linux.
The code in this tutorial underwent testing on a Debian 12 system using GNU Bash 5.1.16.
We can use the find command to filter out directories and display only regular files:
$ find . -maxdepth 1 -type f
In this example, find is the command used to search for regular files (-type f) only in the current directory (.) without including any subdirectories (-maxdepth 1). Thus, it should output the regular files with their relative path, such as ./regularfile.txt.
The find command recursively searches for files and directories based on specified attributes or criteria, and using the -maxdepth 1 option restricts the search to the current directory only.
Alternatively, we can replace the relative path (i.e., .) with either the absolute path or $(pwd) to display regular files in the current directory with their absolute path:
$ find $(pwd) -maxdepth 1 -type f
This command displays the same files but with their absolute path, i.e., /user/baeldung/regularfile.txt.
Another approach to displaying only regular files is by using Bash for loops. We can loop through the contents of a directory and check if each item is a regular file.
Let’s try the for loop in the current working directory:
$ for file in *; do [ -f "$file" ] && echo "$file"; done
This command employs a for loop that goes through each item in the current directory (for file in *), checking whether it is a regular file ([-f “$file”]). If the item is a regular file, its name is displayed (echo “$file”).
The -f test ensures that only regular file names are displayed, while directories and special file types are ignored.
One of the most common ways to list files and directories is the ls command-line tool. It provides multiple options to display the detailed information about the files and directories.
Let’s see it in action and pipe its output to grep for filtering:
$ ls -l . | grep "^-"
In this example, the ls command lists the contents from the current directory (.) in the long list format (-l), while the grep command filters the regular files based on the file type, i.e., the first character in the long-list format.
We can further filter these results with awk to display only the file names:
$ ls -l . | grep "^-" | awk '{print $9}'
This command displays only the regular file names from the current directory.
We can also use the stat command to display the metadata of a file or file system including the file types, size, and permissions.
First, let’s use the stat command to view the sample output:
$ stat --printf="%F %n\n" ./*
This stat command displays each item’s file type and name from the current directory (./*).
The –printf option is used to customize the output of the stat command:
Next, we can filter only regular files using awk via the regular keyword:
$ stat --printf="%F %n\n" ./* | awk '$1=="regular" {print $NF}'
This command displays only the file names of regular files, both empty and non-empty. The pipe (|) takes the output of the preceding command, i.e., stat, and passes it as input to the following command, in this case, awk. The awk part of this command checks each line, and if the first word in the file type is regular ($1==”regular”), it prints the last field ({print $NF}) of the line, i.e., filename.
In this article, we learned different ways to display the regular files excluding directories in the current working directory in Linux.
First, we used the find command to display only regular files in the current directory. We then briefly discussed the use of relative and absolute paths to display the regular files from the current directory. Then, we used the Bash -f test in a for loop to check for and display only regular files.
Next, we employed the combination of ls and grep to list regular files in long-list format and filter out any directories or special file types, ensuring only regular files are displayed. Finally, we used the stat command in combination with awk to filter and display only regular files based on the file types provided by the stat command.
Although we can select any method depending on our current preferences and needs, find is often the simplest and most standard way to display only regular files in the current directory in Linux.