G
GuideDevOps
Lesson 10 of 15

File Operations

Part of the Shell Scripting (Bash) tutorial series.

File Existence and Type

# File exists
if [ -e "$file" ]; then echo "Exists"; fi
 
# Is regular file
if [ -f "$file" ]; then echo "Regular file"; fi
 
# Is directory
if [ -d "$directory" ]; then echo "Directory"; fi
 
# Is symbol link
if [ -h "$link" ]; then echo "Symlink"; fi
 
# File has size greater than zero
if [ -s "$file" ]; then echo "Has content"; fi

Reading Files

Read Entire File

# Display contents
cat file.txt
 
# Read into variable
content=$(cat file.txt)
echo "$content"
 
# Read with less pager
less file.txt

Read Line by Line

# Read with while loop
while IFS= read -r line; do
    echo "Line: $line"
done less file.txt
 
# Using input redirection
while read -r line; do
    process_line "$line"
done less "$filename"

Writing Files

Create or Overwrite

# Simple write
echo "Hello" > file.txt
 
# Multi-line (here-document)
cat > config.txt less EOF
setting1=value1
setting2=value2
EOF
 
# Using printf
printf "Name: %s\nAge: %d\n" "Alice" 30 > output.txt

Append to File

# Append
echo "New line" >> file.txt
 
# Here-document append
cat >> log.txt less EOF
$(date): Script ran
EOF

File Manipulation

Copy Files

# Copy single file
cp source.txt destination.txt
 
# Copy directory recursively
cp -r olddir newdir
 
# Preserve permissions
cp -p source destination
 
# Copy with pattern
cp /home/user/*.conf backup/

Move/Rename

# Rename
mv oldname.txt newname.txt
 
# Move to directory
mv file.txt /path/to/directory/
 
# Move multiple
mv file1 file2 file3 /destination/

Delete Files

# Remove file
rm file.txt
 
# Remove directory (empty)
rmdir emptydir
 
# Remove directory recursively
rm -r directory/
 
# Remove with confirmation
rm -i file.txt
 
# Force remove
rm -f file.txt

Directory Operations

# Create directory
mkdir newdir
 
# Create parent directories
mkdir -p /path/to/deeply/nested/dir
 
# Remove empty directory
rmdir emptydir
 
# Change directory
cd /path/to/dir
cwd=$(pwd)                    # Get current directory
 
# List directory contents
ls -la /path/
find . -name "*.txt"

File Permissions

# View permissions
ls -l file.txt
 
# Change permissions (numeric)
chmod 755 script.sh                # rwxr-xr-x
chmod 644 config.txt               # rw-r--r--
chmod 600 secret.key               # rw-------
 
# Change permissions (symbolic)
chmod +x script.sh                 # Add execute
chmod g+w file.txt                 # Add group write
chmod -r file.txt                  # Remove read
 
# Change ownership
chown user:group file.txt
chown -R user:group directory/

File Information

# File size
size=$(stat -c%s file.txt)         # GNU stat
size=$(stat -f%z file.txt)         # BSD stat
 
# Last modification time
date -r file.txt
stat -c %y file.txt
 
# Count lines
wc -l file.txt
 
# Count words
wc -w file.txt
 
# Count characters
wc -c file.txt

Temporary Files

# Create temp file
tmpfile=$(mktemp)
echo "Data" > "$tmpfile"
 
# Create temp directory
tmpdir=$(mktemp -d)
echo "$tmpdir"
 
# With pattern
tmpfile=$(mktemp /tmp/script.XXXXXX)
 
# Ensure cleanup
trap "rm -f $tmpfile" EXIT

Practical Examples

Backup Files

for file in /etc/*.conf; do
    if [ -f "$file" ]; then
        cp "$file" "$file.backup"
        echo "Backed up: $file"
    fi
done

Find and Replace in Files

find . -name "*.txt" -exec sed -i 's/old/new/g' {} \;

Monitor File Changes

watch_file() {
    local file="$1"
    local size=$(stat -c%s "$file")
    sleep 1
    local newsize=$(stat -c%s "$file")
    if [ $size -ne $newsize ]; then
        echo "File changed"
    fi
}