In the previous chapter, you learned how to explore the filesystem using pwd, ls, and cd. Now it is time to manipulate the contents of that filesystem. This chapter covers the commands for creating files and directories, copying, moving, and deleting them, viewing file contents, searching for files, using wildcards, and creating symbolic links. These skills are at the core of daily terminal work and will continue to be used in the following chapters.
Creating Files and Directories
The first step in managing files is creating them. Linux provides simple commands to create empty files and new directories.
touch: Creating an Empty File
The touch command is used to create a new empty file. Simply type touch followed by the name of the file you want to create:
touch note.txtIf the file does not exist, touch creates it. If the file already exists, touch updates its modification time without changing the file contents. You can also create several files at once in a single command:
touch file1.txt file2.txt file3.txtmkdir: Creating a Directory
The mkdir command (short for make directory) is used to create a new directory:
mkdir projectThis creates a directory named project inside your current directory.
mkdir -p: Creating Nested Directories
By default, mkdir can only create one level of directory. If you want to create a nested directory, e.g. project/documents/2026, and the parent directories do not yet exist, use the -p (parents) option:
mkdir -p project/documents/2026The -p option creates all necessary parent directories at once, without showing errors even if some directories already exist.
Copying, Moving, and Deleting
Once files and directories are created, you need to know how to copy, move, and delete them.
cp: Copying Files or Directories
The cp command (short for copy) is used to copy files or directories. The pattern is cp source destination:
cp note.txt backup.txtThis copies note.txt into a new file named backup.txt. To copy a directory along with all its contents, use the -r (recursive) option:
cp -r project project-backupWithout the -r option, cp will refuse to copy a directory. This option ensures that all subdirectories and files inside are copied as well.
Some additional useful options for cp:
-i(interactive): prompts for confirmation before overwriting an existing destination file. It is highly recommended to get into the habit of using this, especially when you are first learning, e.g.cp -i note.txt backup.txt.-v(verbose): shows each file being copied, useful for monitoring the copy process when many files are involved.-a(archive): copies while preserving original permissions, ownership, and timestamps, and works recursively without needing to add-ragain. This option is suitable when copying data whose original attributes need to be kept, e.g.cp -a project project-backup.
mv: Moving or Renaming
The mv command (short for move) has two functions: moving a file to another location, or renaming a file. Its pattern is the same as cp, i.e. mv source destination:
mv note.txt documents/This moves note.txt into the documents directory. If the destination is a file name, mv renames it instead:
mv note.txt new-note.txtUnlike cp, the mv command does not require a recursive option to move directories. Like cp, mv also supports the -i option to prompt for confirmation before overwriting a destination file, and -v to display the move process in detail. Because mv can overwrite a destination file without any warning by default, getting into the habit of using mv -i is a good practice when you are not yet sure of the final outcome.
rm: Deleting Files
The rm command (short for remove) is used to delete files or directories:
rm note.txtTo delete a directory along with all its contents, use the -r (recursive) option. The -f (force) option makes deletion proceed without prompting for confirmation:
rm -r project-backup
rm -rf old-directoryIt is important to stress that rm does not move files to the Recycle Bin. Files deleted with rm are permanently gone and very difficult to recover. Therefore, use rm with caution, especially when combined with the -rf options.
As a safety measure, especially while you are still learning, get into the habit of adding the -i option so that rm prompts for confirmation before deleting each file:
rm -i note.txtIf you prefer behaviour more like the Recycle Bin in Windows or Trash on macOS, GNOME provides a trash mechanism that can be accessed from the terminal via the gio trash command (built into GLib/GNOME, available on both Ubuntu and Fedora). This command moves files to the Trash, rather than permanently deleting them, so they can still be restored through the Files application:
gio trash note.txtAn alternative is the third‑party package trash-cli, which provides commands like trash-put with similar functionality. For truly permanent and intentional deletion, such as cleaning up cache files or temporary directories, rm remains the correct command.
rmdir: Removing Empty Directories
The rmdir command (short for remove directory) can only delete empty directories:
rmdir empty-directoryIf the directory still contains files or subdirectories, rmdir will refuse with an error message. This makes rmdir a safer choice when you only want to delete an empty directory.
Viewing File Contents
After creating files, you will certainly want to read their contents. Linux provides several commands for displaying text file contents.
cat: Displaying the Entire Content
The cat command (short for concatenate) displays the entire content of a file at once:
cat note.txtThis prints the whole file to the screen. cat can also be used to concatenate multiple files, e.g. cat file1.txt file2.txt which displays the contents of both sequentially. However, for very long files, cat is not very comfortable because the content scrolls straight down.
less and more: Reading Files Page by Page
For long files, use less or more. Both display the file content one screen at a time:
less long-note.txtless is more modern and flexible than more. Inside less, you can scroll with the arrow keys or Page Up/Page Down, search for text by typing /keyword, and exit by pressing the q key. more only supports limited forward navigation, so less is the recommended choice.
head and tail: Viewing the Beginning and End of a File
head displays the first ten lines of a file, while tail displays the last ten lines:
head long-note.txt
tail long-note.txtYou can specify the number of lines with the -n option, e.g. head -n 5 file.txt for the first five lines. The tail command is very useful for monitoring log files that grow continuously, especially with the -f (follow) option which shows new lines in real time.
Searching for Files
As you accumulate more files, the ability to find them quickly becomes important.
find: Searching by Criteria
The find command is the most powerful search tool in Linux. The basic pattern is find location -name pattern:
find ~ -name "note.txt"This searches for a file named note.txt inside your home directory and all its subdirectories. You can use wildcard patterns, e.g. find ~ -name "*.pdf" to find all PDF files.
In addition to name, find can also filter results by file type using the -type option, often used together with -name:
find ~ -type f -name "*.pdf": searches only for regular files (f) with the.pdfextension, ignoring directories that happen to have similar names.find ~ -type d -name "project*": searches only for directories (d) whose names begin withproject.
find can also search by size (-size) and modification time (-mtime), but the -name and -type criteria above are sufficient for most everyday needs as a beginner.
locate: Searching from a Database
locate works differently: it searches through a pre‑indexed database rather than traversing the filesystem directly. As a result, locate is much faster than find:
locate note.txtBecause it relies on a database, the results from locate may not reflect files that have just been created or deleted. You can manually update the database with sudo updatedb, though updates are usually scheduled automatically on a daily basis.
The locate command is not always installed by default and needs to be installed first. The implementation used by both Ubuntu 26.04 LTS and Fedora Workstation 44 is currently plocate, a modern version of locate that is faster and more space‑efficient than the older mlocate, while remaining compatible with the same locate command:
sudo apt install plocate # on Ubuntu
sudo dnf install plocate # on FedoraOnce installed, you still invoke it with the locate command as usual; the package name plocate refers only to the underlying implementation.
which and whereis: Finding Program Locations
which displays the location of the program that will be run when you type its command:
which python3This shows the full path of the python3 program, e.g. /usr/bin/python3. Meanwhile, whereis provides more comprehensive information, including the location of binary files, manual pages, and source code:
whereis python3Both commands are useful for verifying that the intended program is indeed installed and to know its location.
Wildcards and Globbing
Wildcards are special characters used to match multiple file names at once. This matching mechanism is called globbing and is very useful when working with many files. The three main wildcards are:
*: matches zero or more characters. For example,*.txtmatches all files ending in.txt.?: matches exactly one character. For example,file?.txtmatchesfile1.txt, but notfile12.txt.[abc]: matches any one of the characters inside the brackets. For example,file[123].txtmatchesfile1.txt,file2.txt, orfile3.txt.
Wildcards are usually combined with other commands. For example:
ls *.txt
rm file?.log
cp *.jpg images/The first command lists all .txt files, the second deletes log files with a single character, and the third copies all JPG files to the images directory. Wildcards make operations involving many files much more efficient.
Handling File Names with Spaces or Special Characters
As mentioned in Chapter 11, Linux allows spaces in file names, e.g. Meeting Notes.txt. However, the shell interprets spaces as argument separators, so a command like rm Meeting Notes.txt would be interpreted as two separate arguments, Meeting and Notes.txt, which could be disastrous if either name happens to match another file.
To safely handle such file names, you can wrap them in quotes, or use the escape character \ before the space:
rm "Meeting Notes.txt"
rm Meeting\ Notes.txtThe most practical and error‑free way is to type part of the file name and press Tab to take advantage of tab completion (see Chapter 14), because the shell will automatically add the necessary escapes for you.
Symbolic Links
A symbolic link (or symlink) is a special file that points to another file or directory. Think of a symlink as a "shortcut", similar to a shortcut in Windows. The command to create one is ln with the -s (symbolic) option:
ln -s /home/username/project project-shortcutThis creates a symlink named project-shortcut that points to the directory /home/username/project. When you access project-shortcut, you are actually accessing the original directory.
Symlinks are useful for providing quick access to files or directories that are in long locations, or when a program expects a file in a specific location but the actual file is elsewhere. Note that if the original target is deleted, the symlink becomes "broken" and will not work, although the symlink itself remains.
It is worth noting that ln without the -s option creates a different kind of link, a hard link. Unlike a symlink, which only contains a "pointer" to another location, a hard link points directly to the same inode number as the original file, as explained in the inode concept in Chapter 11. As a consequence, a hard link continues to work normally even if the original file name is deleted, because the actual data is only truly removed when no name refers to that inode. However, hard links have limitations: they cannot point to directories, and they cannot cross different partitions or filesystems, so symlinks are much more commonly used in everyday work.
In this chapter, you have learned the basic commands for managing files and directories: creating files with touch, creating directories with mkdir and mkdir -p, copying with cp (along with options -i, -v, and -a), moving and renaming with mv, and deleting with rm, rmdir, and the safer alternative through gio trash. You have also learned how to view file contents using cat, less, more, head, and tail, search for files with find (including -type filtering), locate/plocate, which, and whereis, utilise wildcards along with handling of spaced file names, and create symbolic links while recognising their difference from hard links.
In the next chapter, you will learn how to get help when you have forgotten or do not understand a command, using man, --help, info, and tldr.

