CIS126RH | RHEL System Administration 1 Mesa Community College
Learning Objectives
1
Create files and directories
Use touch, mkdir, and other methods to create new items
2
Copy files and directories
Duplicate files with cp, preserving attributes when needed
3
Move and rename files
Relocate and rename with mv command
4
Delete files and directories safely
Remove items with rm and rmdir, understanding the risks
5
Organize with wildcards and patterns
Use globbing to work with multiple files efficiently
Paths: Absolute vs Relative
Before managing files, understand how to specify their locations. Paths can be absolute (from root) or relative (from current directory).
# Absolute paths start from root (/)[user@host ~]$ cat /etc/hostname
[user@host ~]$ ls /var/log/messages
# Relative paths start from current directory[user@host ~]$ pwd
/home/student[user@host ~]$ cat Documents/notes.txt # Relative to /home/student# Special path symbols. Current directory.. Parent directory~ Home directory (/home/username)~bob Bob's home directory (/home/bob)# Examples using special symbols[user@host Documents]$ ls ../Downloads # Parent's Downloads[user@host /var/log]$ cp messages ~/ # Copy to home
Creating Files: touch
touch creates empty files or updates timestamps on existing files. It is the simplest way to create a new, empty file.
# Create a single empty file[user@host ~]$ touch newfile.txt
[user@host ~]$ ls -l newfile.txt
-rw-r--r--. 1 student student 0 Jan 20 10:00 newfile.txt# Create multiple files at once[user@host ~]$ touch file1.txt file2.txt file3.txt
# Create file in specific directory[user@host ~]$ touch Documents/report.txt
# If file exists, touch updates its timestamp[user@host ~]$ ls -l oldfile.txt
-rw-r--r--. 1 student student 100 Jan 15 08:00 oldfile.txt[user@host ~]$ touch oldfile.txt
[user@host ~]$ ls -l oldfile.txt
-rw-r--r--. 1 student student 100 Jan 20 10:05 oldfile.txt
Note: touch creates files with zero bytes. The file exists but contains nothing. Use editors or redirection to add content.
Creating Directories: mkdir
mkdir (make directory) creates new directories. Use -p to create parent directories as needed.
# Create a single directory[user@host ~]$ mkdir projects
[user@host ~]$ ls -ld projects
drwxr-xr-x. 2 student student 4096 Jan 20 10:00 projects# Create multiple directories[user@host ~]$ mkdir dir1 dir2 dir3
# Create nested directories - fails without parent[user@host ~]$ mkdir projects/web/frontend
mkdir: cannot create directory 'projects/web/frontend': No such file or directory# Use -p to create parent directories automatically[user@host ~]$ mkdir -p projects/web/frontend
[user@host ~]$ mkdir -p projects/web/backend
[user@host ~]$ mkdir -p projects/mobile/{ios,android}
# Verify with tree (if installed) or ls -R[user@host ~]$ ls -R projects
projects:
mobile web
projects/mobile:
android ios
projects/web:
backend frontend
Other Ways to Create Files
# Redirect output to create file with content[user@host ~]$ echo "Hello World" > greeting.txt
[user@host ~]$ cat greeting.txt
Hello World# Append to file (creates if doesn't exist)[user@host ~]$ echo "Another line" >> greeting.txt
# Create empty file with redirection[user@host ~]$ > emptyfile.txt
# Use cat with heredoc for multi-line content[user@host ~]$ cat > notes.txt << EOF
Line 1
Line 2
Line 3EOF# Copy from another file[user@host ~]$ cp /etc/hostname myhostname.txt
# Use text editors (creates on save)[user@host ~]$ vim newdocument.txt
[user@host ~]$ nano newdocument.txt
Tip:> overwrites existing content. >> appends. Be careful with > on important files!
Copying Files: cp Basics
cp (copy) duplicates files and directories. The original remains unchanged; a new copy is created.
cp [options] sourcedestination
# Copy file to new name (same directory)[user@host ~]$ cp report.txt report_backup.txt
# Copy file to different directory (keeps name)[user@host ~]$ cp report.txt Documents/
# Copy file to different directory with new name[user@host ~]$ cp report.txt Documents/quarterly_report.txt
# Copy multiple files to a directory[user@host ~]$ cp file1.txt file2.txt file3.txt Documents/
# Warning: copying to existing file overwrites silently![user@host ~]$ cp newdata.txt olddata.txt # olddata.txt is replaced!# Use -i for interactive (prompts before overwrite)[user@host ~]$ cp -i newdata.txt olddata.txt
cp: overwrite 'olddata.txt'? y
Copying Directories
# Copying a directory requires -r (recursive)[user@host ~]$ cp projects backup_projects
cp: -r not specified; omitting directory 'projects'# Use -r to copy directory and all contents[user@host ~]$ cp -r projects backup_projects
# Copy directory into another directory[user@host ~]$ cp -r projects /tmp/
# Result: /tmp/projects/...# Common options for directory copying[user@host ~]$ cp -rv projects backup_projects # verbose output'projects' -> 'backup_projects'
'projects/file1.txt' -> 'backup_projects/file1.txt'
...# Preserve permissions, ownership, timestamps[user@host ~]$ cp -rp projects backup_projects
# Archive mode (-a): preserve everything, recursive[root@host ~]# cp -a /etc /backup/etc_backup
Important: Without -r, cp silently skips directories. Always use -r for directory copies.
Moving and Renaming: mv
mv (move) relocates files and directories. It also serves as the rename command - moving to the same directory with a new name.
mv [options] sourcedestination
# Rename a file (move to same directory, different name)[user@host ~]$ mv oldname.txt newname.txt
# Move file to different directory[user@host ~]$ mv report.txt Documents/
# Move and rename simultaneously[user@host ~]$ mv draft.txt Documents/final_report.txt
# Move multiple files to directory[user@host ~]$ mv file1.txt file2.txt file3.txt archive/
# Rename a directory[user@host ~]$ mv old_project new_project
# Move directory to new location[user@host ~]$ mv projects /tmp/
Key difference:cp creates a copy (original remains). mv relocates (original is gone from source).
mv Options and Safety
# Like cp, mv overwrites without warning by default![user@host ~]$ mv newfile.txt existingfile.txt # existingfile.txt is GONE# Use -i for interactive (prompts before overwrite)[user@host ~]$ mv -i newfile.txt existingfile.txt
mv: overwrite 'existingfile.txt'? n# Use -n to never overwrite[user@host ~]$ mv -n source.txt destination.txt
# Silently does nothing if destination exists# Use -v for verbose output[user@host ~]$ mv -v projects/ archive/
renamed 'projects/' -> 'archive/projects'# Backup before overwriting[user@host ~]$ mv --backup=numbered important.txt Documents/
# Creates important.txt.~1~ if Documents/important.txt existed
Critical: mv can destroy data instantly. The overwritten file is not recoverable. Use -i when unsure.
Deleting Files: rm
rm (remove) permanently deletes files. There is no trash can or recycle bin - deleted files are gone.
# Remove a single file[user@host ~]$ rm unwanted.txt
# Remove multiple files[user@host ~]$ rm file1.txt file2.txt file3.txt
# Interactive mode - confirm each deletion[user@host ~]$ rm -i important.txt
rm: remove regular file 'important.txt'? y# Force removal (no prompts, no errors for missing files)[user@host ~]$ rm -f maybe_exists.txt
# Verbose - show what's being deleted[user@host ~]$ rm -v old_file.txt
removed 'old_file.txt'
Warning:rm is permanent. There is no undo. Double-check your command before pressing Enter.
Deleting Directories
# rmdir removes empty directories only[user@host ~]$ rmdir empty_folder
[user@host ~]$ rmdir folder_with_files
rmdir: failed to remove 'folder_with_files': Directory not empty# rm -r removes directories recursively (including contents)[user@host ~]$ rm -r old_project
# Deletes old_project and everything inside it# rm -ri for interactive recursive deletion[user@host ~]$ rm -ri old_project
rm: descend into directory 'old_project'? y
rm: remove regular file 'old_project/file.txt'? y
rm: remove directory 'old_project'? y# rm -rf - the "dangerous" combination[user@host ~]$ rm -rf old_project
# Removes everything, no prompts, no errors
DANGER:rm -rf is extremely powerful. A typo like rm -rf / or rm -rf ~/* can destroy your system or all your files. Always verify the path!
Wildcards: Globbing
Wildcards (glob patterns) let you match multiple files by pattern. The shell expands them before the command runs.
Pattern
Matches
Example
*
Any characters (zero or more)
*.txt matches all .txt files
?
Any single character
file?.txt matches file1.txt, fileA.txt
[abc]
Any one character from the set
file[123].txt matches file1.txt, file2.txt
[a-z]
Any one character in the range
file[a-c].txt matches filea.txt, fileb.txt
[!abc]
Any character NOT in the set
file[!0-9].txt matches fileA.txt, not file1.txt
# List all .txt files[user@host ~]$ ls *.txt
# Match files with specific patterns[user@host ~]$ ls report_202?.pdf # report_2021.pdf, report_2024.pdf[user@host ~]$ ls image[1-5].jpg # image1.jpg through image5.jpg
Using Wildcards Effectively
# Copy all .txt files to backup directory[user@host ~]$ cp *.txt backup/
# Move all log files from 2024[user@host ~]$ mv *2024*.log archive/
# Delete all .tmp files[user@host ~]$ rm *.tmp
# List files matching complex pattern[user@host ~]$ ls report_[0-9][0-9][0-9][0-9].pdf # 4-digit year# SAFETY: Preview before destructive operations![user@host ~]$ ls *.bak # See what matchesfile1.bak file2.bak oldstuff.bak[user@host ~]$ rm *.bak # Now delete (if correct)# Echo shows expansion without executing[user@host ~]$ echo rm *.tmp
rm file1.tmp file2.tmp temp.tmp
Best practice: Before rm with wildcards, use ls with the same pattern to verify what matches!
Brace Expansion
Brace expansion generates multiple text strings. Unlike wildcards, it does not match existing files - it creates strings.
# Create multiple directories at once[user@host ~]$ mkdir -p project/{src,docs,tests,build}
[user@host ~]$ ls project/
build docs src tests# Create numbered directories[user@host ~]$ mkdir chapter{1..5}
[user@host ~]$ ls
chapter1 chapter2 chapter3 chapter4 chapter5# Create backup with date suffix[user@host ~]$ cp config.txt{,.bak}
# Expands to: cp config.txt config.txt.bak# Create multiple files with pattern[user@host ~]$ touch file{a..e}.txt
[user@host ~]$ ls *.txt
filea.txt fileb.txt filec.txt filed.txt filee.txt# Nested expansion[user@host ~]$ mkdir -p {dev,staging,prod}/{app,db,cache}
# Creates 9 directories: dev/app, dev/db, dev/cache, staging/app, ...
Working with Hidden Files
Files starting with a dot (.) are hidden. They do not appear in normal ls output and wildcards do not match them by default.
# List including hidden files[user@host ~]$ ls -a
. .. .bashrc .bash_history Documents file.txt# Wildcard * does NOT match hidden files[user@host ~]$ ls *
Documents file.txt# To include hidden files, be explicit[user@host ~]$ ls .[!.]* *
# .[!.]* matches dot files but not . and ..# Copy all files including hidden[user@host ~]$ cp -r source/. destination/
# The trailing /. copies contents including hidden files# Move hidden files[user@host ~]$ mv .config .config.bak # Rename hidden dir# Be careful with hidden files and rm![user@host ~]$ rm -rf .* # DANGER: May match .. (parent!)[user@host ~]$ rm -rf .[!.]* # Safer: excludes . and ..