Making Your Life Easier with alias


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 



Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2003
Pages: 247

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net