Command Line Study Guide
This is 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.
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.
If the man pages are too overwheming, there is also a simpler tool called tldr.
List of Useful Commands
Click on the name of a command for more information.
Basics
clear-- clear the terminal. Also ctrl-l
Documentation
man-- read the built-in documentationtldr-- shorter, simpler documentation for commands
Where Am I?
pwd-- show your current location in the filesystemcd-- change directoryls-- list directory contents. Alsols -l,ls -R, andls -al.tree-- list a directory as a tree. e.g.,tree -d >> outputfile.txt. You may need to install it first.
An alternative way to move around and keep track of where you've been is to use dirs.
dirs-- a stack of recently visited directoriespushd-- 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
Manipulating Files
cp-- copy filesmv-- move a file or directory. Also for renaming thingsmkdir-- make a directoryrm-- remove a file. Alsorm -rf, but very dangerous. See this story for a warning on how dangerous it can be.rmdir--- remove a directorytouch-- create a new empty file
Finding Files
grep-- search the contents of filesrg-- ripgreplocate-- find filesfind-- find files. E.g.,find / -name '*.desktop'fd-- a find command with some improvements
Programs
which-- tells you where a program is locatedapropos-- can't remember a command? Use this to find commands about a keyword, like:apropos wireless
Networking
pingdigtraceroute
Reading and Editing Files
less-- display output with paginationvim-- typevimtutorand see the [[Vim]] page.nano-- simple console editorcat-- display a file and/or concatenate it.bat-- likecatbut with syntax highlighting and other improvementstee-- redirect the output to a file and the screen at the same time. E.g.,ls -1 *.py | wc -l | tee count.txtwhich counts the number of Python files in your directory, writes it to the screen, and saves it to a file.wc-- count things: lines, bytes, characters, words, etc. Example:wc -l filename.txtwill count the lines in a file.head-- view the first lines of a filetail-- view the last lines of a filediff-- compare two different files
Users, Groups, and Permissions
chmodchown
The System
top-- show processes. If you like that, installhtop.htopdudfkillpsshutdownrebootuptimedatesleep
Compressing and Extracting Files
tarzipgzip
Other Tips
- 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.
Additional Utilities
Some of these may need to be installed.
sort-- sorts itemsuniq-- gets only unique itemsmc-- Midnight Commander file browsertr-- translatefold-- wrap lines to a specified widthjq-- tools for JSONcurl-- do stuff with URLswget-- download pages and sitessql2csv(npm install -g sql2csv)csvkit(pip install csvkit)- xml2json (
git cloneit 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.GIFto.gif:rename -v 's/\.GIF$/\.gif/' *.GIFlynx-- a browser in your terminal.
You will occasionally come across these:
sed-- stream editor for filtering and transforming textawk-- pattern scanning and processing language- Perl one line scripts
See additional tools that you might want to investigate:
https://forum.codeselfstudy.com/t/cli-improved-command-line-tools/1001
How to use the terminal for everything:
https://forum.codeselfstudy.com/t/using-the-terminal-for-everything/1005
tig can be used as an alternative to git log. ranger is a file browser.
https://forum.codeselfstudy.com/t/interesting-command-line-tools/112
Keybindings
See this post for useful keyboard shortcuts:
https://forum.codeselfstudy.com/t/becoming-faster-with-the-command-line/990
Additional Resources
See also 7 command line tools for data science.
See also GNU Coreutils Manual.
There are additional posts here: #command-line.