VMware ESXi and vSphere Cluster Management

File Management in Raspberry Pi OS Terminal

Learn how to create, delete, copy, view, and organize files and directories in Raspberry Pi OS using terminal commands.

Introduction to terminal-based file management

A terminal is a text-based interface for entering operating system commands. Raspberry Pi OS can manage files through terminal commands as well as through its graphical file manager.

A file is a named unit of stored data, such as a text document, image, or program. A directory is a folder-like container that can hold files and other directories.

A path identifies the location of a file or directory. An absolute path starts at the filesystem root, represented by /. For example, /home/pi/textfile.txt identifies a file named textfile.txt inside /home/pi/.

A relative filename or path is interpreted from the current directory, which is the directory where the terminal is currently operating. If the current directory is /home/pi/, these two commands refer to the same file:

less /home/pi/textfile.txt
less textfile.txt

Linux filenames and paths are case-sensitive. For example, TextFile.txt and textfile.txt are different names.

Basic file and directory management commands

CommandPurposeBasic syntaxExampleImportant note
touchCreate a file or update its timestampstouch filenametouch textfile.txtIt does not normally add text to the file.
rmRemove filesrm filename-or-pathrm /home/pi/textfile.txtDeletion can be permanent.
cpCopy files or directoriescp source destinationcp /home/pi/textfile.txt /tmp/The original remains in place.
lessRead text one screen at a timeless filename-or-pathless /home/pi/textfile.txtPress q to exit.
catPrint file contentscat filename-or-pathcat /home/pi/textfile.txtBest suited to short files.
mkdirCreate a directorymkdir directorymkdir practice-directoryThe parent directory must exist and be writable.
rmdirRemove an empty directoryrmdir directoryrmdir practice-directoryIt fails if the directory contains anything.
lsList directory contentsls [path]lsWith no path, it lists the current directory.
cdChange the current directorycd directorycd practice-directoryRelative paths are resolved from the current directory.

Creating files with touch

The touch command creates an empty file when the specified name does not already exist. Its basic structure is:

touch filename

Use an absolute path to create textfile.txt in the home directory for the pi user:

touch /home/pi/textfile.txt

This command creates a zero-length file. It does not open an editor or place text inside the file. If the file already exists, touch normally updates the file's timestamp instead of creating a second file.

Confirm that the file exists with ls:

ls /home/pi/

If the file is already present, that may be why running touch appears not to create anything new. Also check that the destination directory exists and that your user can write there.

Deleting files with rm

The rm command removes a file. Its basic structure is:

rm filename-or-path

After verifying the path, remove the example file with:

rm /home/pi/textfile.txt

File deletion with rm is potentially permanent. Unlike moving a file to a graphical desktop's trash, this command normally does not provide an easy undo operation. Check spelling, capitalization, and the complete path before pressing Enter.

Copying files with cp

The cp command takes a source and a destination:

cp source destination

For example, copy the file from /home/pi/ into the temporary directory:

cp /home/pi/textfile.txt /tmp/

This creates another copy at /tmp/textfile.txt and leaves the original at /home/pi/textfile.txt. Check the destination after copying:

ls /tmp/

The source path must identify an existing file. The destination directory must exist and be writable. If the destination is a filename rather than a directory, cp uses that name for the new copy.

Viewing text files with less

less is a paged text viewer. It presents a file one screen at a time, which is useful when the file is longer than the terminal window. Open the example file with:

less /home/pi/textfile.txt

While inside less, you can read the displayed content and move through the file using the viewer's controls. Press q to quit and return to the shell prompt.

q

If the terminal does not return to the shell, you are probably still inside less; press q rather than entering another shell command.

Viewing text files with cat

The cat command writes a text file's contents directly to the terminal:

cat filename-or-path

For the example file, use:

cat /home/pi/textfile.txt

cat is convenient for short files because it prints everything immediately. For a long file, output may scroll past quickly. Use less when you need paged viewing and interactive reading.

