>_ Operating systems & Linux

First Linux shell commands: moving between files and folders

The essential Linux terminal commands explained with examples: pwd, ls, cd, mkdir, cp, mv, rm, absolute and relative paths, completion and history.

Opening a terminal for the first time is often unsettling. Yet a dozen commands are enough to move between files and folders at a speed the graphical interface rarely allows. This article collects the ones I use every day and the habits that prevent the typical beginner mistakes.

Terminal, shell and prompt

The terminal is the window you type into. The shell is the program that interprets what you type: on most distributions it is Bash, on macOS since Catalina it is Zsh. The prompt is the line before the cursor and usually shows the user, the machine name and the current folder, for example:

anna@laptop:~/Documents$

The ~ symbol stands for your home folder, while $ marks a normal user. If you see #, you are working as administrator and should be more careful.

Where am I and what is here

  • pwd (print working directory) prints the full path of the current folder.
  • ls lists the folder's contents.
  • ls -l shows details: permissions, owner, size, modification date.
  • ls -a includes hidden files, those whose name starts with a dot, such as .bashrc.
  • ls -lh makes sizes human-readable (K, M, G instead of bytes).

Options can be combined: ls -lah is probably the most used variant of all.

Moving around: cd and paths

The cd command changes folder. The tricky part is understanding the difference between two kinds of path.

Absolute paths

They start with /, the root of the filesystem, and always point to the same place regardless of where you are: cd /var/log takes you there from anywhere.

Relative paths

They start from the current folder. If you are in /home/anna and type cd Documents, you land in /home/anna/Documents. Two shortcuts are essential:

  • . means the current folder;
  • .. means the parent folder, so cd .. goes up one level.

Other useful shortcuts: cd on its own or cd ~ takes you back home, cd - returns to the folder you were in before.

Creating, copying, moving

Command What it does Example
mkdir creates a folder mkdir projects
mkdir -p also creates intermediate folders mkdir -p photos/2025/summer
touch creates an empty file or updates its date touch notes.txt
cp copies a file cp notes.txt notes-backup.txt
cp -r copies a folder with its contents cp -r projects projects-copy
mv moves or renames mv notes.txt archive/

The mv command does not distinguish between moving and renaming: mv old.txt new.txt changes the name, mv file.txt folder/ moves it. Be careful: if the destination file already exists, cp and mv overwrite it without asking. The -i (interactive) option makes them ask for confirmation.

Deleting: rm has no trash

rm file.txt deletes a file, rm -r folder deletes a folder and everything in it. There is no trash: what rm deletes cannot be restored with a click. A few habits reduce the risk:

  1. Before deleting with a wildcard, check what matches: ls *.log and only then rm *.log.
  2. Use rm -i when deleting several files in a folder you do not know well.
  3. Be wary of commands copied from the internet that contain rm -rf: the -f option suppresses every confirmation.
  4. To remove an empty folder there is rmdir, which fails if the folder contains anything: a small safety net.

Reading files without opening an editor

  • cat file.txt prints the whole content, fine for short files.
  • less file.txt lets you scroll through a long file; quit with q and search with /word.
  • head -n 20 file.txt shows the first 20 lines, tail -n 20 the last ones.
  • tail -f logfile.log keeps listening and shows new lines as they are written: very handy for following a log in real time.

Shortcuts that save time

The Tab key completes file and command names: type cd Doc and press Tab, and the shell completes it to Documents/. If there are several possibilities, a double Tab lists them. Besides saving time, completion prevents typos in names.

The up and down arrows scroll through commands you have already run. Ctrl+R starts a reverse search through the history: type part of a command used days ago and the shell finds it. Ctrl+C interrupts the running command, Ctrl+L clears the screen.

Names with spaces and special characters

The shell uses spaces to separate arguments, so cd Holiday photos is read as two arguments. The fixes are quotes, cd "Holiday photos", or a backslash, cd Holiday\ photos. Tab completion inserts the correct escaping automatically. For files you create yourself, names without spaces or accented letters, using hyphens or underscores, make life easier in the long run.

When you cannot remember an option

Every command has a manual: man ls opens the full documentation. Many commands also accept --help for a shorter summary. Reading the first lines of a man page before using an unfamiliar option is a habit that prevents a lot of trouble.

In short

  • pwd, ls and cd are enough to find your way; learn the difference between absolute and relative paths.
  • cp, mv and rm overwrite and delete without asking: use -i when needed.
  • rm has no trash: check with ls what matches before deleting.
  • Tab, the arrow keys and Ctrl+R make the terminal faster and less error-prone.
  • man and --help are the first place to look for any command.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. With degrees in computer science and psychology, he combines technical analysis with an understanding of the human factor.

Read also