Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1584351
  • 博文数量: 92
  • 博客积分: 2002
  • 博客等级: 大尉
  • 技术积分: 4717
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-01 17:09
文章分类

全部博文(92)

文章存档

2013年(1)

2012年(6)

2011年(85)

分类: LINUX

2011-11-29 17:22:04

It might save some people some agony to have the 'rm' command move files to the Trash folder instead of deleting files immediately. The easiest way to do this is to add the following line to '~/.bashrc':
Code:
    alias rm='mv --target-directory ~/.Trash'


The above alias command just causes 'mv' to be run instead of 'rm'. The problem with this is the command will overwrite files with duplicate names. To avoid overwriting files, the following alias can be used instead:
Code:
    alias rm='mv --verbose -f --backup=numbered --target-directory ~/.Trash/'


The problem with this new alias is it will not move files to the trash if there is a directory with the same name, or vice versa. The solution I use is to make a script '~/bin/trashit' that will move and rename files as necessary. I did not write this script, which you'll notice if you check the source to make sure it's safe...

Code:
#!/bin/sh
# trashit

# original script
#   
#    author: Shane Celis
#
# Sun, 20-May-2007; 06:47:22
#    minor changes...

if [ $# -eq 0 ]; then
        echo "usage: trashit " >&2
        exit 2;
fi

for file in "$@"; do
        # get just file name
        destfile="`basename \"$file\"`"
        suffix='';
        i=0;

        # If that file already exists, change the name
        while [ -e "$HOME/.Trash/${destfile}${suffix}" ]; do
                suffix=" - copy $i";
                i=`expr $i + 1`
        done

        mv -vi "$file" "$HOME/.Trash/${destfile}${suffix}"
done


Remember to give the script execute permissions. If you don't know how, maybe you shouldn't run scripts you find in forums? The new alias command to put in '~/.bashrc' becomes:
Code:
alias rm='~/bin/trashit'


You will need to close and restart any terminal windows before the alias will take effect. You should try creating some files and removing them to make sure the script is working.
Code:
touch t
rm t
touch t
rm t
touch t
rm t
touch t
rm t


In your trash folder, you should find files named:
Code:
t
t - copy 0
t - copy 1
t - copy 2


You might want to tweak the script to your liking. For instance, you might want to rename files based on the date and time they were deleted. It might also be useful for the script to detect what partition the files are in so that they can be moved to the partition's trash folder instead of the user's home directory.

Finally some warnings and notes:
The above script and alias will give you some protection from mistakes you might make on the command line if you use bash. If you use another shell, 'rm' will perform as usual. Try using 'trashit' instead.

The alias will also not work if you type 'sudo rm'. If you want to make sure you are sending files to the trash, you may use 'sudo ~/bin/trashit'.

Continue to be careful when running scripts. They will continue to call the real 'rm' command. Short of removing or replacing '/bin/rm', I do not know how to prevent scripts from using it.

If you want to use the real rm command, you can type '\rm'.
阅读(3405) | 评论(0) | 转发(0) |
0

上一篇:内置命令(build-in cmd)

下一篇:PHY的LED配置

给主人留下些什么吧!~~