Best Unix time-savers

time moyan brenn
flickr / Moyan Brenn (Creative Commons BY or BY-SA)

One good way to prepare for the upcoming new year is to contemplate all of the ways that we can make working on our Unix systems smoother and easier. Here are some of my favorite time-savings techniques.

Reusing history -- hitting that up arrow rather than retyping commands -- and using all of the tricks shown last week for reusing portions of previous commands.

Using command and file completion -- hitting that tab character so that you don't have to type full commands or pathnames. If you have to work in directories like /home/oracle/jboss-4.2.2.GA/server/default/deploy/app.ear/app.war/logs as I do, any shortcut for getting there will save you time again and again.

Another option for those overly pathnames is to set up symbolic links. Just remember where you install them -- in your home directory or maybe in a dedicated links directory?

You could also create, instead, aliases that take you to these special places. Using an alias like this would make the job of moving to my overly long path directory even easier:

alias cdlogs='cd home/oracle/jboss-4.2.2.GA/server/default/deploy/app.ear/app.war/logs'

Other especially handy aliases might be those that help you to quickly find problems such as overly large files or files with overly generous permissions. Here are a couple for finding big files. Modify the size to suit your needs. The examples below are looking for files that use more than a million blocks and are more than a gigabyte in size -- very similar if not the same results. You can also base the file size on bytes, kilobytes, etc. and change your starting point.

alias findbig='find /home -size +1000000b -ls'
alias findbig2='find /home -size +1G -ls'

Finding files by ownership or permissions can also prove a useful alias if you need to do this kind of thing frequently enough.

This alias will find files that have no owners (i.e., onwer UIDs are not included in the /etc/passwd file).

alias findorphans='find / -nouser -ls'

And if you use aliases, it's also good to know how to unset them when they're not doing what you want. An alias like rm -i when you don't want to confirm 88 file deletions, for example, can be very annoying. Keep in mind that you can shut an alias off using the unalias command.

unalias rm

You can also save time by taking advantage of some useful bash shortcuts like ^L that clears your terminal window.

^L -- clears your window
^A -- moves to start of the line
^E -- moves to the end of the line
^U -- deletes everything to the left of your cursor
^K -- deletes evrything to the right of your cursor
^W -- deletes the word to the left of your cursor
^Y -- pastes the content you most recently deleted
alt-f -- goes forward one word
alt-b -- goes backward one word

I also suggest killing processes with pkill so that you don't have to first determine their process IDs and then use kill. Just be careful if you're working as root that you're not killing more than you intended to. The pkill command can terminate processes using substrings, so a command like pkill or will kill oracle as well as ornery.

And there's no better way to do things over and over again than to turn them into cron jobs. Just don't forget about them completely. Every now and then, you should probably verify that your cron jobs are still doing what they were intended to do.

You can also take advatntage of a nice trick for returning to whetevr directory you just left. The process requires use of the cd - command and looks like this:

/home/me>  cd /usr/local/bin
/usr/local/bin> cd -

You can also set up useful functions in one of your dot files. Here are a couple that I like. Put these in your .bash_profile or .bashrc file). The first creates a directory and moves you into it. The second creates a backup of a file before you edit it with vi.

function mkcd()
    mkdir $1
    cd $1

function visv()
    cp $1 $1.bak
    vi $1

And, of course, you should always consider committing to scripts those things you need to do frequently -- especially if the commands require you to be very fastidious. Always commit your most difficult commands to scripts or aliases so that you only have to work them out (and test them) once before you use them over and over again.

Even after 30 years of using Unix, I still find it's fun, but streamlining my work makes it even more fun. Please add a comment describing one of your favorite time-saving techniques.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2014 IDG Communications, Inc.