Manage Files in Linux: touch, rm, and less
Learn how to create empty files with touch, delete files with rm, and read text files page by page with less in the Linux shell.
Introduction to Linux file management
The Linux shell is a command-line environment used to run commands and manage files. Shell commands can create, inspect, rename, move, and remove files.
A file is a named collection of data stored in the filesystem. A directory is a container used to organize files and other directories. This lesson focuses on three basic file tasks: creating an empty file, deleting a file, and displaying the contents of a text file. Directory navigation, creation, and removal use related but different commands.
A filename identifies a file within a directory. A path describes the file's location. In the introductory examples, the filenames are relative names, meaning they refer to files in the current working directory—the directory where the shell is currently operating.
Basic file-management commands
| Command | Purpose | Basic syntax | Key behavior |
|---|---|---|---|
touch | Create an empty file or update timestamps | touch FILE_NAME | Creates the file only when it does not already exist |
rm | Delete a file | rm FILE_NAME | Removes the named file without an interactive trash step by default |
less | View a text file interactively | less FILE_NAME | Displays content page by page; press q to quit |
Creating files with touch
touch is commonly used to create an empty file. Its basic syntax is:
touch FILE_NAMEIf the named file does not exist, touch creates a new file with no content:
touch new_fileUse ls to list the current directory and confirm that the file exists:
lsThe listing should include new_file. For more information, use the long listing form:
ls -lThe long listing shows details such as permissions, ownership, size, and timestamps. A timestamp is stored time information associated with a file, such as its modification time.
What happens when the file already exists?
If the file already exists, touch does not replace or clear its contents. Instead, it updates the file's access and modification timestamps.
touch example.txt
touch example.txtThe second command leaves example.txt in place and updates its timestamps. You can use ls -l example.txt to inspect its displayed modification time. This makes touch a non-destructive way to ensure that a file exists, provided that you understand its timestamp behavior.
Viewing text files with less
A text file contains readable characters that can be displayed in a terminal. less is a pager: a program that displays file contents one screen at a time. It is especially useful when a file is longer than the terminal window.
Use this syntax:
less FILE_NAMEFor example, open an existing text file with:
less textfile.txtless displays the contents interactively. It does not edit or change the file. You can read the content, move through it, and then return to the shell.
| Action | Key or method |
|---|---|
| Move down or up | Arrow keys or mouse wheel where supported |
| Move by a screen | Page Down or Page Up |
| Quit | q |
After opening the viewer, press q to quit and return to the shell prompt. If you cannot enter another shell command, less is probably still running; press q.
Deleting files with rm
rm removes files. Its basic syntax is:
rm FILE_NAMETo remove the practice file created earlier, first verify its exact name:
ls
rm new_file
lsThe first listing should show new_file. After rm new_file completes, the second listing should no longer show it.
When the target does not exist
If the target file does not exist in the current working directory, rm reports an error and does not remove that file. Common causes include a misspelled filename, incorrect capitalization, a file that was already removed, or running the command from a different directory.
Check the directory contents and compare the spelling carefully:
ls
rm new_fileLinux filenames are case-sensitive. For example, new_file, New_File, and NEW_FILE are different names.
Files and directories are different targets
The basic rm FILE_NAME form is intended for a regular file. A directory is a different filesystem object, so attempting to remove a directory with the ordinary file form generally produces an error. Confirm the target type before choosing a directory-removal command. Directory removal can affect many items, so do not use recursive or force options unless you understand their effect.
A safe practice workflow
- Work in a directory where you have permission to create and remove files.
- List the current directory with
lsso you know which names are present. - Create a practice file with
touch new_file. - Run
lsagain to verify thatnew_fileexists. - View an existing text file with
less textfile.txt, then pressqto exit. - Before deleting anything, run
lsand verify the exact filename. - Remove only the intended practice file with
rm new_file. - Run
lsagain to confirm that the file is gone.
Use relative filenames for these beginner examples. If the file is elsewhere, its correct path must be supplied. Always enter filenames and paths accurately, including capitalization.
Troubleshooting
touch does not appear to create the expected file
- Check the current directory and its contents with
ls. - Verify the exact filename and capitalization.
- Make sure you are using a location where you have permission to create files.
rm reports that a file does not exist
- List the directory with
ls. - Check spelling, capitalization, and punctuation.
- Confirm that the file was not already removed.
- Make sure you are in the expected directory, or provide the correct relative or absolute path.
rm cannot remove the target
- Confirm whether the target is a directory rather than a regular file.
- Check the permissions affecting the file and its parent directory.
- Avoid escalating privileges unless you understand exactly what will be removed.
less cannot open a file
- Verify the name and path with
ls. - Confirm that the file exists.
- Check whether you have permission to read the file.
Exam-relevant notes
touch FILE_NAMEcreates an empty file only when that file does not already exist.- Running
touchon an existing file updates timestamps and does not erase its content. rm FILE_NAMEremoves a file permanently in normal command-line usage.less FILE_NAMEdisplays content interactively without editing it.- Press
qto exitless. lsis useful before and after file operations for verification.- File and directory removal are distinct operations; do not assume that a command for one is appropriate for the other.
Related Linux topics
To understand where files are located, review Linux file structure. For directory-focused operations, see Linux command-line topics. File permissions are also important when creating, reading, and removing files; see managing file ownership.