CentOS Directory and File Operations: A Practical Reference
Directory Tree Structure
The Linux file system follows the Filesystem Hierarchy Standard (FHS). Below are the main directories and their purposes:
- /bin – Essential user command binaries (e.g.,
ls,cat,mkdir). - /etc – Host-specific configuration files and system‑wide settings.
- /home – Personal directories for regular users. Each user gets a subdirectory here, such as
/home/username. - /usr – Secondary hierarchy for shareable, read‑only data. Key subdirectories include:
/usr/local– Locally installed software (system‑wide applications)./usr/bin– Non‑essential user commands./usr/sbin– Non‑essential system administration binaries./usr/lib– Object libraries for programs./usr/share– Architecture‑independent data (documentation, man pages, etc.).
- /opt – Add‑on appplication software packages (e.g., third‑party tools, Tomcat).
- /proc – Virtual filesystem exposing kernel and process information as files.
- /root – Home directory for the root user.
- /sbin – System administration commands used primarily by root (e.g.,
ifconfig,fdisk). - /dev – Device files representing hardware components.
- /mnt – Temporary mount point for filesystems.
- /boot – Files needed for the boot process (kernel, bootloader configuration).
- /lib – Essential shared libraries and kernel modules needed to boot the system.
- /tmp – Temporary files; often world‑writable.
- /var – Variable data such as logs, caches, spools, and transient files.
- /lost+found – Fragments recovered after a filesystem check; usually empty.
Path separators: Linux uses forward slashes (/) exclusively, while Windows accepts both / and \. URLs also use / as a separator because of their Unix origin.
Managing Directories
Creating Directories with mkdir
mkdir [-mp] directory_name
-msets the permission mode explicitly.-pcreates parent directories recursively.
$ cd /tmp
$ mkdir -m 750 project/docs/notes
# Fails when intermediate directories are missing
$ mkdir -m 750 -p project/docs/notes
# Succeeds, creating project/, project/docs/, and project/docs/notes/
Removing Empty Directories with rmdir
rmdir [-p] directory_name
-premoves each empty ancestor directory aswell.
$ rmdir test # succeeds if 'test' is empty
$ rmdir project # fails because project/ contains subdirectories
To delete non‑empty directories, use rm -rf directory_name.
Copying Files and Directories with cp
cp [-adfilprsu] source destination
cp source1 source2 ... target_directory
-r– recursive copy for directories.-i– prompt before overwriting.-p– preserve timestamps, ownership, permissions.
$ cp ~/.profile /tmp/profile_safe
$ cp -i ~/.profile /tmp/profile_safe
cp: overwrite '/tmp/profile_safe'? n
Moving and Renaming with mv
mv [-fiu] source destination
mv source1 source2 ... target_directory
-f– force overwrite without asking.-i– interactive confirmation before overwrite.
$ cp ~/.profile profile_copy
$ mkdir storage
$ mv profile_copy storage/ # move file into directory
$ mv storage archive # rename directory
Listing Directory Contents with ls
ls [options] [directory_name]
-a– include hidden entries.-l– long format (permissions, size, timestamp).
$ ls -la ~ # detailed view of all files in home directory
Notes: -p in mkdir/rmdir means ascend (recursive upward). -r in cp/mv means descend (recursive downward). -f forces an action and -i makes it interactive.
File Operations
Creating Files with touch
touch filename
If the file exists, its access and modification timestamps are updated to the current time. If the file does not exist, an empty file is created.
$ ls -l
-rw-r--r-- 1 user user 0 Mar 12 13:43 readme
$ touch readme # timestamp refreshed
$ touch newfile # creates empty newfile
$ ls -l
-rw-r--r-- 1 user user 0 Mar 12 14:06 newfile
-rw-r--r-- 1 user user 0 Mar 12 14:06 readme
Deleting Files and Directories with rm
rm [-fir] file_or_directory
-r– recursively delete directories and their contents.-f– ignore nonexistent files, never prompt.-i– confirm each deletion.
$ rm -i /tmp/sample.txt
rm: remove regular file ‘/tmp/sample.txt’? y
$ rm -rf /tmp/old_project # force‑delete non‑empty directory
Viewing File Contents with cat
- Display a file:
cat filename - Create a new file (ends with Ctrl+D):
cat > filename - Concatenate multiple files:
cat file1 file2 > combined
To display a file in reverse order (last line first), use tac.
Ownership and Permissions
Use ls -l or ll to inspect metadata. Each line shows:
- Type and permissions (e.g.,
-rw-r--r--) - Link count or subdirectory count
- Owner
- Group
- Size in bytes
- Modification date
- Name
Changing Ownership (chown)
chown [-R] owner:group file_or_directory
-Rapplies the change recursively.
# Change owner of script.sh to 'admin' and group to 'staff'
$ chown admin:staff script.sh
# Recursively change14own everything under /data/logs
$ chown -R logger:logger /data/logs
Changing Group (chgrp)
chgrp [-R] group file_or_directory
Changing Permissions (chmod)
Permissions can be set with numeric (octal) or symbolic notation. Each permission has a weight:
r(read) = 4w(write) = 2x(execute) = 1
These are applied to three categories: user (owner), group, and others.
Numeric method:
$ ls -l data.conf
-rw-r----- 1 root root 204 Apr 4 09:10 data.conf
$ chmod 640 data.conf # 6=rw- for user, 4=r-- for group, 0=--- for others
$ ls -l data.conf
-rw-r----- 1 root root 204 Apr 4 09:10 data.conf
Symbolic method:
Use u (user), g (group), o (others), a (all) with +, -, or = to adjust permission bits.
$ touch app.log
$ ls -l app.log
-rw-r--r-- 1 root root 0 Nov 15 10:32 app.log
$ chmod u=rwx,g=rx,o= app.log # set exact permissions
$ ls -l app.log
-rwxr-x--- 1 root root 0 Nov 15 10:32 app.log
# Remove execute permission for everyone
$ chmod a-x app.log
$ ls -l app.log
-rw-r----- 1 root root 0 Nov 15 10:32 app.log
Searching with find
The find command searches the filesystem with a rich set of criteria.
find [starting_path] [conditions] [action]
Common conditions:
-type f(regular file),d(directory),l(symlink)-name 'pattern'(supports globbing)-iname 'pattern'(case‑insensitive)-user tom,-group staff-perm 644-size +10M(larger than 10 MB)-mtime -7(modified within last 7 days)
Actions:
-print(default) – print matching filenames.-exec command {} \;– execute a command for each result.-ok command {} \;– like-execbut prompts for confirmation.
# Find all *.conf files under /etc and display them
$ find /etc -name '*.conf' -type f
#2Find and10delete log files older than 30 days
$ find /var/log -name '*.log.gz' -mtime +30 -exec rm -f {} \;
#alter8Search for8Python scripts and9show their contents safely
$ find projects/ -name '*.py' -ok cat {} \;
< cat ... projects/script.py > ? y
#!/usr/bin/env python3
...
The braces {} are placeholders for each found path, and the escaped semicolon \; terminates the command.