How to exclude certain files in `ls`?
I would like to run ls and exclude certain files in the output.
When I run the following command, I get all files, each on a separate line:
$ ls -1
file1
file2
file3
tempI would like to run this command in a way so that it shows:
$ ls -1 <insert magic here> temp
file1
file2
file3 4 Answers
ls -I <filename>-I: Ignore the filename, i.e. don't list the specified file.
To ignore more than one file add a -I before each filename.
ls -I file1 -I file2To ignore files by their name extensions do the following, for example.
ls -I "*.jpg" -I "*.svg" 3 For me, if I use -I once, it works, but if I use twice it doesn't.
E.g:
This works:
ls -I *.csvBut:
ls -I *.csv -I *.txtdoesn't work and returns
.txtfiles instead.
--ignore did the trick for me. This is what I needed and worked:
ls -lhrt --ignore="*.gz" --ignore="*.1"The above will list files from my log folder excluding old backup logs.
You can also use:
ls --ignore={"*.jpg","*.png","*.svg"} 1 I think that this produces the output you're looking for:
ls -1 !(temp)Apparently, you need shopt -s extglob for that to work (I have it enabled, so I guess some time in the distant past I found it useful and enabled it).
I guess you could also use grep to filter the output:
ls -1 | grep -v '^temp$'Using a pipe and filters provides a lot more flexibility, and skills that are transferable to other commands/situations, though you might not be interested in that for this specific case.