5.6 Basic File Management: cp, mv, rm, touch, mkdir, rmdir (103.3)

Key Takeaways

  • cp copies files and trees; -a (--archive) is equivalent to -dpR and preserves permissions, timestamps, ownership, and symbolic links.
  • mv renames or moves files; moving across filesystems copies data and deletes original, whereas same-filesystem moves only update directory entries.
  • rm removes directory entries and unlinks inodes; -r recurses directories and -f forces removal without prompting.
  • touch updates access (-a) and modification (-m) timestamps or creates 0-byte files; explicit dates use -t or -d.
  • mkdir -p creates nested parent directories idempotently, while file uses magic numbers in file headers rather than file extensions to identify types.
Last updated: August 2026

5.6 Basic File Management: cp, mv, rm, touch, mkdir, rmdir (103.3)

Quick Summary: Linux systems administrators manage files and directory structures using core utilities: cp (copying with attribute preservation), mv (moving/renaming), rm (unlinking and recursive deletion), touch (timestamp manipulation), mkdir (directory creation), rmdir (removing empty directories), file (header magic number classification), and ls (directory listing). Mastery of exact flag behaviors is essential for Topic 103.3.


1. cp: Copying Files and Directories

The cp utility creates duplicate copies of files and directory trees.

Essential cp Command-Line Flags

FlagLong OptionFunction & Description
-r / -R--recursiveCopies directories and their subtrees recursively
-p--preserve=mode,ownership,timestampsPreserves file permissions (mode), ownership (UID/GID), and timestamps
-a--archiveArchive mode: Preserves all attributes, recursion, and symbolic links. Equivalent to -dpR
-d--no-dereferenceCopies symbolic links as symbolic links rather than copying the target files
-i--interactivePrompts for confirmation before overwriting an existing destination file
-f--forceIf destination file cannot be opened, removes it first and tries again; never prompts
-u--updateCopies only when source file is newer than destination or destination is missing
-s--symbolic-linkCreates symbolic links instead of copying file contents
-l--linkCreates hard links instead of copying file contents
-v--verboseDisplays detailed progress showing each file copied
-b--backupCreates a backup of each existing destination file (file~) before overwriting
# Creating an exact archive backup of /etc preserving all ownership, permissions, and symlinks:
sudo cp -a /etc /backup/etc_20260829

