| by Arround The Web | No comments

How to Only List the Files and Not the Directories in Linux

In Linux, we use the “ls” command to navigate through directories for files. Listing all the files and folders in Linux is a common command. But occasionally, we must only list the files and not the folders. In that case, this command does not work. To only list the files using the “ls” command, we need to write “ls *.txt”. But this command works if we need to only list the text files since this command only lists the text files. If we need to list all the files and not only the text files, this command becomes useless. For that purpose, Linux provides us with many options. To list all the files in a specific directory only, we use the following methods:

Using the Ls Command to List the Directories

The ls command is the one that can be used to list the files from directories. The “ls” command works the same as how we manually go through the directory and then get all of the files that we want but it makes it easy for us to enlist them by just running a simple command. It also enables us to pass the options along with it to instruct the compiler to list the files according to your desire.

e.g: "ls -p

Like the previously-mentioned ls command, we pass the –p option. If we want to see all the hidden files, we add the “-a” option to the ls command. If we know the file name or a part of it, we can also use the “run-parts regex” command. This command lists all the files whose names match the provided name. But to make this command work, we must know at least a part of the filename.

Listing the Files by Name

When it comes to listing the files by names, using the ls command is the easiest way to list them. To list the name of files, we simply have to write the following command:

linux@linux-VirtualBox:~ ls | head4

In the provided syntax, we use the ls command along with the head and the number “4”. This means that it only displays the first four encountered files where the head indicates that each file name is printed in a new line instead of printing all in the same line to make it easy to read. When we run the previous command, the output is shown like in the following snippet in which the names of the files are displayed in our terminal.

Now, let’s suppose that we want to display all the files in the terminal. We simply run the following command:

linux@linux-VirtualBox:~ ls | head

As we can see in the screenshot, all of the files that are currently stored in the home directory are displayed by running the previous command.

Same as in the previous example, we can print the complete details of the files along with its name by passing another flag which is “-l” along with the ls command using the following command:

linux@linux-VirtualBox:~ ls –l | head

As seen in the following figure, the details of the files are also printed in the terminal:

Listing the Files in Reverse Order

Using the ls command, we can also print the file names in reverse order. This means that we want to list the files in an upside-down order, or that our compiler starts listing the files from the last one until the first. For that, we run the following command:

linux@linux-VirtualBox:~ ls –r

After running this command, we get the following output in which the files are printed in reverse order:

Listing the Directories

The ls command enables us to list the files as well as the directories. When we only want to list the name of the directories within a specific location, we simply use the “-d” option with the ls files. The “d” flag denotes the directories.

linux@linux-VirtualBox:~ ls d */ | head

After running the previous command, all of the directories that are created in the home directory are displayed in the terminal as shown in the following figure:

Listing the Files Using the Run-Parts Regex Command

The run-parts regex is the Linux command that is used to list all the files that are stored in our system. It is responsible for displaying all the available files. A regex is a tool or pattern that can be used to match the strings that follow any pattern. When it is about searching for the files, the “run apart regex” command can be used. It is effective for enlisting the files from the directories. For that, we must run the following command:

linux@linux-VirtualBox:~ run-parts - -list - -regex . .

When we run the provided command, we get all the files enlisted that are stored in the home directory of our system. When we want to fetch from any specified directory, we just have to pass the path of the directory from which the files are to be fetched.

Listing the Files Using the Find Command

To list all the files only in a specific directory, we use the find command. Same as the “ls” command, there are some options that can be passed with the find command to instruct our compiler to get the desired output.

"find . -maxdepth 1 -type f"

By using maxdepth -1, we make sure that we are looking only at the current directory. If we want to look into all files in that directory and all the other subdirectories, we remove this option where “type f” denotes the type of files that we are searching.

linux@linux-VirtualBox:~ find . –maxdepth 1type f

By running this command, we fetch the files that are currently stored in our home directory. Let’s suppose we want to fetch for the other directory. We simply replace the dot “.” with the directory’s path which should be used to enlist the files. In the following snippet, the output of the command is shown in which all files are enlisted in our terminal.

Conclusion

In this guide, we discussed how the files are listed using the commands, and how we can list multiple files according to our needs without manually searching for them. By a single command, we can get our desired files. After introducing you to the methods of listing the files, we performed the various examples to make it easy for you to implement them according to your need.

Share Button

Source: linuxhint.com

Leave a Reply