March 27, 2017

March 22, 2017

How to exclude a specific files or directory from rm command in linux?


Behind Story:

Sometimes we have a list of files in a directory, and want to bulk remove the unnecessary files from those dirs and leave that specific may important.

References:

http://stackoverflow.com/questions/17184956/how-exclude-files-folders-for-remove

Notes:

From references I try two option to solve this problem:

1. By using find, grep, and xargs rm commands

find . -type 'f' | grep -v "NameToExclude" | xargs rm find

find . -type 'd' | grep -v "NameToExclude" | xargs rmdir



2. By using rm, and ls --ignore commands

rm -fr $(ls -1 --ignore=nameToExclude)

rm -fr $(ls -1 --ignore={"nameToExclude1","nameToExclude2"})


and for me, option no.2 is better and easier