Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In Linux, users use the ls command to list the contents of directories. We can use it with various options to customize the output and control which files and directories are displayed.

In this tutorial, we’ll explore three different methods for excluding a specific directory from the ls command’s output.

2. Understanding the Use Case

Before we begin, let’s assume that we have the following directory structure:

root/
├── dir1/
│   ├── file1
│   └── file2
│   └── one-tmp
├── dir2/
│   ├── file3
│   └── file4
├── dir3/
│   ├── file5
│   └── file6
└── tmp/
    ├── file7
    └── file8

To explain how to exclude a directory in Linux, we’ll use the above directory structure. We’ll discuss three different ways: using the ls command with the –ignore option, the grep -v command, and the find command.

Each method has its own advantages and disadvantages, and the appropriate method will depend on our specific needs and preferences. Regardless of which method we choose, being able to exclude a specific directory from the ls command’s output can be a useful skill to have in our toolkit as a Unix or Linux user.

Let’s now look at all the methods to ignore a directory using the ls command.

3. Using the –ignore Option

There is an option –ignore on the ls command that lets us specify a pattern to ignore. As a result, all files and directories matching that pattern will be excluded.

In the example below, we ignore all directories that end with “tmp”:

$ ls --ignore='*tmp'
dir1/ dir2/ dir3/

In this case, we can see that the “tmp” directory is excluded from the ls command output.

We can also perform a recursive search using the -R option:

$ ls -R --ignore='*tmp'
.:
dir1  dir2  dir3

./dir1:
file1  file2

./dir2:
file3  file4

./dir3:
file5  file6

Note that the one-tmp file in the dir1 directory is not included in the command output.

4. Using the grep -v Command

Another method for excluding a specific directory from the ls command’s output is to use the grep command with the -v option. The grep command searches for patterns in text, and the -v option inverts the search, meaning it displays all lines that don’t match the pattern.

To exclude a specific directory from the ls command’s output using grep, we can pipe the ls command’s output to grep:

$ ls | grep -v 'tmp'
dir1/ dir2/ dir3/

This will list all files and directories in the current directory except for the directory with the name “tmp”.

Also, we can use the -R option of ls to list the directories recursively as we saw above.

5. Using the find Command

The find command searches for files and directories based on various criteria, such as name, size, and modification time. We can combine it with the ls command to exclude a specific directory.

To exclude any directory from the ls command’s output, we can use the find command:

$ find . ! -name '*tmp*' -exec ls {} +
./dir1:
file1  file2  one-tmp
./dir2:
file3  file4
./dir3:
file5  file6

The above command will search for all the directories except for those containing tmp in their name. Note that we’ve used a regex to exclude the directories. We can use regex in the above commands as well.

6. Conclusion

In this article, we learned how to exclude a specific directory using the ls command. We discussed three different methods for excluding a specific directory from the ls command’s output. First, we used the –ignore option of the ls command. After that, we explored the same problem using the grep -v command and the find command.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!