Americas

  • United States
sandra_henrystocker
Unix Dweeb

Unix aliases for good and evil

How-To
Jun 20, 20166 mins
Data CenterLinux

The whole point of Unix aliases is to allow you to type less, make fewer mistakes, and not have quite so much to remember. They are generally act as good shortcuts to getting a lot of work done.

Here are some aliases that can be very handy for helping you get work done — and a few that might just drive your coworkers nuts.

Listing files and sorting by size

Here are some useful aliases for listing interesting files. In the first, the largest files are listed last (r=reverse order) so they’ll still be in view when the command completes. The “h” means that file sizes will be displaying in megabytes or gigabytes.

alias FbySize='ls -Slrh'

Listing files with the largest shown first is simpler because this is the default sort.

alias listBySize='ls -Sl'

Listing directories by size requires use of the du (disk usage) command. This particular command will display only the top level directories. Remove the middle expression (the egrep command) if you want to include the subdirectories as well.

alias DbySize='du -kx | egrep -v "./.+/" | sort -n'

Listing files by date

I often find myself wanting to look at recently added or updated files — for example, if I just put a new script together and can’t remember what I called it. This command looks for files that were added or updated within the last 24 hours.

alias findRecent='find . -type f -mtime 0'

If you want to see the update times to confirm the files’ ages, add -ls to the find command.

alias findRecent='find . -type f -mtime 0 -ls'

If you want to look only in your home directory (rather than the current directory), simply change the dot (.) to a tilde.

alias findRecent='find ~ -type f -mtime 0'

Finding files you own or don’t own

You can look for files that you own by specifying the owner in the find command. To make this alias generic (so it would work for anyone), use the ${USER} argument as shown in the second alias below rather than using the specific username.

alias findMine='find . -user myname'
alias findMine='find . -user ${USER}'

The findMine alias will work in the current directory to the extent that you have permission to search it.

In the find alias below, you’re going to find files that belong to other users, but are located in your home directory. And, as before, if you want to see the details (i.e., who actually owns these files), you can add -ls to the command as shown in the second line below.

alias findOther='find ~ ! -user ${USER}'
alias findOther='find ~ ! -user ${USER} -ls'

When I run a command like one of those, I generally find some files that belong to root as I may have switched to root and prototyped some admin scripts in my home directory. The commands can be read “find, starting in my home directory, files where the owner is not (!) me.

Finding empty files

You can also look for empty files and even for empty subdirectories using the find command’s convenient empty argument.

alias findempty='find ~ -type f -empty'
alias findEmptyDir='find /tmp -type d -empty'

Listing files with a purpose

Some of the more routinely used aliases for listing files allow you to list your files in many useful ways.

For example, you can list all files by including the -a option. Many people call an alias like this “lla”, but calling this command “all” seems rather fitting.

alias all="ls -al"

Listing files with typing indicators (like * if a file is executable and @ if it’s a symbolic link) requires just the addition of the F option.

alias la="ls -aF"

By the way, you can use an uppercase “A” in place of “a” if you don’t want to see the . and .. entries.

Show the entry for just the current directory.

alias ld="ls -ld"

And probably the most common alias for listing files. It’s amazing how good saving a few keystrokes can feel.

alias ll="ls -l"

You can also easily list files with the most recently updated showing up last. This is similar to what we did with the find command earlier, but it’s only listing files in the current directory.

alias ltr='ls -Art'

Again, the “r” and “t” options order the files by time, reversing the normal order and the “A” omits the . and .. entries.

Other useful aliases

Aliases can also be used for a lot of other common tasks (not just finding or listing files), such as making files executable. This particular alias gives everyone execute privilege to whatever file or set of files it’s used with.

alias ax="chmod a+x"

And here’s an alias for starting top with an option to display tasks according to how much memory they are using. The default is to order tasks by their CPU usage.

alias mem='top -a'

Not so nice aliases

You can also use the alias command to play some nasty pranks on your coworkers. Just make sure their desks are far enough away from yours that you won’t catch the first wave of anger when they jump up and start to scream.

alias ls='echo "Segmentation fault" && echo $* > /dev/null'

This alias will probably startle the person who innocently types “ls” and will hide any parameters that they might have provided with their ls command. It will likely take a while for them to figure out what actually happened. So, it might be a good time for a bathroom break.

The next alias is arguably more benign. It makes it look as if the user is always in his home directory. I’m not sure I’d ever recommend something like this, but it helps one to recognize how easily one’s sense of the system can be confused by a very simple alias.

alias pwd="echo ~"

A more dastardly prank involves making any attempt to kill a process look as if the system is being shut down. Proceed with caution!

alias kill='echo system shutting down && echo $* > /dev/null'

Wrap Up

Aliases are generally helpful shortcuts — used to make your life and your users’ time on the command line easier and more pleasant, but they can also be used to wreak havoc on unsuspecting users and admins. Don’t forget that the alias command by itself (no arguments) will list all of the aliases that are in effect while you are working. When strange things happen on the command line, it might not be a bad idea to review what your aliases are doing. As the photo shown with this post (an elevator shaft in the Empire State Building) suggests, not all shortcuts are worth exploring.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author