shell脚本:删除当前目录下除了某几个文件之外的其他文件
有时会有这种特别的需要,就是删除当前目录下的所有文件,除了几个特别指定的文件。
一个特别的应用是:在使用VASP进行计算的时候,常常想要保留4个输入文件,删除剩余的文件。
如果没有一个特殊的脚本,那就需要一个一个的删除,文件多的时候会比较麻烦。
下面的内容可以帮助解决这个问题。
A pattern-list is nothing but a list of one or more patterns (filename) separated by a |. First, turn on extglob option:
shopt -s extglob
Bash remove all files except *.zip and *.iso files
The rm command syntax is:
##Delete all file except file1
rm !(file1)
##Delete all file except file1 and file2
rm !(file1|file2)
##Delete all file except all zip files
rm !(*.zip)
##Delete all file except all zip and iso files
rm !(.zip|.iso)
##You set full path too
rm /Users/vivek/!(.zip|.iso|*.mp3)
##Pass options
rm [options] !(.zip|.iso)
rm -v !(.zip|.iso)
rm -f !(.zip|.iso)
rm -v -i !(*.php)
相应的,用来删除VASP文件的脚本可以按着如下来写:
#!/bin/bash shopt -s extglob rm !(INCAR|KPOINTS|POSCAR|POTCAR|job.vasp)
参考:https://www.cyberciti.biz/faq/linux-bash-delete-all-files-in-directory-except-few/