A handy list of terminal commands for beginners

A handy list of terminal commands for beginners

I'm in the middle of getting to grips with using the command line so I've written a list of handy commands, that I can refer back to. If it is useful to you too, please feel free to use it!

Basic commands

n.b directory is just another word for folder - I use them interchangeably

pwd tells you which directory you are currently in (stands for 'print working directory'). By default, when you open the terminal, you will be in the home directory

ls lists all the files and folders in the directory you are currently in. You can add 'switches' to get you slightly different results:

  • ls -a lists all the files and folders in the current directory, including hidden ones
  • ls -l lists all files and folders in the current directory in a long format, which shows you permissions, ownership, size and modification date
  • ls -t lists all files and folders in the current directory, in order of the time they were last modified
  • ls -alt combines all three commands (you can add as many or as few 'switches' as you need)

cd change directory. Here are some more ways to use this command:

  • cd projects/example-project in this example, this file path will take you from the current directory, down into the projects folder and down again into the example-project folder
  • cd .. takes you up a folder level
  • cd ../.. takes you up two folder levels (you can add as many '/..' as you need to get up to the level you want)
  • cd ~ takes you back to your home directory from wherever you are
  • cd ~/projects takes you back up to your home directory from wherever you are then down into a folder (in this case a folder called projects)

mkdir creates a new folder (think of it as 'make directory')

touch creates a new file

echo prints a string e.g echo "Hello World" will print Hello World on the Terminal screen

open opens a file using the most appropriate programme on the computer e.g open hello.txt will open the file using TextEdit (on a Mac)

cp copies files or directories. The syntax is as follows:

cp folder-name-you-want-to-copy/filename-you-want-to-copy.txt folder-name-to-paste-to

mv this has two main uses:

  • move files from one location to another. The syntax is as follows:

    mv file1-to-be-moved.txt file2-to-be-moved.html destination-folder/destination-subfolder

  • rename files. The syntax is as follows:

    mv old-filename.txt new-filename.txt

rm deletes files and directories (think of it as 'remove' file or directory)

rm -r deletes a directory and all of the directories and files within it CAUTION: This can't be reversed so use with care

rm -i deletes files and directories but will ask you to confirm each deletion first (think of it as 'interactive')

rmdir deletes an empty directory

cat outputs the contents of a file (prints it on the Terminal screen)

less previews the contents of a file, without leaving it permanently on your terminal screen (press q to stop viewing file)

head outputs the first 10 lines of a file

tail outputs the last 10 lines of a file

wc gives you the word count (and the line and character count) of a file, in the order lines, words, characters. Use -l -w or -c after wc to see just that count.

Streams and redirection

Streams

Before looking at the rest of the commands, it is helpful to understand something called 'standard streams'. There are three of them: standard input (stdin), standard output (stdout) and standard error (stderr):

  • Standard input (stdin) is info inputted into the terminal using the keyboard, such as a command e.g. cat index.html

  • Standard output (stdout) is the info outputted after that command e.g. the contents of the index.html file is printed to the screen in Terminal

  • Standard error (stderr) is an error message outputted when a process fails for whatever reason

Redirection

Using these streams, we can redirect inputs and outputs, into another command for example:

echo "Hello" prints Hello on the screen. This is its output. We can take that output and redirect it to do something useful, like this:

echo "Hello" > hello.txt This takes the output and puts it in a new file called hello.txt. If we were to open that file, we would see the text Hello at the top of it.

> will redirect the standard output to wherever/whatever you put after the > e.g. cat latest-sales-figures.txt > sales-figures.txt will either create a new file called sales-figures.txt containing the info from latest-sales-figures.txt OR, if the files exists already, it will overwrite the contents of sales-figures.txt

>> Does the same as >, but it will append instead of overwriting the contents of the file

| this is called the 'pipe'. It takes the output of the command on the left and 'pipes' or inputs it to the command on the right. e.g.

cat chapter1.txt | wc | cat > chapter1-word-count.txt - in this example, the output of the first bit (the contents of the chapter1 file) is piped across as the input for wc, the output of which (the word count numbers) is then piped across to cat to be put into a new document called chapter1-word-count.txt. If you opened chapter1-word-count.txt, you would see it contains the 3 word count numbers as its content.

sort - takes an input and orders it alphabetically

uniq filters out adjacent, duplicate lines in a file. Because it only works on adjacent lines, it is useful to sort first

grep - a search tool. It searches files for lines that matches the pattern you specify and returns those lines. e.g grep "Jennifer" student-names.txt will return all the lines containing Jennifer in the file. It is case-sensitive. Here are some ways of modifying grep with switches:

  • grep -i a case-insensitive version of the above

  • grep -R searches all files in a directory and returns filenames plus the lines in question

  • grep -Rl as above, but only returns filenames

sed the 'Find & Replace' of Terminal. The syntax is as follows:

sed 's/text-to-find/text-to-replace-it-with/' filename.txt

s stands for substitution and it is always used with sed

N.b this command will only replace the first instance in a line; to replace all instances in a line, use g at the end (it stands for global):

sed 's/text-to-find/text-to-replace-it-with/g' filename.txt

find finds all files in the specified directories, including all its sub-directories (aka 'recursively'). It is a more powerful version of ls. e.g

find ~/projects -name "*.png" > my-pngs.txt will search my projects folder (and all its sub-folders) for png files and will create a new file called my-pngs.txt which will contain a list of those png files

man built-in help pages. You can type man before any command to open the help page for that command, which gives you an overview of how to use it e.g. man pwd.

An even better version of man is tldr, which you have to install on your machine (the link takes you to the tldr pages GitHub repository - open the README file to get started).