Safe 'rm' command

From ScorecWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 13:22, 9 April 2009
Cwsmith (Talk | contribs)

← Previous diff
Current revision
Weisse (Talk | contribs)

Line 26: Line 26:
srm <directory> srm <directory>
will mv all sub-directories and files of <directory> to the ~/.Trash . will mv all sub-directories and files of <directory> to the ~/.Trash .
 +
 +[[Category:Tutorials]]

Current revision

Ever delete something via 'rm' that you wish you hadn't ??? [1]

The following instructions will create a 'srm' alias which executes a script that moves the target files and directories to the ~/.Trash instead of oblivion. If you wish you can alias the 'rm' command to execute this script; see instructions below.

Create a file rm.sh with the contents:

 #!/bin/bash
 mkdir ~/.Trash &> /dev/null
 while [ ! -z "$1" ]; do
   mv "$1" ~/.Trash/
   shift  #increment the loop variable
 done

Make it executable:

 chmod +x rm.sh

Alias 'rm' to your script:

 echo "alias srm='<path-to-script>/rm.sh'" >> .bashrc

If you wish you can alias the 'rm' command to execute the script by replacing 'srm' with 'rm' in the above command. NOTE: Some 'rm' commands become invalid as this script is using 'mv'


The alias will be set with the launching of subsequent terminals.


This command by default is recursive. For example:

 srm <directory>

will mv all sub-directories and files of <directory> to the ~/.Trash .

Personal tools