Delete all files in a directory, but keep a specified file

Face questions: delete all files in a directory, but keep a specified file

answer:
Assuming that this directory is / xx /, there are file1, file2, file3..file10 ten files
[Root @ oldboy xx] # touch file {1..10}
[Root @ oldboy xx] # ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9

Method one: find
[Root @ oldboy xx] # ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
! [Root @ oldboy xx] # find / xx -type f -name “file10” | xargs rm -f
[Root @ oldboy xx] # ls
file10

! [Root @ oldboy xx] # find / xx -type f -name “file10” -exec rm -f {} \;
[Root @ oldboy xx] # ls
file10

Both a xargs pass through the Senate, an execution command parameter by -exec find to complete, are counted as find it
# Thank two enthusiastic brothers also enthusiastic answer old boy teacher, deputy head of the group C – Zhang Yao Beijing – Wu Xiaoyu

Old Boys teachers Comments: This method is the best! It will be way!

Method two: rsync
[Root @ oldboy xx] # ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[Root @ oldboy xx] # rsync -az –delete –exclude “file10” / null / / xx /
[Root @ oldboy xx] # ls
file10

Old Boys teachers Comments: This method is icing on the cake, bonus items!

Method three: Open the bash extglob function (! The purpose of this feature is to use rm (* jpg) in such a way to remove not included in the number of files)
[Root @ oldboy xx] # shopt -s extglob
[Root @ oldboy xx] # ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[Root @ oldboy xx] # rm -f! (File10)
[Root @ oldboy xx] # ls
file10
The third method is also grateful zealous brothers Beijing – Zhang rebirth Net
Old Boys teachers Comments: This method is futile, speak out but will make people think you are not level, a look that is searching out, but as the study of knowledge is possible! This method interview answer them may be reduced breakdown!

Then add the two methods (20150802 old boy from classroom education 25)
Method four:
find ./ -type f | grep -v “\ boldboy1 \ b” | xargs rm -f

Method five:
rm -f ls | grep -v" \ boldboy1 \ b "

From the point of view of operation and maintenance, after deleting any of operations should in advance or confirmed to perform backups in a backup exists.