Skip to main content
In this lesson you’ll learn how to manage files from the command line — a fundamental skill for Red Hat system administrators. Working efficiently at the terminal for creating, copying, moving, renaming, and deleting files reduces errors and speeds up daily tasks. This guide covers the essential commands, a quick command summary table, and a hands-on demo transcript so you can follow along.

Basic navigation

  • pwd — print the current working directory.
  • cd — change directory (e.g., cd /var/log or cd .. to go up one level).
  • ls — list directory contents.
Common ls options:
  • -a — include hidden files (names starting with a dot).
  • -l — long listing format (shows permissions, ownership, size, timestamp).
  • -h — human-readable sizes (e.g., -lh).
Tip: Combine options, e.g., ls -alh to see detailed listings including hidden files with readable sizes.

Copying, moving, and deleting

  • cp — copy files and directories. Use -r (or -a) to copy directories recursively.
  • mv — move or rename files and directories.
  • rm — remove files. Use -r to remove directories and their contents recursively; add -f to force.
  • rmdir — remove empty directories only.
Be careful with rm -r (or rm -rf). It permanently deletes directories and their contents if used without caution.
Safer deletion tips:
  • Use rm -i to prompt before each removal.
  • Run ls -la first to verify the files you plan to delete.
  • Consider moving important data to a temporary backup directory before deletion, e.g., mv important/ ~/trash/.

Creating and updating files and directories

  • mkdir — create a directory. Use -p to create parent directories as needed (e.g., mkdir -p projects/data/reports).
  • rmdir — remove an empty directory.
  • touch — update access and modification times; if the file does not exist, touch creates an empty file.
Example:

Viewing file contents

  • cat — concatenate and display files (prints entire contents).
  • head — display the first 10 lines of a file by default; use -n to change the number of lines displayed (e.g., head -n 5 file.txt).
  • tail — display the last 10 lines by default; use -n to change the number of lines (e.g., tail -n 50 /var/log/messages).
  • more / less — view file contents one page at a time (less is generally preferred for navigation).
Use these to inspect logs and configuration files without opening a graphical editor.

Commands at a glance

Hands-on demo

Below is a step-by-step demo transcript showing common file-management workflows: creating directories, creating files, copying/moving files, and removing files and directories safely.
  • Note: the leading d in drwxr-xr-x indicates a directory.
Continue the demo:
  • echo ... > filename writes a line to a file (overwrites if the file exists).
  • cat prints the entire file; head prints the first lines; tail prints the last lines.
Copy a file into the data directory:
Copy a directory recursively (including its files and subdirectories):
Rename (move) a file:
Remove a file:
Attempt to remove a non-empty directory with rmdir:
rmdir fails because data contains files and/or subdirectories. Remove a non-empty directory with rm -r:
This completes the demo: navigating, creating, managing, and inspecting files. These simple commands form the core file-management workflow you’ll use constantly as an administrator. Use these references to dive deeper into options, advanced usage, and best practices for safe file management.

Watch Video