Americas

  • United States
sandra_henrystocker
Unix Dweeb

17 Unix tricks for a happy 2017

How-To
Dec 22, 20166 mins
Data CenterLinux

Hoping that 2017 will be a good year in some important ways, I’ve gathered 17 useful Unix tricks to make your hours on the command line a little more productive.

1: Oops!

For all the times we forget to insert “sudo” before a command that we want to run as root, the oops alias might come in handy.

alias oops='sudo !!'

This alias will rerun the previous command with “sudo” prepended to it. You can call it something other than “oops” if you like — whatever you’re likely to remember when you forget to use sudo.

2: Making files executable

A quick way to set up execute permissions for your scripts is to use an alias that is preset with the permissions you’re most likely to use. For example, you can easily create an alias to give execute permission across the board using an “ax” (all execute) like this:

alias ax="chmod a+x"

You can make the alias give execute permission to just the file owner and group, but you should probably then give it a different name.

alias ugx="chmod ug+x"

3: Reusing arguments from your previous command

You can save some time by reusing an argument from a previous command by using a placeholder instead. The expression $_, for example, represents the last argument from your previous command. While sequences like “vi /some/file/name” and “chmod 750 $_” might have some appeal (you could also reuse the command from your history and edit as needed), the same trick in an alias might be very convenient. Here’s an example:

$ alias 750='chmod 750 $_'
$ vi /some/file/name
$ 750
$ ls -l /some/file/name
-rwxr-x--- 1 justme mygroup 1234 Dec 22 /some/file/name

4: Checking listening processes

It’s always good to have a quick way to verify what processes are listening on your servers/ This alias makes that easy.

$ alias listen='netstat -l | grep "LISTEN "'

5: Examining most frequently used commands

Sometimes you can learn a lot by looking at which commands are being most heavily used by some account on a system you manage. Turn this command into a script or function and you’ll be able to easily generate a simple report.

history | awk '{print $2}' | sort | uniq -c | sort -nr | head

Here’s what you’d do to define the command as a function. Put this code into your .bashrc or /etc/bashrc file.

mostused() {
  history | awk '{print $2}' | sort | uniq -c | sort -nr | head
}

6: Extracting from archives

Here are some aliases that can simplify extracting files from archives. Use the one that corresponds to the extension of the archive you are extracting from.

alias tgz='tar -zxvf'
alias tbz='tar -jxvf'

7: Creating a new directory and jumping into it

You can set up a function to create a directory and cd into it with one command. In this example, we’re calling the function newdir. The second version below adds a pwd command to display your changed location in the file system.

function newdir () { mkdir -p "$@" && eval cd "$@"; }
function newdir () { mkdir -p "$@" && eval cd "$@" && pwd; }

8: Returning to the previous directory

Here’s an easy and fairly obvious way to go back to your previous directory. It uses the $OLDPWD setting to know where to send you.

alias return='cd "$OLDPWD"'

You can also bounce between two directories by using the cd – trick. And you’ll be reminded where you are sitting with each use of the command.

$ cd /var/log
$ cd /apps/maps/settings
$ cd -
/var/log
$ cd -
/apps/maps/settings
$ cd -
/var/log
$ cd -
/apps/maps/settings

9: Using aliases to help you remember important locations

Sometimes important file locations are buried many layers deep in your file system. You can create symbolic links to get to them, but creating an alias means you don’t have to be in some particular location first. Here’s an example.

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

10: Finding files with no known or current owners

If files are left on your system from people who no longer have accounts or were extracted from archives and don’t correspond to local users, you can find them using an alias like this one.

alias findorphans='find / -nouser -ls'

11: Finding recently changed files

An alias like the one below will report on the most recently modified files within the current directory.

$ find . -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -r | head -5
2016-12-22 13:28:06.5339703560 ./.bash_history
2016-12-22 12:51:08.7488158790 ./oddthoughts
2016-12-22 12:39:30.8013399550 ./bin/fixme
2016-12-22 12:34:38.3708657970 ./testingme
2016-12-15 18:25:47.5545172550 ./.viminfo

12: Finding really big files

Finding very large files

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

An alias like this can help you find very large files quickly, though you may have to adjust the size for your system. As written, this one will only find files that are larger than a megabyte in size.

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

13: Listing your aliases

Listing your aliases doesn’t even require an alias. Just type the word alias and you’ll get a nice listing.

$ alias
alias all='ls -al'
alias ax='chmod a+x'
alias c='clear'

14: Listing your functions

Listing your defined functions takes a little more effort, but not too much.

alias functions='set | grep "()"'

Here’s an example of using it.

$ functions
heavilyused ()
newdir ()

15: Backing up in the file system

Who wants to count dots? Using aliases like these, you can more easily select how many directories you want to back up into on your system.

alias up1='cd ..'
alias up2='cd ../..'
alias up3='cd ../../..'

16: Reviewing history a little at a time

Looking through history can be a bit overwhelming if you have a very large history buffer — say your last 1,000 commands — and want to find a command you used just yesterday. No problem if you combine the history and more commands.

alias hm="history | more"

17: Zapping a user

This command can be a little dangerous, but it will kill all the processes belonging to some particular user — if you have sufficient privilege to do so. You might want to use the sudo version.

The three aliases defined below show 1) the simple version, 2) the sudo version, and 3) the self-destruct version.

alias zap='pkill -U'
alias zap='sudo pkill -U'
alias selfdestruct='pkill -U `whoami`'

Happy 2017!

May it be a year that brings you joy even beyond the Unix command line.

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