Stupid linux tricks

I’m a commandline jockey. I love the terminal and use it on a daily basis.
During my time as a sysadmin, devops engineer, software developer and security
researcher I picked up a lot of tricks which, although they might be documented,
they still made me go ‘Huh - that’s awesome’.

This post serves as a reference for myself, and I hope it’s useful for some of
you as well. The ‘tricks’ I mention in here are always documented in some man
page. But let’s be honest, those pages can be incredibly terse and compact some-
times. I prefer having a clear example of how something translates into daily
life on the command line. Hence the name: stupid linux tricks.

Control the commandline

No, literally. Use your <Ctrl> key more. Keep in mind that many terminal
emulators don’t map alt correctly, thus prevent some of the <Alt> key combinations!

Movement

  • To the beginning of a line: <Ctrl> + a
  • To the end of a line: <Ctrl> + e
  • Back one word: <Alt> + b
  • Forward one word: <Alt> + f

Delete stuff

  • Delete from the cursor to the beginning of the line: <Ctrl> + u
  • Delete from the cursor to the end of the line: <Ctrl> + k
  • Undo a delete: <Ctrl> + y

Never type ‘clear’ again

Just use <Ctrl> + l. It does the same, and is faster to use.

Vim, in bash

Put this in your .bashrc: set -o vi

By default, you’ll be in ‘insert’ mode. But hit escape, and you can use the
familiar vim movements like ‘I’ to insert at the beginning of the line or
‘A’ to append at the end or ‘ea’ to append at the end of a word.

Keep in mind that the aforementioned ‘moving around’ with the control keys
doesn’t work in vi-mode.

And a little bonus-tip: I just have to have my <Ctrl> + l clearscreen, even
though I’m in vi-mode. So i added this to my .bashrc:

1
bind -m vi-insert "\C-l":clear-screen

Easy cp/mv backups

Use this little trick in bash (note the leading comma):

1
2
3
4
$ cp myfile{,_old}
$ ls
myfile
myfile_old

The way this works is as follows:

1
2
3
4
$ echo a{b,c}
ab ac
$ echo a{,b}
a ab

So mv file{,_backup} gets expanded to mv file file_backup. Cool huh?

Sudo previous command

In bash, !! expands to the complete last command you typed.

1
2
3
4
$ make sandwich
Cannot 'make sandwich' (are you root?)
# sudo !!
Making sandwich..

Don’t record command

Prepend your command with a space. This will prevent what you’re typing from
showing up in the history. Nice to know for when you’re starting things
which include a password on your commandline.

Reuse long arguments

Useful for when you’re doing things like making directories and cd’ing into
them. Or chmodding files and then opening them in vim. I’m sure you can find
some use for this.

The thing to remember here is !$ pulls from the history, and will thus take
the last argument from the entire line, including things you piped.

1
2
3
4
$ wc -l /var/log/app/foo/thing.txt > count.txt
$ echo !$
echo count.txt
count.txt

Whereas $_ will pull the arguments from the first bash command you gave, but won’t
included piped commands:

1
2
3
4
$ wc -l /var/log/app/foo/thing.txt > count.txt
$ echo $_
echo /var/log/app/foo/thing.txt
/var/log/app/foo/thing.txt

Find strings in things

Strings finds strings (sequences of 4 or more readable characters) inside files.
Yes, that also means in binary files. So if you’re going through, say, an
unpacked android app for instance - try this from the unpacked app’s root
directory:

1
2
find . -type f -print0 | xargs -0 strings | grep -E 'https?://' | tee
../url-like.txt

Feel free to upgrade your grep-game there, since this will probably generate a
lot of noise as well.

Grep and sed

Since almost everything is a string, we have some very powerful tools at our
disposal on the commandline. We can use grep to filter through heaps of data and find lines in
files we’re interested in. We can also use sed to edit those strings.

Here are some useful presets I use on a day to day basis:

Recursively find lines which have ‘http://‘ or ‘https://‘ in them in current-
and all child directories:

1
grep -nHriP 'https?:\/\/' .

Extract all urls from href tags on a page:

1
sed -n 's/.*href="\([^"]*\).*/\1/p' my_filename

Remove all trailing whitespaces from a file:

1
sed 's/[ \t]*$//'

Center all the lines in a 79-column layout. Leading and trailing spaces will be
discarded (90’s style textfile!):

1
sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'

Find my pattern, and show me 3 lines above and below it as well:

1
grep -A3 -B3 "mysearchterm" /var/log/foofile

Bonus: grep within a .gz file:

1
zgrep 'failed' /var/log/auth.log.1.gz

External services

Get your current external ip in the terminal:

1
curl ifconfig.me

Get the current weather for {place} in the terminal:

1
curl http://wttr.in/{place}