Speed up your command line navigation (Part 2)

Photo by Markus Spiske from Pexels

Increased speed and accuracy with a Linux command line can lead you to be hugely more efficient and productive than you once were. This article is a continuation of the hugely popular first article; Speed up your command line navigation (Part 1).

autocd and cdspell —lazily move between directories

Instead of having to type cd usr , cd bin , etc. You can literally just type “usr”, and “bin”, any any other directory name that exists. This is called autocd . Enable it like this;

user@host: shopt -s autocd

Then, if you would normally cd bin, you can simply type bin(you don’t need cd anymore!).

user@host: cd / ; ls
bin sbin var ...
user:host: bin
cd -- bin # Now we're in the bin directory

There is also cdspell, which will automatically fix obvious typos. Unfortunately it doens’t seem to work with autocd.

user@host: shopt -s cdspell
user@host: cd sbiin
sbin

You can see sbiin is automatically corrected to sbin .

Copy a file to create a backup;

user@host: cp myfile.txt{,.bak}

This will copy myservice.cfg to myservice.cfg.bak. This works because the { and }, which is called shell expansion, will basically automatically convert that command you typed into this;

user@host: cp myfile.txt myfile.txt.bak

Jumping to a specific line when opening a file with vim

If you’re anything like me, several times a day you’ll get error messages at “line X in file Y”. Rather than having to open the file and press “down down down” until you get to the line, you can specify it on the command line when opening a file with vim;

user@host: vim service.log +7

This will open service.log at line 7. This saves me a lot of time during the day.

This only works with vim unfortunately, not with less, or other tools.

Clear terminal — Ctrl + L

I find myself doing this basically after every command. This relies on your terminal supporting it of course as well. It’s a great way to reduce visual clutter after a command with lots of output.

Learn the `find` command

Find a file with the word “foo” in it;

user@host: find -iname '*foo*' 

Find empty files and empty directories;

user@host: find -empty 

Find files with the .bak extension and delete them (careful!);

user@host: find -iname '*.bak' -delete

Find files only, not directories;

user@host: find -type f -iname '*cake*'

Find files over 100mb;

user@host: find -size +100M

Find files over 100mb which haven’t been accessed in 7 days (careful, not all filesystems store/update atime — access time. You might be better using mtime — modified time).

user@host: find -size +100M -atime -7

Find files over 100mb which haven’t been accessed in 7 days and delete them (be careful!)

user@host: find -size +100M -atime -7 -delete

Find files over 100mb which haven’t been access in 7 days and save that list to a file;

user@host: find -size +100m -atime -7 > largeOldFiles.txt

--

--

James Read
James Read’s Code, Containers and Cloud blog

Public Cloud and Open Source advocate. Red Hat Solution Architect during the day. Enthusiastic developer at night :) http://jread.com