1. Introduction
The ls command is commonly used to examine the contents of directories and their files. However, it doesn’t readily support direct pattern-matching methods like wildcards or regular expressions to include or exclude particular files.
In this tutorial, we’ll look at how we can use the —ignore* and —*hide options of ls, as well as other commands to ignore or hide files from the output**.
First, we’ll discuss ls —ignore. After that, we’ll look at the —hide option in detail. In addition, we’ll explore how Bash performs globbing. Lastly, we’ll combine the grep command with the ls command to list and filter files.
2. Sample Directory
Let’s first use the tree command to take a look at the sample directory we’ll work on:
$ tree
.
├── dir1
│ ├── image1.jpg
│ ├── image1.png
│ ├── image1.svg
│ ├── file1.txt
│ └── file2.txt
├── dir2
│ ├── image2.jpg
│ ├── image2.png
│ ├── image2.svg
│ ├── file1.html
│ └── file2.html
└── file1.txt
└── file2.txt
└── file3.txt
2 directories, 14 files
Here, we have a main directory which consists of two subdirectories. Each subdirectory contains different types of files. To learn how to exclude filesystem objects from the output of ls, we’ll use the above structure.
3. Using ls –ignore
The ls command includes the –ignore option, which enables us to specify a pattern for exclusion. As a result, the command excludes the files that don’t match with the pattern.
The basic syntax of –ignore takes PATTERN as its value:
ls --ignore=PATTERN
In this case, we can pass any literal name or an actual pattern.
3.1. Exclude Files
First, let’s use –ignore with ls to filter and ignore specific files. In this approach, we’ll replace PATTERN with the actual filename:
$ ls --ignore=file1.txt
dir1 dir2 file2.txt file3.txt
Similarly, to ignore more than one file, we can add –ignore as many times as we need:
$ ls --ignore=file1.txt --ignore=file2.txt
dir2 dir2 file3.txt
Hence, we ignore multiple files by adding multiple –ignore options.
3.2. Asterisk (*) Wildcards
In this approach, we use the asterisk (*) wildcard to match any character. This wildcard matches a sequence or string of characters, including one without any characters.
In the example below, we ignore the .txt files in dir**1:
$ ls --ignore "*.txt" ./dir1/
image1.jpg image1.png image1.svg
By comparing the output with our initial structure, we can see that all the .txt files are excluded. Using the asterisk (*) wildcard, we’ve filtered all the files that have the same extension.
3.3. Question Mark (?) Wildcards
Another wildcard we can use to match filenames is the ? question mark. This symbol matches exactly one character and filters the files accordingly.
In this example, we’ll ignore the files starting with file:
$ ls --ignore "file?.txt" ./dir1/
image1.jpg image1.png image1.svg
Notably, file1 and file2 from dir1 aren’t included in the command output.
3.4. Processing Multiple Files
We can also apply the –ignore option on multiple files to filter them in just one go and get our desired result.
For this example, we’ll filter two different file types:
$ ls --ignore "*.jpg" --ignore "*.txt" ./dir1/
image1.png image1.svg
Alternatively, we can enclose the extension in curly brackets {…} to include multiple file types, i.e., use brace parameter expansion. This enables us to generate list values by specifying patterns inside curly braces. In the brace expansion, we list the values we want to exclude:
$ ls --ignore={"*.jpg","*.png","*.svg"} ./dir1/
file1.txt file2.txt
This command lists all the files except the ones ending with the .jpg, .png, and .svg extensions.
3.5. Performing Recursive Search
To perform the filtering operation recursively over a directory with ls and –ignore, we add the -R option. This option runs the operation recursively through any subdirectories as well.
For this example, we’ll switch back to the main directory from dir1 and execute the command:
$ ls -R --ignore "*.txt"
.:
dir1 dir2
./dir1:
image1.jpg image1.png image1.svg
./dir2:
file1.html file2.html image2.jpg image2.png image2.svg
Using the globbing approach enabled us to filter the files in a single directory. However, with the recursive option, we can perform the filtering option in the subdirectories as well.
4. Using the –hide Option in ls
Another approach to filter and exclude files using the ls command is to hide the files using the –hide option. Similar to the –ignore option, we specify patterns to exclude and wildcards to pattern-match more than one file in the –hide option.
In the command below, we’ll ignore the .jpg files in dir2:
$ ls --hide=*.jpg ./dir2/
image2.png image2.svg file1.html file2.html
In addition, the –hide option also supports the use of wildcards.
Let’s consider a scenario where we want to view none of the files except the one passed as a parameter. In this case, we’ll use [!…]:
$ ls --hide=[!file2.html] ./dir2/
dir22 file1.html
This outputs only the file we passed to the –hide option. The rest of the files are excluded. However, any subdirectories are visible in the output.
5. Using grep With ls
Apart from employing the ls command and its options alone or with globbing, we can also combine grep with ls to filter files. The grep command is a powerful utility for pattern and text matching.
In this section, we’ll use grep to filter the output of ls for all the files that match a particular pattern and ignore them. After that, we can pipe the output to the ls command to work with all files that don’t match the pattern.
5.1. Using grep -v
Let’s first execute a grep command over dir1:
$ ls | grep -v ".txt" ./dir1/
image1.jpg
image1.png
image1.svg
In the above code, we use the ls command to get the entire directory content. After that, we pipe the output to the grep command with the -v option. This option is used to invert the matching pattern. In other words, it matches the .txt file extension and excludes it.
5.2. Using xargs
Similarly, we can combine grep and ls with xargs to ignore any unwanted files. The xargs or eXtended ARGuments is a command that reads items from standard input and then passes those items as an argument to a specified command.
In the example below, we use extended arguments to filter the .html files in dir2 directory:
$ ls ./dir2/| grep -Ev ".html" | xargs ls
image2.jpg
image2.png
image2.svg
Here, the first part lists the files and subdirectories in the current directory.
After that, we pipe the output of ls to grep to search for the pattern .html. Also, we add -E for extended regular expressions and -v to invert the match. In other words, we exclude any file containing the .html extension.
Lastly, we use xargs which takes each line of the input and passes it as an argument to the ls command. In this example, ls prints the files that don’t have the .html extension.
6. Conclusion
In this article, we learned how to ignore and hide filesystem objects from the output of the ls command.
First, we looked at the –ignore option along with the literals and wildcards we can use for pattern-matching. After that, we discussed the –hide option to hide files according to patterns. Lastly, we saw the grep command and its pattern-matching capability to ignore and hide objects from the output of ls.
Each method here has its own advantages and disadvantages. The appropriate method to ignore and hide files depends on the specific needs and preferences.