RED HAT ENTERPRISE LINUX

Managing Files
from the Command Line

Create, Copy, Move, and Delete Files and Directories

CIS126RH | RHEL System Administration 1
Mesa Coomunity College

Learning Objectives

1
Create files and directories

Use touch, mkdir, and text editors to create new items

2
Copy files and directories

Master the cp command with its essential options

3
Move and rename items

Use mv for moving and renaming files and directories

4
Delete files and directories safely

Remove items with rm and rmdir while avoiding disasters

File Management Commands

touch

Create empty files or update timestamps

mkdir

Create directories

cp

Copy files and directories

mv

Move or rename files and directories

rm

Remove files and directories

rmdir

Remove empty directories only

Key Principle: These commands work silently on success. No output means the operation succeeded.

Creating Files: touch

touch [options] filename(s)
# Create a single empty file
touch newfile.txt

# Create multiple files at once
touch file1.txt file2.txt file3.txt

# Create file with spaces in name (quote it)
touch "my document.txt"

# Update timestamp of existing file
touch existingfile.txt

# Verify the files were created
ls -l newfile.txt
-rw-r--r--. 1 student student 0 Dec  7 10:30 newfile.txt
Note: If the file exists, touch updates its modification timestamp. If it doesn't exist, touch creates an empty (0 byte) file.

Creating Directories: mkdir

mkdir [options] directory(ies)
# Create a single directory
mkdir projects

# Create multiple directories
mkdir docs scripts logs

# Create nested directories (parent path must exist)
mkdir projects/webapp          # Only works if projects/ exists

# Create nested directories including parents (-p)
mkdir -p projects/webapp/src/main

# Create with specific permissions
mkdir -m 700 private

# Verbose output
mkdir -v newdir
mkdir: created directory 'newdir'
Pro Tip: Always use mkdir -p when creating nested paths - it creates missing parents and doesn't error if directory exists.

Other Ways to Create Files

# Using text editors (creates file when saved)
vim newfile.txt
nano newfile.txt

# Using output redirection
echo "Hello World" > greeting.txt     # Create/overwrite
echo "More text" >> greeting.txt      # Append

# Create file with heredoc
cat > config.txt << EOF
Setting1=value1
Setting2=value2
EOF

# Copy from another file
cp /etc/hosts myhosts

# Download from the web
curl -o file.txt https://example.com/data.txt
Redirection: > creates/overwrites the file, >> appends to existing content.

Copying Files: cp

cp [options] source destination
# Copy file to new name (same directory)
cp original.txt backup.txt

# Copy file to different directory (keeps name)
cp report.txt /home/student/documents/

# Copy file to different directory with new name
cp report.txt /home/student/documents/report_backup.txt

# Copy multiple files to a directory
cp file1.txt file2.txt file3.txt /backup/

# Copy with verbose output
cp -v config.txt config.bak
'config.txt' -> 'config.bak'
report.txt
/backup/

cp Options

Option Purpose When to Use
-r, -R Recursive - copy directories Required for copying directories
-i Interactive - prompt before overwrite Protect against accidental overwrites
-v Verbose - show files being copied Confirm operations, debugging
-p Preserve mode, ownership, timestamps Backups, maintaining file attributes
-a Archive - preserve all (-pR + more) Complete backups, cloning directories
-u Update - copy only when source is newer Incremental backups, syncing
-n No clobber - don't overwrite existing Safest copy mode

Copying Directories

# Copy directory (MUST use -r)
cp -r projects/ projects_backup/

# Copy directory - ERROR without -r
cp projects/ backup/
cp: -r not specified; omitting directory 'projects/'

# Archive copy (preserves everything)
cp -a /var/www/ /backup/www_backup/

# Copy directory contents INTO existing directory
cp -r projects/* /backup/projects/

# Copy preserving all attributes
cp -rp source_dir/ dest_dir/

# Verbose recursive copy
cp -rv configs/ configs_backup/
'configs/app.conf' -> 'configs_backup/app.conf'
'configs/db.conf' -> 'configs_backup/db.conf'
⚠ Trailing Slash Matters: cp -r dir /dest/ copies dir INTO dest. cp -r dir/* /dest/ copies CONTENTS of dir into dest.

Moving and Renaming: mv

mv [options] source destination
# Rename a file (same directory)
mv oldname.txt newname.txt

# Move file to different directory
mv report.txt /home/student/documents/

# Move and rename simultaneously
mv draft.txt /final/published_report.txt

# Move multiple files to a directory
mv file1.txt file2.txt file3.txt /archive/

# Move a directory (no -r needed!)
mv projects/ /home/student/work/

# Verbose move
mv -v config.old config.new
renamed 'config.old' -> 'config.new'
Key Difference from cp: mv doesn't need -r for directories - it moves the entire directory in one operation.

mv Options

# Interactive - prompt before overwrite
mv -i source.txt destination.txt
mv: overwrite 'destination.txt'? y

# No clobber - never overwrite
mv -n source.txt destination.txt

# Force - override -i, never prompt
mv -f source.txt destination.txt

# Verbose - show what's happening
mv -v *.log /var/log/archive/
'app.log' -> '/var/log/archive/app.log'
'error.log' -> '/var/log/archive/error.log'

# Update - move only if source is newer
mv -u updated.conf /etc/

# Backup existing before overwrite
mv --backup=numbered file.txt /dest/
# Creates /dest/file.txt.~1~ if file.txt exists