Flylib.com

Books Software

 
 
 

More on rm (or Oops I Didn t Really Mean That.)


More on rm (or "Oops! I Didn't Really Mean That.")

When you delete a file with Linux, it is gone. If you didn't really mean to delete (or rm ) a file, it is time to find out if you have been keeping good backups . The other option is to check with the rm command before you delete a file. Instead of simply typing rm followed by the filename, try this:

rm -i file_name1 file_name2 file_name3

The -i option tells rm to work in interactive mode. For each of the three files in the example, rm will pause and ask if you really mean it:

rm : remove 'file_name1'?

If you like to be a bit wordier than that, you can also try

rm --interactive file_name

but that goes against the system administrator's first principle.

Of course, in following that principle, you could remove all the files starting with the word file by using the asterisk:

rm -i file*


Making Your Life Easier with alias

You might find that you want to use the -i option every time you delete anything, just in case. It's a lot easier to type Y in confirmation than it is to go looking through your backups . The problem is that you are adding keystrokes, and everyone knows that system administrators are notoriously lazy people. Then there's that whole issue of the first principle. That's why we shortened "list" to simply ls , after all. Don't despair, though—Linux has a way. It is the alias command:

alias rm='rm -i'

Now every time you execute the rm command, it will check with you beforehand. This behavior will only be in effect until you log out. If you want this to be the default behavior for rm , you should add the alias command to your local .bashrc file. If you want this to be the behavior for every user on your system, you should add your alias definitions to the systemwide version of this file, /etc/bashrc , and save yourself even more time. Depending on your distribution, alias definitions may already be set up for you. The first way to find out is to type the alias command on a blank line:

[root@website /root]# alias

alias cp='cp -i'

alias ls='ls --color'

alias mv='mv -i'

alias rm='rm -i'

Using the cat command, you can look in your local .bashrc file and discover the same information:

[root@website /root]# cat .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

# Source global definitions

if [ -f /etc/bashrc ]; then

        . /etc/bashrc

fi

Well, isn't this interesting? Notice the two other commands here, the cp (copy files) and mv (rename files) commands, and both have the -i flag as well. They too can be set to work interactively, verifying with you before you overwrite something important. Let's say I want to make a backup copy of a file called important_info using the cp command:

cp important_info important_info.backup

Perhaps what I am actually trying to do is rename the file (rather than copy it). For this, I would use the mv command:

mv important_info not_so_important_info

The only time you would be bothered by an "Are you sure?" type of message is if the file already existed. In that case, you would get a message like the following:

mv: overwrite 'not_so_important_info'?

Forcing the Issue

The answer to the inevitable next question of "What do you do if you are copying, moving, or removing multiple files and you don't want to be bothered with being asked each time when you've aliased everything to be interactive?" is this: Use the -f flag, which, as you might have surmised, stands for "force." Once again, this is a flag that is quite common with many Linux commands—either a -f or a --force .

Imagine a hypothetical scenario in which you move a group of log files daily so that you always have the previous day's files as backup (but just for one day). If your mv command is aliased interactively, you can get around it like this:

mv -f *.logs /path_to/backup_directory/

Musing

Yes, I know that mv looks more like move than rename . In fact, you do move directories and files using the mv command. Think of the file as a vessel for your data. When you rename a file with mv , you are moving the data into a new container for the same data, so it isn't strictly a rename—you really are moving files. Looked at that way, it doesn't seem so strange . Sort of.


The reverse of the alias command is unalias . If you want your mv command to return to its original functionality, use this command:

unalias mv