Author: Keith Winston
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 add my favorite aliases to my .bashrc.
To create an alias, use the syntax alias name='value'
where name is the name of the new command and value is the command string that is executed. 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 the existing alias.
To see all the current aliases known to your shell, run alias
with no parameters.
I created and tested all of the following aliases on SUSE 10 with the bash shell.
List the most recently modified files and directories
alias lt='ls -alt | head -20'
Once it’s 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 by piping the output from ls -alt
to the head
command. This example also demonstrates that an alias may be a series of commands, rather than just a short name for a single utility and its options.
List only subdirectories
alias ld='ls -al -d * | egrep "^d"'
This alias 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 alias 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 Security Enhanced Linux (SELinux) log file.
Show the SELinux security context of all processes
alias pss='ps --context ax'
As the SELinux code starts to appear in mainstream distributions, the security context of processes is something you may need to track. This alias displays the security context of all processes. The --context
option tells ps
to display the security context, while the a
and x
options combine to select all processes.
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 that 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'
The dusk
alias 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 take a long time to run. The report from dusk
will look something like this:
keithw@penguin:~/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
If you want to make the output even easier to read, throw in the -h
(human-readable) option, and du
will print the file sizes in kilobytes, megabytes, gigabytes, and so on.
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'
The nsl
alias 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. It uses grep
to sort through the netstat output and remove lines that match CLOSE_WAIT
, so you see only programs that are listening or connected.
This is a great way to make sure no unexpected services are running in the background. To see all available information, run this command as root. The output from nsl
will be similar to this sample:
root@penguin:~# 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 returns 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 or 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 and info pages. What are your favorite aliases?