Unix tip: Useful Unix aliases

By Sandra Henry-Stocker  14 comments

It's easy to create aliases and some of them can save you a lot of time and trouble. Stored in one of your startup files (e.g., .profile), they can provide a modestly easier environment for your work. This week, we'll look at a bunch of especially useful aliases.

If you use functions, but lose track of what you've called them, you can use this handy alias for listing them:

boson# alias functions='set | grep "()"'

Type "functions" on your command line with this function in effect and you might see something like this:

boson# functions
SHOW_ARGC=()
SHOW_ARGV=()
SHOW_LINENO=()
SHOW_SOURCE=()
STACK=()
GROUPS=()

You can also set up aliases for helping you to move around in the file system and find your way back. Some of these just save you a bit of typing, but consider how often you type cd commands.

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias back='cd $OLDPWD'

The last alias in the list above might prove particularly useful if you're working between two directories and just want to bounce back and forth between the two of them. Each time you move, $OLDPWD takes on the value of the directory you were just in.

There are a number of ways to get a listing of only the directories in your current file system location. This one seems the snappiest:

alias dirs='ls -d */'

To shorten what you have to type to get long listings or categorized listings, you might try these:

alias ll='ls -l'
alias lf='ls -F'
alias l='ls -al'

The "lf" alias will provide a file listing with / at the end of directory names and @ at the ends symbolic links. The "l" alias will, of course, provide a long listing of all files and with a nice savings on keystrokes.

A useful extension of the aliases shown above is to include a pipe to more so that you are sent one screenful of output at a time. This alias has been very nice to use:

alias lm="ls -al | more"

To be prompted before you remove files, you can use one of the following aliases. I generally don't want to have to type "y" for each and every file deletion, so I prefer leaving rm as is and using del when I want the extra check. Of course you can always just use "rm -i".

alias rm='rm -i'
alias del='rm -i'

You can use the same idea with other aliases that return a lot of text. It's a good idea to use a consistent naming scheme (i.e., adding an "m") so the aliases are easier to remember. Here's an example with the history command:

alias h=history
alias hm="history | more"

You can set up a case-insensitive grep command and save a little typing with this "gi" alias. Notice how easily it selects your target text without concern for how the text appears in the target file.

alias gi="grep -i"
boson# gi hello /tmp/msgs865
hello world
Hello World
HELLO WORLD

One additional alias that I have found very useful, since I am often looking for particular files and don't want directories in my listings is this "ff" (find file) alias:

alias ff="find . -type f -name"

This ff alias starts at the current directory. You could instead have two such aliases, one that looks from root and one that looks from the current directory:

alias ff="find / -type f -name"
alias f.="find . -type f -name"

Once you've defined and invoked a large useful set of aliases, you should be prepared to turn them off selectively or in one fell swoop. Keep the "unalias " command in mind if you want to disable a particular alias and "unalias -a" if you want to dsiable all of your aliases at once.

14 comments

    Anonymous 45 weeks ago
    Here's one I like to use, this one for Python applications, but easy to set up for other languages as well:alias greppy="find .
    Anonymous 46 weeks ago
    great tip, thanks!Here's one that I use. It's a little specific to me perhaps, but a good example of how the alias reduces an oft used command from a great long string to 4 charactersdiabetes mellitus symptomsalias cloc='/Users/verdon/Library/Scripts/cloc-1.08.pl –read-lang-def=/Users/verdon/Library/Scripts/cloc-definitions.txt'Bed sheetsNote: cloc is a script for counting lines of code
    Anonymous 48 weeks ago
    I really like your article. With his vast knowledge, we can learn more about their wonderful post.This is a very useful post. I really do not need information,birds for sale but it certainly helps to know. Learn new every day that's what I always say
    Anonymous 2 years ago
    Dorota Loi
    Dorota Loi 37 weeks ago in reply to Anonymous
    Here's one I like to use, this one for Python applications, but easy to set up for other languages as well:alias greppy="find .
    Anonymous 3 years ago
    I meant to say that 'cd' and comma both begin with a 'c', that's how I remember it.Also, in the 'now use just the alias' section, the second one should be ,b (not ,c).Ciao
    Anonymous 3 years ago
    THE CODE:cdA(){ A_old_dir=$PWD [ ! -z $* ] && A_new_dir=$* cd $A_new_dir}cdB(){ B_old_dir=$PWD [ ! -z $* ] && B_new_dir=$* cd $B_new_dir}cdC(){ C_old_dir=$PWD [ ! -z $* ] && C_new_dir=$* cd $C_new_dir}alias ,a=cdAalias ,b=cdBalias ,c=cdC# I use a comma as a shortcut to 'cd'# Both begin with a comma.# Now ,a is easier than cdA # Your choice, name them anything.# try these,a /usr/local/bin # puts you in specified directory,b /var/adm,c /usr/bin# now use just the alias (no directory),a # puts you in /usr/local/bin,c # puts you in /var/adm,c # puts you in /usr/bin# and my two favorite cd-related shortcutsalias ,=cd # cd to a directoryalias ,,='cd -' # cd back to the previous directory# I picked parts of these up over the years doing# UNIX support. I've tweaked them and made them # do what I've needed. Have fun.
    Anonymous 3 years ago
    Regarding the comment on not aliasing rm to 'rm -i' due to its effect in scripts:An alias shouldn't make any difference in scripts, as scripts should have hard paths for commands.Scripts should contain something like this:RM=/bin/rmLS=/bin/ls...rm is then called in the script as $RMThere are at least 2 reasons for doing so:* Your script will always run /bin/rm, not some script or executable named 'rm' that appears somewhere in your PATH This is the same reason you probably don't want ./ in your path * The PATH value becomes irrelevant to the running of frequently used commands in the script.Personally, I use a function hardpath() that determines the location of each executable within a predetermined set of directories, and sets the requisite variable to that location. The script fails if a required executable is not found in the path set.
    Anonymous 3 years ago
    You may not want to aliase rm -i as rm, especially if you use rm to remove files, etc using a script in cron, using the next choice would be better.
    Anonymous 3 years ago
    To go back to the previous directory I just use "cd -". No special alias is required. But what if you're working amongst three directories? What's a good way to quickly set up shortcuts which allow to bounce between them?
    Dorota Loi
    Dorota Loi 37 weeks ago in reply to Anonymous
    great tip, thanks!Here's one that I use. It's a little specific to me perhaps Scraps , but a good example of how the alias reduces an oft used command from a great long string to 4
    Anonymous 3 years ago
    Thanks - there were some useful aliases in your latest article. You might like this function, which I find very useful when looking for permission errors:lsdup() {here=$(pwd) while true do print "$(ls -ld) $PWD" cd .. [ $PWD = "/" ] && break donecd $here}

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Operating SystemsWhite Papers & Webcasts

      White Paper

      Microsoft Enterprise Agreement Program Overview

      Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

      White Paper

      Watson - A System Designed for Answers. The future of workload optimized systems design

      Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question