DEV Community

Prasanna Natarajan
Prasanna Natarajan

Posted on • Originally published at npras.in

Tips to save time with alias command (not what you think)

Tuesdays are not as cool as Mondays or Fridays. But it's never shabby for productivity tidbits. Here's two for today. The alias shell command has saved so many fingers over the years. I figured 2 tips that could save even more.

Tip 1: Echo the command first

I have murder as an alias for kill -9. So when I have to kill any process, I find it's id (let's say it's 2345) and then just run murder 2345.

It's cool. I've been using it for years now. But over time, I might forget the actual command itself. It's important though. If you are fiddling with a server or a docker container you won't have access to your aliases and your time-saving habit will cost time there. Instead I define my alias like this:

alias murder="echo 'cmd: kill -9 ' && kill -9 "

Now, when I run the murder command, it first echoes the actual command and then does the killing.

$ murder 62210
cmd: kill -9

I'll spend at least half a second to read the output which will make me glance at the actual command. It might come in handy some day!

Here are more examples:

# Shell command to run a simple http server to serve current directory's
# files.
$ serve
cmd: ruby -run -e httpd . -p 3000
[2019-09-10 10:15:33] INFO  WEBrick 1.4.2
[2019-09-10 10:15:33] INFO  ruby 2.6.3 (2019-04-16) [x86_64-darwin16]
[2019-09-10 10:15:33] INFO  WEBrick::HTTPServer#start: pid=62753 port=3000

As you can see, the first output line is the actual command. The alias is setup as:

alias serve="echo 'cmd: ruby -run -e httpd . -p 3000' && ruby -run -e httpd . -p 3000"

All you need is the echo command and the && operator to define it like so.

You can even put this in a shell function. Here's an example:

psgrep() {
  echo 'cmd: ps -ef | grep -i '
  ps -ef | grep -i $1
}

Running psgrep would show the matching line-items from the ps command's output.

$ psgrep irb
cmd: ps -ef | grep -i
  501 62214 60386   0 10:11AM ttys003    0:00.00 grep -i irb
  501 62210 61813   0 10:11AM ttys004    0:00.36 irb

Tip 2: Create alias for aliases

Certain commands can be grouped. For example there are many disk-space related commands. du -sh lists the size of the current directory. df -h lists the disk-space of the whole system. du -sh * | sort -k1,1rn | head sorts the current directories contents by size. We can create aliases for these so that we don't have to remember the complex flags and pipes. But what if we forget the aliases themselves?

You can simply type alias which will print out all the aliases you've defined, and then maybe you can grep just the items you want. But it's tedious and you still have to remember certain parts of what you are looking for.

There's a better way. Create an alias that just echoes these aliases with a short description of what they do. You can group them by functionality. For example, in the case of the commands above, they are all about calculating the disk-size. So I defined an easy-to-remember alias called size. It's output is:

$ size
dush - current dir size
dirszsort - sort dir contents by size
disksz - disk size

Now I'll know which of these I need at the moment and just run it.

$ disksz
cmd: df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/disk1      465G  368G   97G  80% /
/dev/disk2      2.0M  140K  1.9M   7% /private/var/folders/jn/bnyd1y_503xgka61g50ygf740000te/T/NVProtectedEditingSpace

The size alias is defined as:

alias size="echo 'dush - current dir size' &&
            echo 'dirszsort - sort dir contents by size' &&
            echo 'disksz - disk size'"

Now, could someone please create a repo with these nice group aliases so all can contribute and/or steal?

Top comments (0)