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
- 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.
- List a folder’s contents with
ls(list):
ls
- List with details (permissions, owner, size, date) using
ls -l:
ls -l
- Also show hidden files with
ls -a. Hidden files start with a dot, for example.bashrc:
ls -a
- Combine both, details plus hidden files:
ls -la
Moving between folders
- Move to another folder with
cd(change directory):
cd Documents
- Move one level up with
cd ... Two dots mean the parent folder:
cd ..
- Go straight to your home folder with
cd. Thesign means your home folder:
cd ~
- Move to a folder using its full address:
cd ~/Documents/Projects
- Check your position again with
pwdafter moving.
Creating files and folders
- Create a new folder with
mkdir(make directory):
mkdir Projects
- Create nested folders at once with
mkdir -p. Without-p, the parent folder must already exist:
mkdir -p ~/Documents/Projects/website
- Create an empty file with
touch:
touch notes.txt
- Create several empty files at once:
touch one.txt two.txt three.txt
Copying, moving, and deleting
- Copy a file with
cp(copy). Write the source file first, then the destination:
cp notes.txt notes-copy.txt
- Copy a file into another folder:
cp notes.txt ~/Documents/
- Copy a folder with all its contents using
cp -r. The-rflag means recursive: copy everything inside it too. Without-r, a folder cannot be copied:
cp -r Projects Projects-copy
- Move a file to another folder with
mv(move):
mv notes.txt ~/Documents/
- Rename a file with
mvtoo. Moving and renaming are the same command:
mv old.txt new.txt
- Delete a file with
rm(remove):
rm notes.txt
- Delete a folder with all its contents using
rm -r:
rm -r old-folder
WARNING:
rm -rfis destructive and CANNOT be undone. The-fflag means force: delete without asking first. Files deleted byrmare gone from the disk permanently. They do not go to the trash. There is no undo button. Before deleting, check the folder’s contents withlsfirst. Never runrm -rf /orrm -rf ~: that deletes the contents of your whole system or your whole home folder.
Reading a file’s contents
- Read a file with
cat(concatenate: join and show the contents):
cat notes.txt
- For a long file,
catscrolls past the screen. Uselessto scroll slowly instead. Pressqto quit:
less notes.txt
Running as admin with sudo
- 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. Runsudoonly for commands you understand. Do not pastesudocommands 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.
- 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–.
- 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
- 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. Permission777(everyone may do everything) is dangerous. Do not use it for important files.
Asking for help
- Every command has built-in help. Add
–helpafter the command:
ls --help
- Read a command’s full manual with
man(manual). Pressqto quit:
man ls
- 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.
Comments