Get more work done in less time and keystrokes, using the command line interface on Linux, Mac, & other UNIX-ish systems.
Switch directories with”$OLDPWD” or “-”
The $OLDPWD
variable evaluates to the path of the previously visited directory. The hyphen(-) symbol as shorthand on most shells.
Both are quick way to hop back-and-forth between any two directories.
mannycrafts@devunstuck:/opt/bitnami/wordpress$ cd wp-admin/
mannycrafts@devunstuck:/opt/bitnami/wordpress/wp-admin$ cd -
/opt/bitnami/wordpress
mannycrafts@devunstuck:/opt/bitnami/wordpress$ echo $OLDPWD
/opt/bitnami/wordpress/wp-admin
mannycrafts@devunstuck:/opt/bitnami/wordpress$ cd $OLDPWD
mannycrafts@devunstuck:/opt/bitnami/wordpress/wp-admin$ pwd
/opt/bitnami/wordpress/wp-admin
Moar Dots
Most of us learn to exit out of file directories by typing cd ..
into one prompt after another. Eventually we might graduate to using sequences of dots and slashes eg. ../../../..
at the prompt. But even this method is not lazy enough..
#travel up through directories, one level per up period
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."
alias .......="cd ../../../../../.."
This trick won’t work out of the box on certain Linux distributions and shells. See Max Hoffman‘s guide on making this more broadly compatible:
https://mhoffman.github.io/2015/05/21/how-to-navigate-directories-with-the-shell.html
Keep colors on redirected output
Text color is often lost when output for commands like ls -la
is redirected or piped to pagers( e.g. less).
Set --color="always"
for ls
or grep
to keep terminal color codes.
mannycrafts@devunstuck:/opt/bitnami/wordpress$ ls -la --color=always | less -R
Other programs (e.g., pod2text
) accept a plain --color
flag with no additional parameters.
Neat filename output with ls -1
ls -l
( lowercase L ) will output a list of files with several other details, but sometimes it’s more information than desired( e.g. when piping files as input ).
To list just the filenames, use the -1
( number one ) flag.
mannycrafts@devunstuck:/opt/bitnami/wordpress$ ls -1
admin-wp
index.php
licenses
license.txt
mannycrafts-wp.log
readme.html
tmp
wp-activate.php
wp-admin
wp-blog-header.php
Use Explain Shell
Use Explain Shell web app to decipher unfamiliar commands and options without having to dig through the man pages for each flag.
Set $MANPAGER To Vim
Vim natively man page awareness comes in handy, When you need to go down the rabbit hole…or man hole 💩 for any programs or tools.
The man
pages for most programs are littered with references to other executables. The risk of losing your precious train of thought is highest while opening and closing many different man pages while doing this sort of research.
To set vim as your $MANPAGER run the following command( or place in ~/.bash_profile ) :
export MANPAGER="/bin/sh -c \"col -b | vim -c 'set ft=man ts=8 nomod nolist nonu noma' -\""
Now you can quickly jump to any manpage listed under the cursor, using the CTRL-K command
Use Vim’s CTRL-O, CTRL-I key combo to jump traverse back through visited man pages.
Full-text search man pages
Most Linux users will be aware of the -k flag for finding programs by keywords. This search is fast but only checks program descriptions.
For a more thorough search, use man -K thesearchterm
and browse through results using CTRL-D to skip irrelevant ones, till the one you are searching for turns up.
It could take a few minutes for the full-text search of all available man pages to complete.
$ man convert
No manual entry for convert
$ man -K convert
--Man-- next: rst-buildhtml(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: rst2html(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: rst2html4(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: rst2html5(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: rst2latex(1) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
0 responses to “Shell & CLI: Time Saving Tips”