You are not authorized to post comments.

Unix tip: Useful Unix aliases

By Sandra Henry-Stocker  2 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.

ITworld LIVE

Operating SystemsWhite Papers & Webcasts

White Paper

A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

White Paper

Consolidating Lotus Domino x86 Workloads on IBM Power Systems

Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

White Paper

Task, workflow & issue management for teams. Try free!

Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

Webcast On Demand

Best Practices in Monitoring VMware

The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

Sponsor: Nimsoft

White Paper

How Nimsoft Service Desk Speeds Deployment and Time to Value

For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

See more White Papers | Webcasts

Ask a question

Ask a Question