How to exclude certain files or directories from the ls command output in Linux?
Published on Aug. 22, 2023, 12:17 p.m.
To exclude certain files or directories from the ls
command output in Linux, you can use the --ignore
or -I
option. This option allows you to specify a pattern that should be ignored by the ls
command. For example, to exclude all files and directories with the word “backup” in the name, you could use the following command:
ls --ignore=*backup*
This will list all files and directories in the current directory, except for those that contain the word “backup” in their name.
Alternatively, you can use the -I
option followed by the name of the file or directory you want to exclude. For example, to exclude a file named “temp.txt”, you could use the following command:
ls -I temp.txt
This will list all files and directories in the current directory, except for “temp.txt”. You can also use this option multiple times to exclude multiple files or directories.
Note that depending on your shell and their configuration, you might have to enclose the pattern in quotes to prevent the shell from interpretting the asterisk character before it is given to the ls
command as an argument.