VMware ESXi and vSphere Cluster Management

Manage Files in Linux with touch, rm, and less

Learn how to create files with touch, delete them with rm, and read text files one screen at a time with less in the Linux shell.

Linux file management can be performed from a shell, a command-line interface used to issue Linux commands. From the shell, you can create, view, rename, move, edit, and delete files. This lesson focuses on three basic tasks: creating an empty file with touch, removing a file with rm, and reading a text file with less.

A file is a named collection of data stored in the filesystem. A text file contains readable character-based content. A directory is a container that holds files and other directories. File operations and directory operations are related but not identical: touch, rm, and less primarily operate on files, while commands such as mkdir and rmdir manage directories.

Core Linux file commands

CommandPurposeBasic syntaxKey behavior or caution
touchCreate an empty file or update timestampstouch FILE_NAMEDoes not replace the contents of an existing file.
rmRemove a filerm FILE_NAMERemoval is normally immediate; check the name carefully.
rm -iRemove a file with confirmationrm -i FILE_NAMEPrompts before deleting the file.
lessRead a text file one screen at a timeless FILE_NAMEDisplays content; it is not primarily an editor.
lsList directory contentslsUseful for verifying creation and deletion.
pwdDisplay the current working directorypwdHelps confirm where a relative path is interpreted.

Create files with touch

touch creates an empty file when the specified filename does not already exist. Its basic syntax is:

touch FILE_NAME

For example, create a file named new_file in the current working directory and verify it with ls:

touch new_file
ls

The expected result is that new_file appears in the directory listing. You can also inspect its details with:

ls -l new_file

If a file with that name already exists, touch does not replace its contents. Instead, it updates filesystem timestamps, which are stored date and time metadata associated with filesystem activity, including modification time.

touch textfile.txt
ls -l textfile.txt

The file remains in place and its timestamp may change, while its text content remains unchanged. If touch does not seem to create a file, check whether it already exists, confirm the location with pwd, and verify that you have write permission in the directory.

Delete files with rm

rm removes files from the shell. Its basic syntax is:

rm FILE_NAME

To remove the example file and verify that it is gone:

rm new_file
ls

new_file should no longer appear in the listing. Removing a file with standard rm is normally immediate and does not use a recycle bin. Use the command carefully, especially when working with paths, wildcards, or filenames typed by hand.

Ask for confirmation with rm -i

The -i option enables interactive deletion. The shell asks for confirmation before removing the file:

rm -i "project notes.txt"

Answer the prompt only after checking that the displayed filename is the one you intend to remove. For cautious manual deletion, review the target with ls first and prefer rm -i.

Read text files with less

less is a terminal pager: a program that displays text output or file content one screen at a time. Its basic syntax is:

less FILE_NAME

It is especially useful for text files that are too long to fit on one terminal screen. Create a small sample file and open it:

printf 'First line\nSecond line\nThird line\n' > textfile.txt
less textfile.txt

Inside less, use the following controls:

Key or inputAction
qQuit less and return to the shell.
Arrow keysMove through the content line by line.
SpaceMove forward by one screen.
bMove backward by one screen.
Page Up / Page DownMove backward or forward by a page.
/search-termSearch forward for the specified term.
nMove to the next search match.

less displays content; it does not primarily edit the file. Use a text editor when you need to change file contents. If a file shows unreadable characters, it may be binary rather than plain text or may use an unexpected character encoding. Confirm that the file is intended to be text before reading it with less.

Work safely with filenames and paths

A filename is the name assigned to a file within a directory. A path identifies a filesystem location. A relative path is interpreted from the current working directory, while an absolute path is a full path beginning at the filesystem root.

pwd
ls
touch notes.txt
less notes.txt
rm notes.txt

In this example, notes.txt is a relative filename. Use pwd when you are unsure of the current location. To operate on a file elsewhere, supply a relative or absolute path, such as:

less documents/report.txt
rm /home/user/documents/report.txt

Filenames containing spaces or shell-special characters should be quoted. Quoting tells the shell to treat the enclosed text as one filename:

touch "project notes.txt"
less "project notes.txt"
rm -i "project notes.txt"

Tab completion is another practical safety technique. Type enough of a filename to identify it, then press the Tab key. The shell can complete the name or show matching choices, reducing typing errors and helping you target the intended file.

A basic verification workflow

  1. Confirm the location with pwd.
  2. List the current contents with ls.
  3. Create an empty file with touch new_file.
  4. Run ls again to verify that it appears.
  5. Create sample text, if needed, with printf 'First line\nSecond line\nThird line\n' > textfile.txt.
  6. Read the text with less textfile.txt, then press q to quit.
  7. Remove the temporary file with rm -i new_file and confirm the prompt.
  8. Run ls once more to verify the result.

Troubleshooting

rm reports that a file does not exist

  • The filename may be misspelled.
  • You may be in a different directory than expected.
  • The name may contain spaces or special characters and need quoting.
pwd
ls
ls -l "project notes.txt"

Use tab completion or quote the filename. If the file is elsewhere, provide the correct path.

less cannot open the requested file

  • Verify the filename and path with ls.
  • Use ls -l FILE_NAME to inspect whether the file exists and its details.
  • The file may have been deleted or never created.
  • The current user may not have read permission.

touch did not appear to create a file

  • A file with the same name may already exist; use ls -l FILE_NAME to check.
  • You may be looking in a different directory; run pwd.
  • You may lack write permission in the current directory; try a directory where you have permission.

A file was removed accidentally

Common causes include using the wrong filename or path, or using a wildcard or unquoted name that matched unintended files. Standard rm usually does not provide a built-in recycle bin. Review names with ls before deletion, use rm -i for cautious manual removal, and restore the file from a backup when one is available.

Key points

  • Use touch FILE_NAME to create an empty file. For an existing file, it updates timestamps without replacing contents.
  • Use rm FILE_NAME to remove a file, and rm -i FILE_NAME to request confirmation.
  • Use less FILE_NAME to read a text file one screen at a time; press q to exit.
  • Use pwd and ls to confirm your location and verify operations.
  • Quote filenames containing spaces or special characters, and use tab completion to reduce mistakes.

For the next steps, study directory management with mkdir, rmdir, and rm, moving and renaming with mv, copying with cp, terminal text editors, permissions and ownership, and searching with find and grep.

Return to the file-management lesson