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.
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), andls(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
| Flag | Long Option | Function & Description |
|---|---|---|
-r / -R | --recursive | Copies directories and their subtrees recursively |
-p | --preserve=mode,ownership,timestamps | Preserves file permissions (mode), ownership (UID/GID), and timestamps |
-a | --archive | Archive mode: Preserves all attributes, recursion, and symbolic links. Equivalent to -dpR |
-d | --no-dereference | Copies symbolic links as symbolic links rather than copying the target files |
-i | --interactive | Prompts for confirmation before overwriting an existing destination file |
-f | --force | If destination file cannot be opened, removes it first and tries again; never prompts |
-u | --update | Copies only when source file is newer than destination or destination is missing |
-s | --symbolic-link | Creates symbolic links instead of copying file contents |
-l | --link | Creates hard links instead of copying file contents |
-v | --verbose | Displays detailed progress showing each file copied |
-b | --backup | Creates 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
cpoption flag is identical to-dpRand 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.
| Flag | Long Option | Behavior |
|---|---|---|
-i | --interactive | Prompts before overwriting an existing destination file |
-f | --force | Overwrites destination files without prompting |
-u | --update | Moves only when source is newer or destination is missing |
-n | --no-clobber | Does not overwrite an existing destination file |
-v | --verbose | Prints 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.
| Flag | Long Option | Behavior |
|---|---|---|
-r / -R | --recursive | Removes directories and their contents recursively |
-f | --force | Ignores non-existent files, never prompts, suppresses error codes |
-i | --interactive | Prompts before every file removal |
-I | N/A | Prompts once before removing more than 3 files or recursing |
-d | --dir | Removes 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:
atime(Access Time): Last time the file's contents were read (e.g., viacat,grep).mtime(Modification Time): Last time the file's contents were modified/written.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
| Flag | Long Option | Function & Description |
|---|---|---|
-a | N/A | Changes the access time (atime) only |
-m | N/A | Changes the modification time (mtime) only |
-c / -h | --no-create | Do not create file if it does not already exist (updates timestamps only) |
-d '<string>' | --date | Uses human-readable date string instead of current time |
-t <stamp> | N/A | Uses explicit format: [[CC]YY]MMDDhhmm[.ss] |
-r <file> | --reference | Copies 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 emptyif 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/cdeletesc, thenbif empty, thenaif empty).
- Removes empty directories. Fails with
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.gzarchives).-i(--mime): Outputs MIME type strings (e.g.,text/plain; charset=utf-8orapplication/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
| Flag | Long Option | Function & Description |
|---|---|---|
-a | --all | Lists all files including hidden files starting with . as well as . and .. |
-A | --almost-all | Lists all files including hidden files, but excludes . and .. |
-l | N/A | Long listing format (permissions, link count, owner, group, size, mtime, name) |
-h | --human-readable | Displays file sizes with units (K, M, G) in powers of 1024 (used with -l) |
-t | N/A | Sorts listing by modification time (mtime), newest first |
-r | --reverse | Inverts sort order (e.g., ls -ltr lists oldest first, newest at the bottom) |
-R | --recursive | Lists all subdirectories recursively |
-d | --directory | Lists directory entries themselves, not their contents (e.g., ls -ld /var/log) |
-i | --inode | Prints the internal filesystem index number (inode) of each file |
-S | N/A | Sorts files by file size, largest first |
-1 | N/A | Lists one file per line (ideal for scripts and pipelines) |
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 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?
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?