File Management
This section covers core Linux file management operations, archiving and compression with tar and gzip, soft and hard link behavior, mount options, and directory navigation utilities essential for RHCSA preparation.
Theory Questions
1. Which directory stores dynamically created temporary files cleared on reboot?
The /tmp directory is used for temporary files that do not need to persist across system reboots.
- Command(s):
ls -ld /tmp - Why: The system automatically cleans
/tmpduring boot sequences or mounts it as a temporary file system (tmpfs). - Gotchas: Do not confuse
/tmpwith/var/tmp, which preserves files across system reboots. - Verify:
df -hT /tmp
2. Which directory is dedicated to storing system log files?
System and application logs are stored in /var/log.
- Command(s):
ls -ld /var/log - Why:
/var/logserves as the centralized location for standard system log files and historical log archives. - Gotchas: Modern RHEL systems use
systemd-journaldbinaries stored in/var/log/journal/, which are best queried usingjournalctl. - Verify:
ls -l /var/log
3. How do you apply security mount options to a dedicated /home partition?
Remount the file system with nodev, nosuid, and noexec flags, and append these options to /etc/fstab.
- Command(s):
mount -o remount,nodev,nosuid,noexec /home - Why:
nodevprevents interpreting character/block devices,nosuidignores set-user-identifier bits, andnoexecblocks binary execution on user partitions. - Gotchas: Omitting changes in
/etc/fstabcauses remounted security parameters to revert upon reboot. - Verify:
findmnt /home
4. How do you get an accurate overview of mounted block storage devices?
Use lsblk to display physical and logical block devices in a visual tree format.
- Command(s):
lsblk - Why:
lsblklists physical block devices while filtering out virtual filesystems (such asproc,sysfs, andtmpfs). - Gotchas:
df -hshows only active file system usage and omits unformatted or unmounted block devices. - Verify:
lsblk -f
5. How do you list directory contents with the newest files listed last?
Sort by time (-t) and reverse the sort order (-r) using ls -tr.
- Command(s):
ls -tr - Why:
-torders files by modification timestamp (newest first), while-rreverses this order, putting the newest entries at the bottom of the terminal output. - Gotchas: Forgetting
-routputs the newest files at the top, which can scroll off-screen in dense directories. - Verify:
ls -ltr
6. How do you copy all regular and hidden files from /home/$USER to the current directory?
Target the directory contents using dot notation while maintaining attributes via -a.
- Command(s):
cp -a /home/$USER/. . - Why:
/home/$USER/.captures all contents, including hidden dotfiles (.bashrc,.config), while-apreserves ownership and permissions. - Gotchas: Using shell globbing wildcard
*ignores hidden dotfiles. Usingcp -r /home/$USER .creates a nested folder instead of copying contents directly. - Verify:
ls -la
7. How do you rename a file from myfile to mynewfile?
Use the mv command to rename files within the same file system directory.
- Command(s):
mv myfile mynewfile - Why:
mvchanges the path reference or directory entry name of the target file. - Gotchas:
mvwill overwrite an existing destination file without prompting unless-i(interactive mode) is specified. - Verify:
ls -l mynewfile
8. What characteristics distinguish hard links from symbolic links?
Hard links point directly to an existing inode on disk, whereas symbolic links point to a path string.
- Command(s):
ls -i file1 file2 - Why: Hard links share identical inode numbers and underlying data blocks. Deleting one hard link does not erase file data until all hard links to that inode are removed.
- Gotchas: Hard links cannot span across different file systems/partitions and cannot target directories.
- Verify:
stat file1 file2
9. How do you create a symbolic link to /home inside /tmp?
Use ln -s specifying the target destination path followed by the link path name.
- Command(s):
ln -s /home /tmp/home_link - Why:
-screates a soft (symbolic) link containing a text reference pointing to/home. - Gotchas: Ensure target paths use absolute references to prevent broken links if the link file is moved later.
- Verify:
ls -l /tmp/home_link
10. How do you update or append files to an existing tar archive?
Use the append (-r) flag on uncompressed archives.
- Command(s):
tar -rvf archive.tar newfile - Why:
-rappends new files to the end of an uncompressed.tararchive. - Gotchas: Appending files directly to compressed archives (
.tar.gz,.tar.xz) is unsupported. You must unpack, add, and re-compress the archive. - Verify:
tar -tf archive.tar
11. Which system directory contains system-wide configuration files?
System-wide administrative configuration files reside in /etc.
- Command(s):
ls -ld /etc - Why:
/etcholds essential configuration files, startup scripts, and service parameters. - Gotchas: Binaries and executables should not be stored in
/etc; they belong in/usr/binor/usr/sbin. - Verify:
ls /etc
12. How do you list directory files sorted by modification time, newest first?
Sort output using ls -t.
- Command(s):
ls -t - Why: The
-toption sorts directory entries by modification timestamp in descending order. - Gotchas: Adding
-rwill reverse the display order, showing the oldest entries first. - Verify:
ls -lt
13. How do you rename a file from myfile to yourfile?
Rename the filename using the mv utility.
- Command(s):
mv myfile yourfile - Why:
mvupdates the filename entry within the file system directory structure. - Gotchas: If
yourfilealready exists,mvreplaces it without warning unless-iis used. - Verify:
ls -l yourfile
14. How do you recursively delete a directory structure and all of its contents?
Execute rm -rf specifying the target directory path.
- Command(s):
rm -rf /path/to/dir - Why:
-rprocesses directory trees recursively, and-fbypasses confirmation prompts and ignores non-existent files. - Gotchas: Double-check path parameters before pressing enter. Accidental execution on root
/or system folders can destroy operating system data. - Verify:
ls /path/to/dir
15. How do you create a symbolic link to /tmp in your user home directory?
Use ln -s utilizing tilde (~) expansion to reference the home directory path.
- Command(s):
ln -s /tmp ~/tmp_link - Why:
~expands dynamically to the currently logged-in user’s home path (/home/$USER). - Gotchas: Running this command under
sudoexpands~to/rootinstead of your standard user account home directory. - Verify:
ls -l ~/tmp_link
16. How do you copy all files starting with a, b, or c from /etc to the current directory?
Match specific leading characters using square bracket globbing patterns [abc]*.
- Command(s):
cp -r /etc/[abc]* . - Why:
[abc]*expands to match any file or folder name starting with ‘a’, ‘b’, or ‘c’. The-rflag ensures matched directories are copied recursively. - Gotchas: Omitting
*matches only exact single-character names nameda,b, orc. - Verify:
ls -d [abc]*
17. How do you create a symbolic link to /etc in your home directory?
Create a soft link pointing to /etc inside ~.
- Command(s):
ln -s /etc ~/etc_link - Why:
-screates a symbolic link pointing to the/etctarget path. - Gotchas: Ensure the destination filename is provided to avoid link name ambiguity.
- Verify:
ls -l ~/etc_link
18. How do you safely remove a symbolic link pointing to a directory?
Remove the link using rm or unlink without appending a trailing slash.
- Command(s):
rm symlink_name(orunlink symlink_name) - Why: Targeting
symlink_namedirectly unlinks the reference file without modifying the target directory contents. - Gotchas: Adding a trailing slash (
rm symlink_name/) targets the contents inside the target directory rather than the symlink file itself. - Verify:
ls -l symlink_name
19. How do you create a gzip-compressed tar archive of /etc and /home at /tmp/etchome.tgz?
Combine tar creation options with gzip compression using -czvf.
- Command(s):
tar -czvf /tmp/etchome.tgz /etc /home - Why:
-ccreates a new archive,-zcompresses viagzip,-vshows process output, and-fnames the target archive file. - Gotchas: The
-fflag must immediately precede the archive target output path parameter. - Verify:
tar -tzvf /tmp/etchome.tgz | head
20. How do you extract a single specific file from a compressed tar archive?
Pass the archive file and the precise internal path to tar -xzvf.
- Command(s):
tar -xzvf archive.tgz etc/passwd - Why:
tarinspects the archive index and extracts only the specified path argument. - Gotchas: Archives store relative file paths stripping the leading slash (e.g.,
etc/passwdinstead of/etc/passwd). Check internal paths first usingtar -tf. - Verify:
ls -l etc/passwd
Lab Tasks
File Management Practice
Perform end-to-end file management, archive building, link generation, and compression workflows.
Configuration Steps
# 1. Elevate session privilege to root shell
sudo -i
# 2. Build uncompressed tar archive containing /home and /etc
tar -cvf /root/essentials.tar /home /etc
# 3. Copy archive file to temporary storage directory
cp /root/essentials.tar /tmp/
# 4. Create a hard link pointing to essentials.tar at system root
ln /tmp/essentials.tar /essentials.tar
# 5. Rename hard link file reference
mv /essentials.tar /archive.tar
# 6. Create symbolic soft link in root home pointing to /archive.tar
ln -s /archive.tar /root/link.tar
# 7. Remove target archive file and verify dangling symlink behavior
rm /archive.tar
ls -l /root/link.tar
# 8. Clean up dangling soft link entry
rm /root/link.tar
# 9. Apply gzip compression to original root archive file
gzip /root/essentials.tar
-
Why:
-
tar -cvfaggregates file directories into a consolidated uncompressed stream. -
lngenerates hard links pointing to the identical inode number on the filesystem. -
mvupdates path references without altering underlying inode data. -
rmon a target file causes symlinks pointing to its path to break (dangle). -
gzipcompresses.tararchives, replacing the source file with a.tar.gzextension. -
Gotchas:
-
Hard links must reside on the same filesystem. If
/tmpresides on an isolated mount point separate from/,lnwill fail. -
gzipremoves the original uncompressed file upon successful completion unless-k(keep) is supplied.
Verification Procedures
Confirm archive creation, file size changes, and compression outputs.
# Verify resulting compressed archive file status
ls -lh /root/essentials.tar.gz
# Inspect contents of the compressed tar file
tar -tzvf /root/essentials.tar.gz | head