Linux使用别名alias将rm命令替换为mv避免误删除
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux使用别名alias将rm命令替换为mv避免误删除相关的知识,希望对你有一定的参考价值。
alias别名rm
- rm杀伤力太大
- 用alias将rm替换为mv
将删除的文件移动至/tmp文件夹下以删除时间命名的文件夹中
## 设定目标文件夹为DIR=/tmp/rm`date +%F%T
## 删除前先建立文件夹 mkdir $DIR
## 执行rm命令时相当于执行了mv -t $DIR
[03:27:51 root@C8-3-55 ~]#alias rm='DIR=/tmp/rm`date +%F%T`;mkdir $DIR;mv -t $DIR'
[03:31:23 root@C8-3-55 ~]#touch a..c1..3.txt;ls *.txt
123456.txt 2.txt a2.txt a5.txt b3.txt c1.txt c4.txt f2.txt passWord.txt
123.txt 奈曼.txt a3.txt b1.txt b4.txt c2.txt c5.txt name.txt PWD.txt
1.txt a1.txt a4.txt b2.txt b5.txt c3.txt f1.txt passwd.txt username.txt
[03:31:34 root@C8-3-55 ~]#rm [a..c][1..5].txt
[03:32:36 root@C8-3-55 ~]#ls *.txt
123456.txt 1.txt 奈曼.txt a3.txt b1.txt b3.txt b5.txt c3.txt f1.txt name.txt passWord.txt username.txt
123.txt 2.txt a2.txt a4.txt b2.txt b4.txt c2.txt c4.txt f2.txt passwd.txt PWD.txt
[03:32:45 root@C8-3-55 ~]#rm a..c*.txt
[03:33:19 root@C8-3-55 ~]#ls *.txt
123456.txt 123.txt 1.txt 2.txt 奈曼.txt f1.txt f2.txt name.txt passwd.txt passWord.txt PWD.txt username.txt
[03:33:31 root@C8-3-55 ~]#ls /tmp/rm2021-03-2503\\:3
rm2021-03-2503:32:36/ rm2021-03-2503:33:19/
[03:33:31 root@C8-3-55 ~]#ls /tmp/rm2021-03-2503\\:32\\:36/
a1.txt a5.txt c1.txt c5.txt
[03:34:19 root@C8-3-55 ~]#ls /tmp/rm2021-03-2503\\:33\\:19/
a2.txt a3.txt a4.txt b1.txt b2.txt b3.txt b4.txt b5.txt c2.txt c3.txt c4.txt
- 此操作中利用了mv -t 的特性
mv -t 可以将原文件和目标目录进行对调,从而实现将标准输入的内容可以执行
mv [选项]... -t 目录 源文件...
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
永久保存别名
将alias写进 .bashrc目录中
[03:59:13 root@C8-3-55 ~]#vim .bashrc
[04:00:41 root@C8-3-55 ~]#cat .bashrc
# .bashrc
# User specific aliases and functions
#alias rm='rm -i'
alias rm='DIR=/tmp/rm`date +%F%T`;mkdir $DIR;mv -t $DIR'
alias cp='cp -i'
alias mv='mv -i'
alias cip='cat /etc/sysconfig/network-scripts/ifcfg-ens33'
alias yi='yum -y install'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
以上是关于Linux使用别名alias将rm命令替换为mv避免误删除的主要内容,如果未能解决你的问题,请参考以下文章