originally published on December 12, 2005 at
newsforge.com
If you spend any time working with the shell, you probably use many GNU
utilities. One thing that distinguishes the GNU versions from the
classic Unix versions is that the GNU programs are rife with additional
options. Some of these options are so useful you may want to create an
alias so you can use them all the time.
A shell alias is a simple way to create your own custom command.
To make your aliases available every time you open a BASH shell, add
them to your $HOME/.bashrc file. For other shells, place them in the
associated run control file. Many distributions include a basic set of
aliases for all users. When I start working on a new system or get a
new account, I immediately install my favorite aliases.
An alias is created using the following syntax:
alias name='value'
where name is the name of the new command and value is the command
string that is executed. Note, if you create an alias with the same
name as an existing executable program in your path, the shell will run
the alias instead of the program.
To remove an alias from your current shell session, use:
unalias name
where name is an existing alias.
To see all the current aliases known to your shell, run alias
with no parameters.
All of the following aliases were created and tested on SUSE 10 with
the BASH shell.
List the most recently modified files and directories
alias lt='ls -alt | head -20'
Once this alias is loaded, typing lt lists the most
recently modified contents of the current directory. The a
and l options show all files including hidden files in a
long listing format. Since I am usually interested in a file I have
just changed, I limit the output to 20 lines with the head
command.
List only subdirectories
alias lsd='ls -al -d * | egrep "^d"'
This shows only subdirectories of the current directory by using egrep
to limit the listing to entries with the 'd' directory attribute.
Show the inode number for files in the current directory
alias li='ls -ai1 | sort'
This prints the inode number for each file, sorted in ascending order.
The last option to ls in this alias is the numeral one, not the letter L.
You might need the inode number for file system repair or to track down
a file name referenced by inode in an SE Linux log file.
Show the SE Linux security context of all processes
alias pss='ps --context ax'
As the NSA Security Enhanced Linux code starts to appear in mainstream
distributions, the security context of processes is something you may
need to track.
Fast directory navigation with pushd and popd
alias +='pushd .'
alias _='popd'
I tend to move around a lot on the command line. If I am working on
code in a deeply nested directory, but need to go check some log files,
I'll use the shell built-in pushd command to save my current directory
location and popd to get back to the directory later. These aliases
simplify the process by entering '+' before changing
directories, and '_' to return to the directory later. You
can push more than one directory on to the stack and pop them back off
in reverse order. Note, I used the underscore instead of the minus sign
for popd because the minus sign is a reserved symbol.
Find disk space abusers
alias dusk='du -s -k -c * | sort -rn'
Shows disk usage of files and subdirectories in the current directory,
sorted with the ones using the most disk space first. The report shows
disk usage in 1K increments and the total for the directory at the top.
Beware, if you start this command from the root directory or at the top
of a deeply nested directory, it could run a long time. Here is a sample
report:
keithw@kingtux:~/datacore/scripts$ dusk
2216 total
1160 irc
308 awk
252 learning-bash-examples
136 perl
76 new_script-1.1.0
36 bash-extras
16 python
12 restore
12 pipes
12 expect
12 download
12 bash-functions
Show all programs connected or listening on a network port
alias nsl='netstat -alnp --protocol=inet | grep -v CLOSE_WAIT |
cut -c-6,21-94 | tail +2'
Uses netstat to show the process ID and program name of everything
either connected or listening on a network port, including the sockets
of the sending and receiving hosts. This is a great way to make sure no
unexpected services are running in the background. To see all available
information, this command should be run as root. Here is a sample report:
root@kingtux:~# nsl
Proto Local Address Foreign Address State PID/Program name
tcp 0.0.0.0:771 0.0.0.0:* LISTEN 3981/rpc.statd
tcp 0.0.0.0:139 0.0.0.0:* LISTEN 3830/smbd
tcp 0.0.0.0:111 0.0.0.0:* LISTEN 2908/portmap
tcp 0.0.0.0:80 0.0.0.0:* LISTEN 4036/apache
tcp 0.0.0.0:22 0.0.0.0:* LISTEN 3838/sshd
tcp 0.0.0.0:631 0.0.0.0:* LISTEN 3582/cupsd
tcp 127.0.0.1:25 0.0.0.0:* LISTEN 3761/exim4
tcp 0.0.0.0:7100 0.0.0.0:* LISTEN 3875/xfs
tcp 0.0.0.0:445 0.0.0.0:* LISTEN 3830/smbd
tcp 192.168.1.101:50372 66.35.250.177:80 ESTABLISHED 6585/firefox-bin
[snip]
Quickly find packages in the RPM database
alias rpmq='rpm -qa | grep $1'
This alias takes one parameter that is used to query the RPM database
and return all package names that contain the string. The usage is
simply: 'rpmq string'.
Combinations and Permutations
If you want to dig deeper into any of the options used in these aliases,
check out the man/info page for the command. There are so many
permutations of options and commands that you can sometimes stumble
across a cool solution by studying the more obscure options in the
man/info pages. What are your favorite aliases?

This work is licensed under a
Creative Commons Attribution-NonCommercial 2.5 License.