Wednesday, June 24, 2015

Deleting Files With Little Extra Care On Linux Command Shell !

SeaMonkey 2.31 Release Notes We know that deleting files on a Linux command prompt is an irreversible process. Whilst files deleted from a GUI file manager are usually moved to recycle-bin from where they can be recovered later if they are deemed to have been deleted mistakenly, such fortune is not available to files deleted on the command prompt. Once deleted using command `rm` they are lost permanently (while there tricks and tools to recover them are available, process is not easy and full-proof).

Command `rm` has two command-line switches that can prevent you from accidentally deleting files that you do not intend to. It does so by prompting you.

Both these switches are explained below.


Switch -i

Usage:
  $ rm -i FILES ...

EXAMPLE:
 $ touch a b c d e f 
 $
 $ ls ?
 a b c d e f
 $
 $ rm -i a b c d e f
 rm: remove regular empty file ‘a’? y
 rm: remove regular empty file ‘b’? n
 rm: remove regular empty file ‘c’? y
 rm: remove regular empty file ‘d’? n
 rm: remove regular empty file ‘e’? y
 rm: remove regular empty file ‘f’? n
 $
 $ ls ?
 b d f
 $

The above example demonstrates that -i switch deletes files interactively. In other words, it asks you whether or not you are sure of your action before removing each file.

Switch -i can slow you down if you are heavy into housekeeping and mostly know what you are doing. The next switch -I can be your friend if you are one of the above.


Switch   -I  (Note: that is uppercase I as in Icecream, not lowercase l as in linux)

Usage:
 $ rm -I FILES ...


OBSERVE THE NEXT SET OF EXAMPLES CAREFULLY:

EXAMPLE - Deleting More Than 3 Files
$ touch a b c d e f
$
$ ls ?
$
a  b  c  d  e  f
$
$ rm -I a b c d e f       # Deleting more than 3 files, would prompt (only Once)
rm: remove all arguments? y
$
$ ls ?
ls: cannot access ?: No such file or directory



EXAMPLE - Deleting 3 Or Less Files
$ touch a b c d e f
$
$ ls ?
a  b  c  d  e  f
$
$ rm -I a b c     # Would not prompt for less than or equal to 3 files
$
$ ls ?
d  e  f
$




EXAMPLE - Deleting Files Recursively
$touch a b c d e f
$
$ mkdir -p dir1/dir2
$
$ touch dir1/file1 dir1/dir2/file2
$
$ ls -R dir1
dir1:
dir2  file1

dir1/dir2:
file2
$
$ rm -R -I dir1 # Prompts because of recursion, regardless of the total number of files
rm: remove all arguments recursively? y
$
$ ls -R dir1
ls: cannot access dir1: No such file or directory
$


Above examples demonstrate that -I switch prompts only under two cases:
 (a) Prompts (only once) if the number of files being asked to be deleted on the command prompt are more than 3
 OR
 (b) Prompts (only once) if the files are requested to be deleted recursively through sub-directories (recursive = -R switch)


Switch   -v
In addition to -i or -I, one can add another switch -v (Verbose), as shown below, and receive feedback as to which files were actually deleted (!!) by the command `rm`.

USAGE:
$ rm -v -i FILES ...

OR

$ rm -v -I FILES ...


EXAMPLE:
$ touch a b c d e f
$
$ ls ?
a  b  c  d  e  f
$
$ rm -v -I a b c d e f
rm: remove all arguments? y
removed ‘a’
removed ‘b’
removed ‘c’
removed ‘d’
removed ‘e’
removed ‘f’
$
$ ls ?
ls: cannot access ?: No such file or directory
$

If typing extra command-line switches following the `rm` command every time seem tedious, then we can use `alias` command to ease that. I will soon write a blog on `alias` command, come back and check.


Hope this helps.  Enjoy your time exploring and learning Linux.