Linux command line basics: moving around, copying, deleting safely and permissions
TUREFF• •0
The terminal looks intimidating because it gives you nothing to click, but day-to-day use comes down to a couple of dozen commands. Once you know how to move around, look at files, copy and remove things without regret, and install software, most tutorials and error messages start to make sense. Everything below works on almost every distribution, and most of it on macOS too.
Where am I, and what is here?
- pwd prints the folder you are in (the "working directory").
- ls lists what is in it. Add the option -l for a long listing with sizes, dates and permissions, and -a to include hidden files, whose names start with a dot. Most people type them together as ls -la.
- cd followed by a folder name moves into it. cd with two dots goes up one level, cd on its own goes back to your home folder, and cd followed by a dash returns to the previous folder.
Paths starting with a slash are absolute, measured from the top of the system; anything else is relative to where you are. The tilde is shorthand for your home folder. Press Tab while typing a name and the shell completes it for you.
Looking at files
- cat prints a short file to the screen.
- less opens a long one so you can scroll; press q to quit and a slash to search.
- head and tail show the first or last ten lines. tail with the option -f keeps following a log file as it grows.
Creating, copying and moving
- mkdir makes a folder; with -p it creates any missing parent folders as well.
- touch creates an empty file (or updates the date on an existing one).
- cp copies: cp, then the source, then the destination. To copy a whole folder add -r (recursive).
- mv moves, and renaming is just moving to a new name in the same place.
Both cp and mv will silently overwrite a file that already exists at the destination. Adding -i makes them ask first, which is a good habit while you are learning.
Deleting without disaster
rm removes files and rm -r removes folders with everything in them. There is no recycle bin on the command line: what rm deletes is gone. A few habits keep you safe:
- Run ls with the same name or pattern first, so you see exactly what will be matched.
- Use rm -i, which asks before each file, until you are confident.
- Be very careful with the asterisk wildcard, and never paste a deletion command from the internet that you do not understand. A stray space in a path can turn "delete this folder" into "delete everything here".
Permissions in plain terms
Every file belongs to a user and a group, and has three sets of permissions: for the owner, for the group, and for everyone else. Each set says whether that person may read (r), write (w) or execute (x) it. In the ls -l listing you will see a string such as rwxr-xr-- : the owner can do everything, the group can read and run it, others can only read.
- chmod changes permissions. chmod u+x followed by a file name makes a script runnable by you.
- chown changes the owner, and usually needs administrator rights.
If you get "Permission denied", ask whether you should be touching that file before reaching for sudo.
sudo: borrowing administrator rights
Typing sudo in front of a command runs it as the administrator (root), after asking for your password. You need it to install software, edit system settings and change files outside your home folder. You do not need it for your own files, and using it there can leave files owned by root that you then cannot edit. Treat every sudo as a moment to double-check what you typed.
Installing software
Linux distributions install software from trusted repositories through a package manager, rather than from downloads on websites:
- Debian, Ubuntu and Mint use apt: sudo apt update refreshes the list, sudo apt upgrade updates everything, and sudo apt install followed by a name installs a program.
- Fedora uses dnf, with similar install and upgrade commands.
- Arch uses pacman.
Many distributions also offer Flatpak or Snap for desktop apps.
Reading the manual
- man followed by a command name opens its manual page; read it with the same keys as less.
- Most commands also print a short summary with the option --help.
- When a command fails, read the error message slowly. It usually names the file and the problem.
Practise in a spare folder of throwaway files, and the terminal soon stops feeling dangerous. Which command confused you most when you started, or which one do you wish you had learned sooner? Share it in the comments.