# Updating a destination directory with only modified or new source files:
cp -uv /src/* /opt/destination/

💡 LPIC-1 Exam Fill-in-the-Blank Alert: Which cp option flag is identical to -dpR and enables full archive copying (preserving links, ownership, permissions, and timestamps)? The answer is -a (or --archive).


2. mv and rm: Moving, Renaming, and Removing

The mv Utility

mv moves or renames files and directories.

  • Same Filesystem: Only updates directory entries; the inode number and disk data blocks remain unchanged.
  • Cross-Filesystem: Copies the file data blocks to the target filesystem, allocates a new inode, and removes the original source.
FlagLong OptionBehavior
-i--interactivePrompts before overwriting an existing destination file
-f--forceOverwrites destination files without prompting
-u--updateMoves only when source is newer or destination is missing
-n--no-clobberDoes not overwrite an existing destination file
-v--verbosePrints source and destination names during moves

The rm Utility

rm removes hard links from directory structures (unlinking). When a file's hard link count reaches 0 and no running processes hold open file descriptors to it, the kernel frees its storage blocks.

FlagLong OptionBehavior
-r / -R--recursiveRemoves directories and their contents recursively
-f--forceIgnores non-existent files, never prompts, suppresses error codes
-i--interactivePrompts before every file removal
-IN/APrompts once before removing more than 3 files or recursing
-d--dirRemoves empty directories (identical to rmdir)
# Safely removing a directory tree with a single confirmation prompt:
rm -rI /tmp/build_artifacts

# Removing files that have leading hyphens (preventing flag confusion):
rm -- -rf_malicious_filename.txt

3. touch: Timestamp Modification & File Creation

Every file in a Linux filesystem maintains three primary timestamps:

  1. atime (Access Time): Last time the file's contents were read (e.g., via cat, grep).
  2. mtime (Modification Time): Last time the file's contents were modified/written.
  3. ctime (Change Time): Last time the file's inode metadata or permissions were altered (updated automatically by kernel; cannot be set manually).

If the target file does not exist, touch creates a new empty 0-byte regular file.

Essential touch Command-Line Flags

FlagLong OptionFunction & Description
-aN/AChanges the access time (atime) only
-mN/AChanges the modification time (mtime) only
-c / -h--no-createDo not create file if it does not already exist (updates timestamps only)
-d '<string>'--dateUses human-readable date string instead of current time
-t <stamp>N/AUses explicit format: [[CC]YY]MMDDhhmm[.ss]
-r <file>--referenceCopies timestamps from <file> to the target file
# Setting explicit timestamp: August 29, 2026, at 14:30:00 (Format: YYYYMMDDhhmm.ss):
$ touch -t 202608291430.00 release.tar.gz

# Setting date using human-readable string:
$ touch -d "2026-08-29 14:30:00" release.tar.gz
$ touch -d "3 days ago" file.log

# Updating timestamp only if file exists (no new file created):
$ touch -c /tmp/optional_lockfile.pid

4. mkdir and rmdir: Directory Management

  • mkdir (Make Directory):
    • -p (--parents): Creates parent directories as needed without error if they already exist (idempotent). E.g., mkdir -p /opt/app/releases/v2/config.
    • -m <mode> (--mode): Sets octal or symbolic permissions at creation time (e.g., mkdir -m 700 /home/user/.ssh).
    • -v (--verbose): Displays a message for each created directory.
  • rmdir (Remove Directory):
    • Removes empty directories. Fails with Directory not empty if any files, subdirectories, or hidden dotfiles exist.
    • -p (--parents): Removes directory and its parent ancestors if they become empty (e.g., rmdir -p a/b/c deletes c, then b if empty, then a if empty).

5. file: File Classification via Magic Numbers

The file command determines the true data type of a file by inspecting header bytes and signatures (known as magic numbers stored in /usr/share/misc/magic), rather than relying on file extensions.

Essential file Flags

  • -b (--brief): Brief mode; omits the filename prefix from output lines.
  • -z (--uncompress): Looks inside compressed files (e.g., inspects contents of .gz archives).
  • -i (--mime): Outputs MIME type strings (e.g., text/plain; charset=utf-8 or application/x-pie-executable).
  • -L (--dereference): Follows symbolic links to determine the target's type.
$ file /bin/bash
/bin/bash: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked...

$ file -b -i /etc/passwd
text/plain; charset=us-ascii

$ file -z backup.tar.gz
backup.tar.gz: POSIX tar archive (gzip compressed data...)

6. ls: Directory Listing Options Reference

FlagLong OptionFunction & Description
-a--allLists all files including hidden files starting with . as well as . and ..
-A--almost-allLists all files including hidden files, but excludes . and ..
-lN/ALong listing format (permissions, link count, owner, group, size, mtime, name)
-h--human-readableDisplays file sizes with units (K, M, G) in powers of 1024 (used with -l)
-tN/ASorts listing by modification time (mtime), newest first
-r--reverseInverts sort order (e.g., ls -ltr lists oldest first, newest at the bottom)
-R--recursiveLists all subdirectories recursively
-d--directoryLists directory entries themselves, not their contents (e.g., ls -ld /var/log)
-i--inodePrints the internal filesystem index number (inode) of each file
-SN/ASorts files by file size, largest first
-1N/ALists one file per line (ideal for scripts and pipelines)
Test Your Knowledge

Which command will copy an entire directory tree named /var/www to /backup/www while preserving all file permissions, ownerships, timestamps, and symbolic links without dereferencing them?

A
B
C
D
Test Your Knowledge

A system administrator needs to update the access and modification timestamps of a file named /etc/app.conf to the current time, but must NOT create the file if it does not already exist. Which command achieves this?

A
B
C
D
Test Your Knowledge

Which command and option should be used to display detailed metadata (permissions, owner, size) for the directory /etc/nginx itself, rather than listing the contents inside that directory?

A
B
C
D