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 something, 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 principlethat's why we shortened list to ls, after all. Don't despair, thoughLinux has a way. It is the alias command.

alias rm='rm -i' 


Now, every time you execute the rm command, it checks with you beforehand. This behavior is only 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 system-wide version of this file, /etc/bashrc, and save yourself even more time. There may already be alias definitions set up for you, even if only one or two. The first way to find out what has been set up for you is to type the alias command on a blank line.

$ alias alias ls='ls --color=auto' 


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

marcel@ubuntu:~$ cat .bashrc # enable color support of ls and also add handy aliases if [ "$TERM" != "dumb" ]; then     eval "`dircolors -b`"     alias ls='ls --color=auto'     #alias dir='ls --color=auto --format=vertical'     #alias vdir='ls --color=auto --format=long' fi # some more ls aliases #alias ll='ls -l' #alias la='ls -A' #alias l='ls -CF' 


Note

The preceding hash marks (#) represent comments and not the admin command prompt.


As you can see, there are other suggested aliases that you can uncomment by removing that leading hash mark. Incidentally, here are a few I like to add to everyone's .bashrc file (or to the global /etc/bash.bashrc):

 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' 


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

cp important_info important_info.backup 


Perhaps I am actually trying to 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

Inevitably, you're next question is this: 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? 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 commandseither 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 renameyou really are moving files. Looking at it 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 Ubuntu Linux
Moving to Ubuntu Linux
ISBN: 032142722X
EAN: 2147483647
Year: 2004
Pages: 201

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