The terminal is a text window for giving commands to your computer. Type a command, then press Enter. This tutorial covers the most basic commands: viewing folders, moving between folders, creating files, copying, deleting, reading files, and setting permissions. Permissions are the access rules for a file or folder. All commands here work on Arch Linux.

Viewing: where you are and what is inside

  1. See your current folder with pwd (print working directory):
pwd

The result looks like /home/briyan. That is the address of the folder you are in.

  1. List a folder’s contents with ls (list):
ls
  1. List with details (permissions, owner, size, date) using ls -l:
ls -l
  1. Also show hidden files with ls -a. Hidden files start with a dot, for example .bashrc:
ls -a
  1. Combine both, details plus hidden files:
ls -la

Folder tree and terminal navigation

Moving between folders

  1. Move to another folder with cd (change directory):
cd Documents
  1. Move one level up with cd ... Two dots mean the parent folder:
cd ..
  1. Go straight to your home folder with cd . The sign means your home folder:
cd ~
  1. Move to a folder using its full address:
cd ~/Documents/Projects
  1. Check your position again with pwd after moving.

Creating files and folders

  1. Create a new folder with mkdir (make directory):
mkdir Projects
  1. Create nested folders at once with mkdir -p. Without -p, the parent folder must already exist:
mkdir -p ~/Documents/Projects/website
  1. Create an empty file with touch:
touch notes.txt
  1. Create several empty files at once:
touch one.txt two.txt three.txt

Copying, moving, and deleting

  1. Copy a file with cp (copy). Write the source file first, then the destination:
cp notes.txt notes-copy.txt
  1. Copy a file into another folder:
cp notes.txt ~/Documents/
  1. Copy a folder with all its contents using cp -r. The -r flag means recursive: copy everything inside it too. Without -r, a folder cannot be copied:
cp -r Projects Projects-copy
  1. Move a file to another folder with mv (move):
mv notes.txt ~/Documents/
  1. Rename a file with mv too. Moving and renaming are the same command:
mv old.txt new.txt
  1. Delete a file with rm (remove):
rm notes.txt
  1. Delete a folder with all its contents using rm -r:
rm -r old-folder

Warning about rm -rf: permanent deletion, no trash

WARNING: rm -rf is destructive and CANNOT be undone. The -f flag means force: delete without asking first. Files deleted by rm are gone from the disk permanently. They do not go to the trash. There is no undo button. Before deleting, check the folder’s contents with ls first. Never run rm -rf / or rm -rf ~: that deletes the contents of your whole system or your whole home folder.

Reading a file’s contents

  1. Read a file with cat (concatenate: join and show the contents):
cat notes.txt
  1. For a long file, cat scrolls past the screen. Use less to scroll slowly instead. Press q to quit:
less notes.txt

Running as admin with sudo

  1. Some commands need admin rights, for example changing system files. Add sudo (superuser do: run as admin) in front of the command:
sudo pacman -Syu

The command above updates all packages on Arch Linux. You will be asked for your password.

Be careful with sudo. Admin commands can change or break your system. Run sudo only for commands you understand. Do not paste sudo commands from the internet without reading what they do first.

Permissions: who may do what

Every file has three permissions: r (read), w (write), and x (execute, run). These apply to three groups: the file’s owner, the group, and everyone else.

  1. See a file’s permissions with ls -l:
ls -l notes.txt

The result looks like -rw-r–r–. The nine letters after the - are three groups of three letters: owner rw-, group r–, others r–.

  1. Change permissions with chmod (change mode). There are two ways: numbers and letters.

The number way: each letter has a value. r = 4, w = 2, x = 1. Add the values inside one group. For example rwx = 4 + 2 + 1 = 7. r-x = 4 + 1 = 5. rw- = 4 + 2 = 6.

The most common numbers:

  • 755 = rwxr-xr-x. The owner may do everything, the group and others may read and run. Good for folders and scripts.
  • 644 = rw-r–r–. The owner may read and write, the group and others may only read. Good for regular files.
chmod 755 script.sh
chmod 644 notes.txt

Diagram of rwx permissions, owner, group, others, and the numbers 755 and 644

  1. The letter way: add or remove one permission without changing the rest. For example, give the owner permission to run a file:
chmod u+x script.sh

The letter u means the owner (user), g means the group, o means others, a means all. The + sign adds a permission, - removes one, = sets it exactly.

Be careful with a wrong chmod. Permissions that are too strict can lock you out of your own file, so you cannot read or run it. Permission 777 (everyone may do everything) is dangerous. Do not use it for important files.

Asking for help

  1. Every command has built-in help. Add –help after the command:
ls --help
  1. Read a command’s full manual with man (manual). Press q to quit:
man ls
  1. Find a command whose name you forgot with apropos. For example, find commands related to copying:
apropos copy

Command summary

Command Function
pwd Show the current folder
ls, ls -l, ls -a List a folder’s contents
cd, cd .., cd ~ Move between folders
mkdir, mkdir -p Create a folder
touch Create an empty file
cp, cp -r Copy a file or folder
mv Move or rename
rm, rm -r Delete a file or folder
cat Read a file’s contents
sudo Run as admin
chmod Change permissions
man, –help Ask for help

Practice: create a folder called ~/terminal-practice, fill it with three empty files, copy one file to another folder, then delete the practice folder. If every step runs without an error, you know the basic terminal commands.