Quick tips -> Using the Linux Command line

One of my favorite things about using the Linux command line for any applicable tasks is that there’s usually at least one way to simplify a long list of operations down to just a few keystrokes. The tips below, some of which are major time-savers, are things I learned from friends, the internet, or discovered by accident.

Moar Dots

Most of us learn to exit out of file directories by repeatedly typing “cd ..” into one prompt after another. Eventually we graduate to using a sequence of dots and slashes eg. “../../../../” at the prompt. But even this method is not lazy enough, so I stumbled on a quicker way which involves typing any number of periods(eg. cd .... into the console to travel up the same number of directories. This trick won’t work out of the box on certain Linux distrubutions and shells, but the aliases below suggested by Max Hoffman work great for acheiving consistent, “more dots” behaviour across all shells.
 
#travel up through directories, one more level for each additional period
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."
alias .......="cd ../../../../../.."
	
Check out the full article for more great tips:
https://mhoffman.github.io/2015/05/21/how-to-navigate-directories-with-the-shell.html

$OLDPWD

The $OLDPWD variable always evaluates to the path of the previously visited direcotory, which makes it a convenient way to hop back-and-forth between any two directories.

Color Always

When text output from commands like ls -la is redirected or piped to pagers like less, any text formatting color is often lost. Some Linux programs provide a --color flag. Set --color=always to keep terminal color codes which can then be interperated by programs like “less -R” to view the colors.

Cleaner output with ls -1

ls -l gives a detailed list of files and directories, which sometimes is more information than desired. When you just want newline seperated filenames to pipe to another program, like sort, use the -1 ( minus one ) flag ls -1.

Use Explain Shell

There’s an awesome online tool for deciphering shell commands called Explain Shell. It’s saved me loads of time and is great for tracking down explanations for before running them commands. When in a hurry , I use Explain Shell to get the jist of what in unfamiliar command does, without having to dig through the man pages. Many man pages and info sections are massive tomes of information and are great references for unfamiliar programs, but slower to skim for the important bits. Over the years I’ve spent as web admin, I’ve encountered many roadblocks to proper development of the site or implementation of a feature. Sometimes bugs, and at other times incompatibilities, require that I borrow solutions from online forums or develper blogs. In those cases, I feel responsible for undstanding any copied commands well before attempting to run them.

Set $MANPAGER To Vim

Some program manuals contain references to so many other programs, that reading through them feels like getting lost in an endless system of ant tunnels. Quickly jump between man pages with Vim’s CTRL-K command which works by passing any text under the cursor as an argument to man eg. man text_under_vim_cursor .Vim’s man-page browsing feature comes in handy, for anyone who insists on mannually going down the rabbit hole.😉

Crawl manuals with man’s -K flag

Most Linux users will be aware of the lowercase -k flag for finding programs by keywords. This search is fast but only checks program descriptions, instead of the full text of a program’s manual files. For a more thorough search, use man -K the_search_term and use CTRL-D to skip irrelevant results. The downside to this is the search could easily take a couple minutes to complete. Also many irrelevant results will be returned during the search, each time, and for each match , the search will stop to wait for action from the user before continuing.