有关linux防止误删除,很多地方都介绍引入alias转换rm命令为mv命令,下面的方法在网上较多地方被引用的,不信你用“alias undel”关键字google一下:
在bashrc加入以下指令,但是先要在/目录下创建一个名为.trash的子目录
alias rm 'mv -f !* ~/.trash'
alias undel 'mv ~/.trash/!* ./!*'
alias cleantrash '/bin/rm -rf ~/.trash; mkdir ~/.trash; sync'
alias lrm 'ls ~/.trash'
我发现这里alias定义rm等命令时,不知为何,都没有=号(我也查了其它几个网上的版本,但都没有这个=号),所以实际上根本没法生效。然后我自己加了=号上去,变成
alias rm='mv -f !* ~/.trash'
alias undel='mv ~/.trash/!* ./!*'
alias cleantrash='/bin/rm -rf ~/.trash; mkdir ~/.trash; sync'
alias lrm='ls ~/.trash'
我在我的RH9上配置,但在调用rm时,得到如下错误:
mv:can't stat source !*
我查了许多资料,想搞清楚mv -f !*中这个!号和!*究竟是什么含义,但一直找不到答案。这里想请教高手给指定迷津。[color=blue][/color]
bingosek 回复于:2005-08-01 13:03:00
你man一下alias有下面一段话:
alias [-p] [name[=value] ...]
Alias with no arguments or with the -p option prints the list of
aliases in the form alias name=value on standard output. When
arguments are supplied, an alias is defined for each name whose
value is given. A trailing space in value causes the next word
to be checked for alias substitution when the alias is expanded.
For each name in the argument list for which no value is sup-
plied, the name and value of the alias is printed. Alias
returns true unless a name is given for which no alias has been
defined.
可见,alias是要=的
bingosek 回复于:2005-08-01 13:36:53
!*有可能为$*,指后面所有的参数
sakulagi 回复于:2005-08-01 14:27:13
csh里的alias就是没有=
waker 回复于:2005-08-01 15:01:37
bashrc .............
应该是 .cshrc 吧?
在csh中 !*表示上一次命令的参数
neo2718 回复于:2005-08-01 15:32:27
哪位给个标准答案阿?
dahai01 回复于:2005-08-01 17:59:01
echo ${!*} a v
a v
就是表示你所输入的参数.
在这里就是 mv -f !* (!* 就是你要删除的file 你所输入的文件名就是它的值)
alexgaowei 回复于:2005-08-02 10:42:56
上面dahai01提到:!* 就是你要删除的file 你所输入的文件名就是它的值,我大概猜可能也是这样,就是没有找到一个地方专门有这个解释的,而且为什么我实际执行也行不通啊。
这个方法我在机械工业出版社的《Red Hat Linux 9桌面应用》同样找到,这个书上有用到=,其余一模一样。而且也是说加在bashrc上。
可有哪位高手顺便帮我试一下,如果有错误,贴出正确的,大家来共享。
JD_CHN 回复于:2005-08-02 14:05:10
关注。。
alexgaowei 回复于:2005-08-02 16:01:09
!*这个意思根据前面的回复我已经搞清楚了。比如你敲入命令:
ls test
然后再敲入一个命令,!*,这时结果就出现test。
[root@Linux member]# ls temp
a.out hi.c
[root@Linux member]# !*
temp
-bash: temp: command not found
心里有点纳闷的是这样简单的东西居然没有任何资料来说明,还得大家捣腾一下才明白……
现在我按前面介绍的方法已设置好alias,然后执行
[root@BitLinux member]# alias rm
alias rm='mv -f !* ~/.trash'
[root@BitLinux member]# rm temp
mv: can't stat source !*
我如果直接这样执行:
[root@BitLinux member]# ls temp
a.out hi.c
[root@BitLinux member]# mv -f !* ~/.trash
就没有问题。
请高手再给予几分关注,行的话,其实还是很有实际意义。
JD_CHN 回复于:2005-08-02 16:54:53
#alias rm
alias rm='mv -f !* ~/.trash/'
#rm aa
mv: when moving multiple files, last argument must be a directory
我测试的
dradhzn 回复于:2005-08-02 18:55:51
following commands run with Sun Solaris 8 .
make sure you do this in C shell
# csh
or to get useful debug info , ( pay special attention to the changes of the "\" before and after 6_6 )
# csh -x
dradhzn%
dradhzn% echo "alias rm 'mv -f \\!* ~/.trash' " >> ~/.cshrc
dradhzn% source ~/.cshrc
dradhzn% alias rm
mv -f !* ~/.trash
dradhzn% touch tf1 tf2 tf3
dradhzn% mkdir ~/.trash
dradhzn% rm tf1
dradhzn% rm tf2
dradhzn% rm tf3
dradhzn% ls tf*
No match
dradhzn% ls ~/.trash
tf1 tf2 tf3
to tentatively enable a rm alias for your session , use following command :
alias rm 'mv -f \!* ~/.trash'
some exlaination of C shell command history
http://archive.ncsa.uiuc.edu/General/Training/InterUnix/csh/history_2.html
alexgaowei 回复于:2005-08-03 13:59:00
如果删除的是文件,则出现楼上测试的结果:
#alias rm
alias rm='mv -f !* ~/.trash/'
#rm aa
mv: when moving multiple files, last argument must be a directory
但如果删除的是文件夹,则出现
mv:can't stat source !*
郁闷啊,郁闷
neo2718 回复于:2005-08-03 15:26:44
AIX下!*不行
JD_CHN 回复于:2005-08-03 18:09:46
别沉,继续ding......
waker 回复于:2005-08-03 21:37:26
你想要说明就在bash的man中
http://www.gnu.org/software/bash/manual/bashref.html#SEC119
9.3.2 Word Designators
JD_CHN 回复于:2005-08-05 13:54:01
真的没办法了吗
dradhzn 回复于:2005-08-05 18:25:45
引用:原帖由 "neo2718"]AIX下!*不行 发表:
tested with C shell in AIX 5.2
copy following to a file "run.csh" and make it executable
#!/bin/csh -x
uname -a
echo "alias rm 'mv -f \\!* ~/.trash' " >> ~/.cshrc
source ~/.cshrc
alias rm
touch tf1 tf2 tf3
mkdir ~/.trash
mkdir td1
ls -l t*
rm tf1
rm tf2
rm tf3
rm td1
ls -l t*
ls -l ./.trash
run it with ./run.csh , and this is runtime output .
uname -a
AIX cheetah 2 5 0008859F4C00
echo alias rm 'mv -f \!* ~/.trash'
source ~/.cshrc
alias rm mv -f !* ~/.trash
alias rm
mv -f !* ~/.trash
touch tf1 tf2 tf3
mkdir /home/dradhzn/.trash
mkdir td1
ls -l td1 tf1 tf2 tf3
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf1
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf2
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf3
td1:
total 0
mv -f tf1 /home/dradhzn/.trash
mv -f tf2 /home/dradhzn/.trash
mv -f tf3 /home/dradhzn/.trash
mv -f td1 /home/dradhzn/.trash
No match.
ls -l ./.trash
total 1
drwxr-xr-x 2 dradhzn staff 512 Aug 05 18:18 td1
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf1
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf2
-rw-r--r-- 1 dradhzn staff 0 Aug 05 18:18 tf3
nothing wrong with the file or directory . :|
JD_CHN 回复于:2005-08-11 11:43:35
bash 上有人试成功过吗
waker 回复于:2005-08-11 12:00:20
bash的alias扩展会把!*当普通字串扩展为 '!*',以后不可能进行历史扩展
netawater 回复于:2005-09-12 11:07:57
我在Bash下沒有用 !*
alias rm='mv --target-directory=~/Trash'
這樣也可以的。
sway2004009 回复于:2005-09-12 14:11:25
好像不行啊。
我的问题和楼主的一样。
哪位帮忙。
netawater 回复于:2005-09-13 08:58:09
用這個,那個好像只能root用。
alias rm='mv --target-directory ~/Trash'
qiuzl 回复于:2005-10-13 07:25:10
引用:原帖由 "waker"]bash的alias扩展会把!*当普通字串扩展为 '!*',以后不可能进行历史扩展 发表:
是啊,我看到有人这么写:
del () { /bin/mv -i ${*} ~/.Trash; }
99超人 回复于:2005-10-13 10:28:04
好象在csh下表示第一个参数的位置
ksh下是的参数只能是在最后
tianci3982730 回复于:2005-10-13 10:54:10
引用:原帖由 "waker" 发表: bashrc .............
应该是 .cshrc 吧?
在csh中 !*表示上一次命令的参数
正确
|