Command Line Study Guide

This page is from the old wiki. It’s a guide for becoming more familiar with the command line. These are some basic GNU/Linux terminal commands that are useful to know as a minimum. Feel free to edit this post as a wiki page – just click the “edit” button.

Built-in Documentation

Most commands come with documentation in the form of “man[ual] pages”. To read the man page for a command use the man command.

Example: to read the documentation for the ls command, type man ls in a terminal. You can also search Google for information on any command.

List of Useful Commands

  • ls – list directory contents. Also ls -l, ls -R, and ls -al.
  • cd – change directory
  • cp – copy files
  • mv – move a file or directory. Also for renaming things
  • mkdir – make a directory
  • rm – remove a file. Also rm -rf, but very dangerous. See this story for a warning on how dangerous it can be.
  • rmdir — remove a directory
  • touch – create a new empty file
  • pushd – move to another directory with a bookmark (actually a stack of directories you’ve jumped from, so you can use it multiple times)
  • popd – jump back to the place where you pushd’d from
  • pwd – show current location
  • clear – clear the terminal. Also ctrl-l
  • less – display output with pagination
  • vim – type vimtutor and see the [[Vim]] page.
  • nano – simple console editor
  • cat – display a file and/or concatenate it
  • top – show processes. If you like that, install htop.
  • man – read the built-in documentation
  • locate – find files. E.g., sudo updatedb; locate *.desktop
  • find – find files. E.g., find / -name '*.desktop'
  • grep – search files and directories
  • tree – e.g., tree -d >> outputfile.txt. You may need to install it first.
  • tar, zip, gzip – manage archives
  • wc – count things: lines, bytes, characters, words, etc. Example: wc -l filename.txt will count the lines in a file.
  • tee – redirect the output to a file and the screen at the same time. E.g., ls -1 *.py | wc -l | tee count.txt which counts the number of Python files in your directory, writes it to the screen, and saves it to a file.
  • kill
  • ps
  • locate
  • find
  • shutdown
  • reboot
  • uptime
  • chmod
  • chown
  • du
  • df
  • head
  • tail
  • diff
  • date
  • df
  • sleep
  • which
  • apropos – can’t remember a command? Use this to find commands about a keyword, like: apropos wireless
  • ping
  • dig
  • traceroute

Also:

  • tab completion
  • pipes |, >, and >>
  • aliases
  • ctrl-r – reverse search
  • keyboard shortcuts: ctrl-u, ctrl-k, ctrl-a, ctrl-e, alt-f, alt-v, ctrl-d, alt-d (from Emacs)

For managing remote servers: ssh, scp, and rsync

And Tmux. (Also check out kitty terminal which has a similar splitting feature. It’s worth learning the basics of tmux in any case.)

Additional Utilities

Some of these may need to be installed.

  • sort – sorts items
  • uniq – gets only unique items
  • mc – Midnight Commander file browser
  • tr – translate
  • fold – wrap lines to a specified width
  • jq – tools for JSON
  • curl – do stuff with URLs
  • wget – download pages and sites
  • sql2csv (npm install -g sql2csv)
  • csvkit (pip install csvkit)
  • xml2json (git clone it and add to path)
  • ImageMagick – process and view images, e.g., display cat_pic.jpg, convert --resize 200x200 giant_hubble_photo.jpg hubble_photo_thumb.jpg
  • rename – bulk rename files with regular expressions. Example: rename all files with the extension .GIF to .gif: rename -v 's/\.GIF$/\.gif/' *.GIF
  • lynx – a browser in your terminal.

You will occasionally come across these:

  • sed – stream editor for filtering and transforming text
  • awk – pattern scanning and processing language
  • Perl one line scripts

See additional tools that you might want to investigate:

How to use the terminal for everything:

tig can be used as an alternative to git log. ranger is a file browser.

Keybindings

See this post for useful keyboard shortcuts:

Additional Resources

See also 7 command line tools for data science.

See also GNU Coreutils Manual.

There are additional posts here: command-line.

2 Likes

I will be using this often. Thanks! I will also try add some of the short cuts I have built. :slight_smile:

2 Likes

Here’s a site that might be useful. I’m not sure where I saw it originally, but it was in my tabs.
https://explainshell.com/

1 Like

Wow, that site is awesome. I love the design too! :slight_smile:

Not sure if this is exactly germane to this thread, but I thought it was interesting and helpful:
Understanding Bash: Elements of Programming

1 Like

Most recent favorite shortcut in Bash and similar shells

(ctrl-x ctrl-x) jumps to the beginning of the current input 
                repeat it to go back to where you were

    More specifically, it moves to a marker that defaults to start of the line 
    and you can use (ctrl-space) to set it to a new location
3 Likes

Here’s some additions to that list.

  • history - show a history of recently-invoked commands for this tty (then use !1234, for example, to re-run the earlier ID as seen)
  • touch - not only creates new files but can be used to change the timestamp of an existing one, useful for archival purposes or if working on caching code which looks for recent activity (or to age a file with touch -c -t 201901010000 example.txt)
  • echo - a humble program to say something to the terminal, usually in combination with something else like concatenating to an existing file
  • >, >> and 2> (redirection commands) - The first is used to send the standard output to a new file or to overwrite an existing one. The second as shown is used to create a new file or to append to an existing one. The third version shows how to additionally send the standard error output to a new file like in the first example.
  • | (pipe command) - Used to string together the output of the first command as the input to the next (as in history | grep ls)
  • sudo - Allows a temporary elevation to the root user from the current one, often used when running system-level commands like reboot
  • pkill - A friendlier version of kill if there’s only one instance running of a command or if you wish to kill all those so-named, for example, sudo pkill node
  • nslookup - A program to help troubleshoot DNS-related problems
  • ifconfig - A report of the network devices and their state
  • dmesg - A report of device-related logged messages
  • iwlist - A report of wi-fi zones as seen and related information (sometimes also iw)
2 Likes

A post was merged into an existing topic: CLI: improved (command line tools)

For people wanting to learn more about the command line, including complete beginners, I highly recommend the book The Linux Command Line. It’s available as a free pdf or in print from No Starch Press.

In my opinion, it’s an absolute gem of a book – extremely approachable and well laid out. I wish I had it 20 years ago! It is unapologetically Linux-focused though, so no guarantees which parts will carry over to OSX and other operating systems.

5 Likes