You are logged in to your server, facing the prompt, and the first thing to master is finding your way around the tree and handling files. Creating a directory, copying a configuration file, moving a backup, deleting a temporary file: these moves come back dozens of times a day in Linux administration. This guide shows you the essential commands to do them. If the terminal or the prompt mean nothing to you yet, start with Open a terminal and understand the prompt.
What you will learn
- Locate yourself in the tree with
pwd - Move between directories with
cd - List files and directories with
lsand its key options - Create files (
touch) and directories (mkdir) - Copy (
cp), move (mv) and delete (rm) files - Check the result of each operation
How do you find where you are and move around?
Unlike a graphical file manager, the terminal does not permanently show where you are. You are always positioned somewhere in the tree, the set of folders arranged as a tree from the root /. On Linux, "directory" is the usual word for "folder", and the one you are in is called the current directory: every command you type applies there by default. Two commands are enough for this: pwd tells you where you are, cd takes you elsewhere.
pwd: where am I?
At any moment, pwd (print working directory) tells you the directory you are in:
pwd/home/bobIt is the first reflex when you are lost: pwd puts you back in context.
cd: change directory
cd (change directory) is the fundamental navigation command. A path that starts with / starts from the root of the system: it is an absolute path. The double dot .. stands for the parent directory, and ~ for your home directory (the home). These are the forms to know:
cd /etc/ssh # go to a directory by absolute pathcd .. # go up one level (parent directory)cd ~ # go back to your home (/home/bob)cd - # go back to the previous directorycd # same as cd ~ (back home)After each cd, run pwd to confirm your position.
cd /var/logpwd/var/logOne last shortcut is worth adopting straight away: cd - takes you back to
the last directory visited and prints the destination path on the way. It is
handy when you switch back and forth between two places, a configuration and the log of the service it drives, for example.
cd /etc/sshcd /var/logcd -/etc/sshTo understand the difference between absolute and relative paths, read the Absolute and relative paths guide.
How do you list files with ls?
ls (list) shows the contents of a directory. Without an option, it shows the names of files and folders:
lsDocuments Images Projects script.shEssential options
Six options are enough for everyday use. The table says what each one does and in which situation to choose it.
| Option | Effect | When to use it |
|---|---|---|
-l | Long format (permissions, owner, size, date) | Check permissions or size |
-a | Shows hidden files (starting with .) | See .bashrc, .ssh/, etc. |
-h | Human-readable sizes (1K, 234M, 2G) | With -l to read sizes quickly |
-t | Sorts by modification date (newest first) | Find the last modified file |
-r | Reverses the sort order | With -t to get the oldest first |
-R | Recursive listing (subdirectories included) | Explore a tree |
Common combinations
Options stack behind a single dash, in any order: ls -la and ls -al give the same result. Here are the four combinations you will type most often.
ls -la # see everything, long format (hidden files included)ls -lh # long format with readable sizesls -lt # long format, sorted by datels -ltr # long format, sorted by date (oldest first)Here is an example on /etc/ssh/, which holds the SSH configuration. It assumes the openssh-server package is installed: without it you will have neither sshd_config nor the host keys, and you can try ls -lah /etc/ instead.
ls -lah /etc/ssh/total 632Kdrwxr-xr-x 4 root root 4,0K 15 nov. 19:03 .drwxr-xr-x 1 root root 4,0K 15 nov. 19:03 ..-rw-r--r-- 1 root root 1,7K 6 mai 2026 ssh_config-rw-r--r-- 1 root root 3,4K 6 mai 2026 sshd_config-rw------- 1 root root 411 15 nov. 19:03 ssh_host_ed25519_key-rw-r--r-- 1 root root 99 15 nov. 19:03 ssh_host_ed25519_key.pubThis output is an extract, taken on Debian 13 (openssh-server 10.0p1, locale fr_FR.UTF-8), which is why the dates and decimals are in French. On your machine the list is longer, and the sizes, dates and permissions differ. The . and .. lines appear because of -a: they are the directory itself and its parent.
Look at the first column first, the permissions. The private key ssh_host_ed25519_key is -rw-------: only root can read it. The public key .pub and the sshd_config file are -rw-r--r--: everyone can read them.
Then look at the date. The month names (nov., mai) and the decimal comma (4,0K) come from the language of the system: in English you will read Nov 15 and 4.0K, with the month before the day. For a file modified more than six months ago, ls shows the year (2026) instead of the time.
Reading the output of ls -l
This dense line is the one you will read most often in administration, so it is worth decoding once and for all. The first character gives the type of the entry, the next nine its permissions, then come the owners, the size and the date of the last modification. The detail of the nine permission characters is in Linux permissions.
-rw-r--r-- 1 root root 1,7K 6 mai 2026 ssh_config│ │ │ │ │ │ ││ │ │ │ │ │ └─ file name│ │ │ │ │ └─ modification date│ │ │ │ └─ size│ │ │ └─ owning group│ │ └─ owning user│ └─ number of links (not important for now)└─ type and permissions (d = directory, - = file, l = link)How do you create files and directories?
On Linux, creating a file and creating a directory are two distinct operations, with two distinct commands. Neither opens an editor or asks for confirmation: they run silently, and it is up to you to check the result with ls. Above all, remember the -p option of mkdir, the one that saves you from creating directories one by one.
touch: create an empty file
touch creates an empty file if it does not exist. If it already exists, it only updates its modification date:
touch /tmp/my-file.txtls -l /tmp/my-file.txt-rw-rw-r-- 1 bob bob 0 Apr 6 14:30 /tmp/my-file.txtThe 0 is the size: the file is empty. The rw-rw-r-- permissions assume a umask of 002. The umask is the mask that removes rights from new files (type umask to read yours; the principle is explained in Linux permissions). With a umask of 022, you would read rw-r--r--. The same umask explains the drwxrwxr-x permissions of a directory, which you will see below.
mkdir: create a directory
mkdir (make directory) creates one or more directories:
mkdir /tmp/my-folderThe -p option creates the whole intermediate tree if it does not exist:
mkdir -p /tmp/project/src/configWithout -p, the command would fail if a parent directory did not exist (/tmp/project/ or /tmp/project/src/).
Check with ls -ld, where -d asks to show the directory itself rather than its contents:
ls -ld /tmp/project/src/configdrwxrwxr-x 2 bob bob 4096 Apr 6 14:35 /tmp/project/src/configHow do you copy files and directories?
Copying a configuration file before editing it is the basic reflex of administration: it is your only way back if the service refuses to restart. Two details make all the difference with this command: without the -r option it refuses to handle a directory, and without -p (or -a) the copy keeps neither the dates nor the original owner.
cp: copy
cp (copy) copies a file or a directory. The first argument is the source, the second the destination:
cp /etc/ssh/ssh_config /tmp/ssh_config.bakThe options of cp to know:
| Option | Effect |
|---|---|
-r | Recursive copy (required for directories) |
-p | Preserves permissions, dates and, if you have the right to, the owner |
-a | Archive: recursive copy that preserves all attributes (equivalent to -dR --preserve=all) |
-i | Asks for confirmation before overwriting an existing file |
-v | Shows each copied file (verbose) |
The -a option is the one for a faithful backup: it copies the whole tree and keeps the permissions, the dates, the hard links and the extended attributes.
With -r alone, it is the opposite: the copy receives the permissions of the original file, reduced by the umask, today's date and the name of the user who makes it.
For a configuration directory, as in the example below, cp -rp is enough; -a is worth it when links and attributes matter too. One limit applies to -p as to -a: only root can keep the owner of a file that belongs to another user. Copied by an ordinary user, the file belongs to that user.
To copy a whole directory, for example /etc/nginx/ if nginx is installed on your machine (otherwise, take another directory of /etc):
cp -rp /etc/nginx/ /tmp/nginx-backup/Check the result:
ls -la /tmp/nginx-backup/How do you move or rename a file?
Linux has no "rename" command: moving a file and renaming it are the same operation, carried out by mv. Changing the name simply means moving the file to a path whose last element differs. Like cp, this command overwrites the destination without warning if it already exists.
mv: move or rename
mv (move) is used both to move and to rename:
# Rename a filemv /tmp/old-name.conf /tmp/new-name.conf
# Move a file into a directorymv /tmp/file.txt /tmp/project/
# Move and rename at the same timemv /tmp/draft.txt /home/bob/Documents/final-report.txtThe useful options of mv:
| Option | Effect |
|---|---|
-i | Asks for confirmation before overwriting |
-n | Never overwrites an existing file |
-v | Shows each operation (verbose) |
mv -v /tmp/test /tmp/test-renamedrenamed '/tmp/test' -> '/tmp/test-renamed'How do you delete files and directories?
This is the section where attention matters most: on the command line there is no trash can and no undo. A successful deletion does not even print a message, which makes a mistake all the more discreet. Two commands exist: rm, which deletes files (and directories with -r), and rmdir, which refuses to touch a non-empty directory.
rm: delete
rm (remove) deletes files. It is irreversible.
rm /tmp/temporary-file.txtThe options of rm:
| Option | Effect |
|---|---|
-r | Recursive deletion (needed for directories) |
-i | Asks for confirmation for each file |
-f | Forces deletion without confirmation |
-v | Shows each deletion |
# Delete a directory and its contentsrm -r /tmp/test-project/
# Delete with confirmationrm -ri /tmp/important-folder/rmdir: delete an empty directory
rmdir only deletes empty directories. It is safer than rm -r when you know the folder should be empty:
rmdir /tmp/empty-folderIf the directory contains files, rmdir refuses and prints an error, which protects you from an accidental deletion.
Practical exercise
This sequence replays every command in the guide on a disposable tree created in /tmp, the directory for temporary files: nothing you do there affects the system. Type the commands rather than pasting them, and always run the check shown before moving on to the next step. The last step cleans everything up.
-
Create a working tree:
Terminal window mkdir -p /tmp/lab-files/{config,backup,logs}The braces
{config,backup,logs}are expanded by the shell:mkdirreceives three paths,/tmp/lab-files/config,/tmp/lab-files/backupand/tmp/lab-files/logs.Check with
ls -R /tmp/lab-files/(-Rgoes down into the subdirectories). -
Create test files:
Terminal window touch /tmp/lab-files/config/app.conftouch /tmp/lab-files/config/db.conftouch /tmp/lab-files/logs/app.log -
Copy the configuration into the backup:
Terminal window cp -rp /tmp/lab-files/config/ /tmp/lab-files/backup/config-2026/Check with
ls -la /tmp/lab-files/backup/config-2026/. -
Rename a file:
Terminal window mv /tmp/lab-files/config/app.conf /tmp/lab-files/config/application.confCheck with
ls /tmp/lab-files/config/. -
Delete the logs:
Terminal window rm /tmp/lab-files/logs/app.logrmdir /tmp/lab-files/logsCheck with
ls /tmp/lab-files/: thelogsfolder is gone. -
Clean everything up:
Terminal window rm -r /tmp/lab-files
Troubleshooting
The error messages of these commands are short but precise: they almost always name the file at fault and the reason for the refusal. Get into the habit of reading them in full before running the command again: the message already tells you which option is missing or which right you lack. Depending on the system language, they may appear in French. Here are the seven you will meet first, with the matching fix.
| Symptom | Likely cause | Fix |
|---|---|---|
No such file or directory | Wrong path or typo | Check with ls and pwd, use the Tab key for completion |
Permission denied | Insufficient permissions on the file or directory | Check with ls -la, use sudo if needed |
cp: -r not specified; omitting directory '/tmp/project' | Missing -r option to copy a directory | Use cp -r source/ destination/ |
rm: cannot remove '/tmp/project': Is a directory | rm without -r on a directory | Use rm -r directory/ |
rmdir: failed to remove '/tmp/project': Directory not empty | The directory contains files (maybe hidden ones) | Check with ls -a, then use rm -r if you are sure |
mkdir: cannot create directory '/tmp/project': File exists | The directory already exists | Nothing to fix if you wanted that directory: it is already there. With -p, mkdir no longer reports an error |
File overwritten by cp or mv | No confirmation asked | Always use -i on important files |
Put it into practice
Handling files without a safety net is exactly what a disposable environment allows: nothing to break, everything to learn. The lab gives you a target tree and has you build it from scratch in your working directory, with cd, ls, mkdir, cp, mv and rm. The validation checks every path: a file with the right name in the wrong place does not count, and neither does an extra file. In the course on AI-generated applications, this lab is optional.
Key points
pwdtells you where you are,cdmoves you,lsshows the contents.ls -lahis a very common combination: long format, hidden files, readable sizes.mkdir -pcreates a whole tree in one command.cp -rpcopies a directory while preserving permissions and dates, the reflex for backups;cp -aalso keeps links and attributes. Only root also keeps the original owner.mvboth moves and renames.rmis irreversible: check withlsbefore deleting, preferrm -iwhen in doubt.- After each operation, check the result with
ls -la.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Absolute and relative paths on Linux
Continue the course: Install and configure Git on Linux, macOS and Windows
A new full quiz with random questions
Work again only on the questions you missed