Linux online course

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

CommandPurposeBasic syntaxKey behavior
touchCreate an empty file or update timestampstouch FILE_NAMECreates the file only when it does not already exist
rmDelete a filerm FILE_NAMERemoves the named file without an interactive trash step by default
lessView a text file interactivelyless FILE_NAMEDisplays 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_NAME

If the named file does not exist, touch creates a new file with no content:

touch new_file

Use ls to list the current directory and confirm that the file exists:

ls

The listing should include new_file. For more information, use the long listing form:

ls -l

The 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.txt

The 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_NAME

For example, open an existing text file with:

less textfile.txt

less 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.

ActionKey or method
Move down or upArrow keys or mouse wheel where supported
Move by a screenPage Down or Page Up
Quitq

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_NAME

To remove the practice file created earlier, first verify its exact name:

ls
rm new_file
ls

The 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_file

Linux 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

  1. Work in a directory where you have permission to create and remove files.
  2. List the current directory with ls so you know which names are present.
  3. Create a practice file with touch new_file.
  4. Run ls again to verify that new_file exists.
  5. View an existing text file with less textfile.txt, then press q to exit.
  6. Before deleting anything, run ls and verify the exact filename.
  7. Remove only the intended practice file with rm new_file.
  8. Run ls again 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_NAME creates an empty file only when that file does not already exist.
  • Running touch on an existing file updates timestamps and does not erase its content.
  • rm FILE_NAME removes a file permanently in normal command-line usage.
  • less FILE_NAME displays content interactively without editing it.
  • Press q to exit less.
  • ls is 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.