Linux Centos delete all files except a certain file(s)

I haven't written anything in a long time. Usually we can force delete all the contents in the current folder directly by rm -rf *, but sometimes we need to keep some files, for example, the website transfer update needs to keep the program compressed package, etc. It needs to be used in the linux centos environment. The function of deleting all files except a certain file(s) has been removed. The specific method is as follows:

rm -f! (a)

If you keep a and b, you can run rm -f !(a|b) to achieve this, but generally you will be prompted after running in bash

“-bash: !: event not found ”

This can be solved by running :shopt -s extglob .


 

The set command can be used to customize the shell environment, using option "o" to turn options on or off. For example, open the option: set -o option, close the option: set +o option.

For example, to open vi interactive command line editing, it is as follows:

[root@localhost ~]# set -o #View current settings
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
#Close xtrace off
[root@localhost ~]# set -o vi #Open the vi option
[root@localhost ~]# set -o #View the current setting
allexport off
braceexpand on
emacs off
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi on #xtrace off has been
opened

    Built-in set command options

 

option name  quick switch meaning 
allexport  -a  New or modified variables are automatically marked for output from the time this option is set until the option is reset 
braceexpand  -B Turn on curly brace expansion, which is a default 
emacs    Use emacs built-in editor for command line editing, which is the default 
erected -e  Exit when the command returns a non-zero exit status (failure). Not set when reading initialization file 
histexpand  -H  Turn on the ! and !! extensions when performing history replacement, a default 
history    Open command line history, default is open 
ignoreeof   It is forbidden to exit the shell with the EOF (Ctrl+D) key. You must type exit to exit. Equivalent to setting the shell variable IGNOREEOF=10 
keyword  -k   put keyword arguments into the environment of the command 
interactive-comments   For interactive shells, treat the text following the # sign as a comment 
monitor -m  Set up job control 
noclobber -C  Prevent files from being rewritten when redirected 
noexec -n  The command is read, but not executed. Used to check the syntax of the script. Interactive runtime does not turn on 
noglob  -d  Pathname expansion is prohibited. i.e. close wildcards 
notify -b  Notify user when background job completes 
nounset -u   Display an error message when expanding an unset variable 
onecmd -t  Exit after reading and executing commands 
physical -P   When set, suppress symlinks when typing cd or pwd. Use physical directory instead 
privileged -p  设置后,shell不读取.profile或ENV文件,且不从环境继承shell函数,将自动为setuid脚本开启特权 
verbose  -v  为调试打开verbose模式 
vi   使用vi内置编辑器进行命令行编辑 
xtrace -x  为调试打开echo模式 
     

 

 
    shopt命令是set命令的一种替代,很多方面都和set命令一样,但它增加了很多选项。可有使用“-p”选项来查看shopt选项的设置。“-u”开关表示一个复位的选项,“-s”表示选项当前被设置。
 

[root@localhost ~]# shopt -p    #查看当前设置情况
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -s checkwinsize
shopt -s cmdhist
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u gnu_errfmt
shopt -u histreedit
shopt -u histappend
shopt -u histverify
shopt -s hostcomplete
shopt -u huponexit
shopt -s interactive_comments
shopt -u lithist
shopt -s login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo

[root@localhost ~]# shopt -s cdspell   #把cdspell选项打开
[root@localhost ~]# shopt -p cdspell   #打印cdspell设置
shopt -s cdspell
[root@localhost ~]# cd /raot           #故意拼写错误
/root
[root@localhost ~]# pwd                #查看当前目录
/root
[root@localhost ~]# cd /usr/loca/bin   #故意拼写错误
/usr/local/bin
[root@localhost bin]# pwd              #查看当前目录   
/usr/local/bin
[root@localhost bin]# shopt -u cdspell #把cdspell选项打开
[root@localhost bin]# shopt -p cdspell #打印cdspell设置
shopt -u cdspell
[root@localhost bin]# cd /raot         #故意拼写错误
-bash: cd: /raot: 没有那个文件或目录     #进入目录错误

 
shopt命令选项
选项 含义 
cdable_vars 如果给cd内置命令的参数不是一个目录,就假设它是一个变量名,变量的值是将要转换到的目录
cdspell 纠正cd命令中目录名的较小拼写错误。检查的错误包括颠倒顺序的字符,遗漏的字符以及重复的字符。如果知道一处修改,正确的路径就打印出,命令将继续。只用于交互式shell
checkhash bash在试图执行一个命令前,先在哈希表中寻找,以确定命令是否存在。如果命令不存在,就执行正常路径搜索
checkwinsize bash在每个命令后检查窗口大小,如果有必要,就更新LINES和COLUMNS的值
cmdhist bash试图将一个多行命令的所有行保存在同一个历史项中。这使得多行命令的重新编辑更方便
dotglob bash在文件名扩展的结果中包括以点(.)开头的文件名
execfail 如果一个交互式shell不能执行指定给exec内置命令作为参数的文件,它不会退出。如果exec失败,一个交互式shell不会退出 
expand_aliases  别名被扩展。默认为打开 
extglob 打开扩展的模式匹配特征(正常的表达式元字符来自Korn shell的文件名扩展) 
histappend  当shell退出时,历史清单将添加到以HISTFILE变量的值命名的文件中,而不是覆盖文件 
histreedit  如果readline正被使用,用户有机会重新编辑一个失败的历史替换 
histverify 如果设置,且readline正被使用,历史替换的结果不会立即传递给shell解析器。而是将结果行装入readline编辑缓冲区中,允许进一步修改 
hostcomplete  如果设置,且readine正被使用,当正在完成一个包含@的词时bash将试图执行主机名补全。默认为打开 
huponexit  如果设置,当一个交互式登陆shell退出时,bash将发送一个SIGHUP(挂起信号)给所有的作业 
interactive_comments  在一个交互式shell中,允许以#开头的词以及同一行中其他的字符被忽略。默认为打开 
lithist  如果打开,且cmdhist选项也打开,多行命令将用嵌入的换行符保存到历史中,而无需在可能的地方用分号来分隔 
mailwarn  如果设置,且bash用来检查邮件的文件自从上次检查后已经被访问,将显示消息“The mail in mailfile has been read” 
nocaseglob  如果设置,当执行文件名扩展时,bash在不区分大小写的方式下匹配文件名 
nullglob 如果设置,bash允许没有匹配任何文件的文件名模式扩展成一个空串,而不是它们本身 
promptvars  如果设置,提示串在被扩展后再经历变量和参量扩展。默认为打开 
restricted_shell  如果shell在受限模式下启动就设置这个选项。该值不能被改变。当执行启动文件时,不能复位该选项,允许启动文件发现shell是否是受限的 
sourcepath 如果设置,source内置命令使用PATH的值来寻找包含作为参数提供的文件的目录。默认为打开 
source  点(.)的同义词 
shift_verbose 如果该选项设置,当移动计数超过位置参量个数时,shift内置命令将打印一个错误消息 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214979&siteId=291194637