CommandBest useHow output is shownHow to exit
lessLong or interactive text-file readingOne screen at a time in a viewerPress q
catShort files or quick outputAll contents are printed to the terminalNo viewer to exit; the command ends after printing

Managing directories

Directories organize files and other directories. Because commands use the current directory when no absolute path is supplied, changing directories changes how relative filenames are interpreted.

Create and inspect a practice directory

Create a directory in the current directory:

mkdir practice-directory

List the current directory to confirm that it exists:

ls

Enter it with cd:

cd practice-directory

Now practice-directory is the current directory. Running ls lists its contents, and a relative command such as less file.txt looks for file.txt inside this directory.

ls

To return to the parent directory, use:

cd ..

The two dots represent the parent directory. You can also use an absolute path with cd when you want to go directly to a known location.

Remove an empty directory

rmdir removes a directory only when it is empty:

rmdir practice-directory

If rmdir fails, use ls to inspect the directory. It may contain files or subdirectories. Remove or relocate those contents first, then run rmdir again. Be especially careful with recursive deletion options, which can remove directory contents and are outside this basic workflow.

How paths affect commands

Suppose the current directory is /home/pi/. These commands use the same source file:

cat textfile.txt
cat /home/pi/textfile.txt

If you change to another directory, the relative command may no longer find the file:

cd /tmp/
cat textfile.txt

In this situation, cat looks for /tmp/textfile.txt. To refer to the home-directory copy, use its absolute path:

cat /home/pi/textfile.txt

The same rule applies to touch, rm, cp, less, and directory commands. When a command reports that a file cannot be found, check the current directory, spelling, capitalization, and path.

Getting command help

Most common commands provide brief built-in help with the --help option:

cp --help

Help output commonly shows command syntax, available options, and a short explanation of behavior. For fuller documentation, open the command's man page, which is the built-in manual documentation for that command:

man cp

Manual pages can be longer than the terminal window and are usually displayed in a pager. Press q to leave the manual page. Replace cp with another command name to read its documentation, such as man less or man mkdir.

Troubleshooting common problems

touch appears not to create a file

  • The file may already exist; use ls to check it. In that case, touch updated its timestamp.
  • The destination directory may not exist. Verify the path.
  • You may not have permission to write in that directory. Try a writable location such as your home directory.

rm says that a file cannot be found

  • Check for a misspelled or incorrectly capitalized filename.
  • Use ls to inspect the current directory.
  • Use an absolute path or change to the correct directory with cd.

cp fails or the copy is unexpected

  • Use ls to verify the source file.
  • Confirm that the destination directory exists.
  • Check that the destination is writable.
  • List the destination after copying to see exactly where the new file is located.

cat output scrolls too quickly

The file is probably longer than the terminal window. Use less filename-or-path for paged viewing.

rmdir fails

The directory contains a file or subdirectory. Run ls to inspect it, remove or relocate its contents, and then retry rmdir.

Practical command sequence

The following sequence demonstrates the main operations. The copy and viewing commands should be run before removing the original file.

touch /home/pi/textfile.txt
ls /home/pi/
cp /home/pi/textfile.txt /tmp/
ls /tmp/
less /home/pi/textfile.txt
cat /home/pi/textfile.txt
rm /home/pi/textfile.txt
mkdir practice-directory
ls
cd practice-directory
ls
cd ..
rmdir practice-directory

Because the file created by touch is empty, less and cat may display no visible text. The file still exists until the rm command removes it.

Key points to remember

  • Use absolute paths beginning with / when you need an unambiguous location.
  • Relative filenames are resolved from the current directory.
  • touch creates an empty file or updates an existing file's timestamps.
  • rm removes files and should be used only after checking the path.
  • cp creates a second copy and leaves the source unchanged.
  • Use less for paged reading and cat for quick output of short files.
  • Use mkdir, rmdir, ls, and cd to manage directories and navigation.
  • Use command --help for a quick reference and man command for full documentation.

For a consolidated reference, return to File Management.