Przejdลบ do treล›ci

๐Ÿ—‚ Extended Filesystem Operations

Shell scripts frequently manipulate files and directories. This section covers safe, efficient filesystem operations.

๐Ÿงญ Common Operations

Creating Directories

1
2
3
4
5
6
7
8
# Single directory
mkdir /path/to/dir

# Nested directories (no error if exists)
mkdir -p /path/to/nested/dir

# With permissions
mkdir -m 750 /path/to/secure

Copying Files

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Simple copy
cp source.txt dest.txt

# Recursive copy
cp -r dir1 dir2

# Preserve attributes
cp -a source dest   # Archive mode (recursive + preserve all)

# Interactive (prompt before overwrite)
cp -i source dest

# Verbose
cp -v source dest

Moving/Renaming

1
2
3
mv old_name new_name
mv file.txt /new/location/
mv -i old new   # Interactive

Removing

1
2
3
4
5
rm file.txt              # Single file
rm -r directory/         # Recursive delete
rm -rf directory/        # Force recursive (dangerous!)
rm -i file.txt           # Interactive prompt
rm -v file.txt           # Verbose

โš ๏ธ Never use rm -rf with variables without validation!


๐Ÿงช Safe Deletion Patterns

Validate Before Delete

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
target="/path/to/delete"

if [ -z "$target" ]; then
    echo "Error: target is empty" >&2
    exit 1
fi

if [ "$target" = "/" ]; then
    echo "Error: cannot delete root" >&2
    exit 1
fi

rm -rf -- "$target"

The -- prevents options from being interpreted if filename starts with -.


Delete Only Matching Files

1
2
3
4
5
6
7
8
# Delete only .tmp files
find /tmp -name "*.tmp" -type f -delete

# Delete files older than 7 days
find /var/log -name "*.log" -mtime +7 -delete

# Delete empty directories
find /tmp -type d -empty -delete

๐Ÿง  Permissions and Ownership

Changing Permissions

1
2
3
chmod 755 script.sh        # rwxr-xr-x
chmod u+x,g-w,o-r file     # Add execute, remove write, remove read
chmod 644 file.txt         # rw-r--r--

Numeric format: | Digit | Owner | Group | Others | |-------|-------|-------|--------| | 7 | rwx | rwx | rwx | | 6 | rw- | rw- | rw- | | 5 | r-x | r-x | r-x | | 4 | r-- | r-- | r-- |


Changing Ownership

1
2
3
chown user:group file.txt
chown -R user:group directory/
chgrp group file.txt

๐Ÿงช Path Manipulation

Extract Components

1
2
3
4
5
path="/home/user/documents/file.txt"

dirname "$path"   # /home/user/documents
basename "$path"  # file.txt
basename "$path" .txt  # file (strip extension)

Real Paths

1
2
realpath ../relative/path   # /full/resolved/path
readlink -f symlink         # Follow symlinks

Building Paths Safely

1
2
3
4
5
6
7
8
# โŒ Unsafe (spaces break it)
path="$base/$filename"

# โœ… Safe
path="${base%/}/${filename#./}"   # Handle leading/trailing slashes

# Or use printf
path=$(printf '%s/%s' "$base" "$filename")

๐Ÿง  Temporary Files and Directories

Secure Temporary Files

1
2
3
4
5
6
7
# Create secure temp file
tmpfile=$(mktemp)
trap "rm -f $tmpfile" EXIT

echo "data" > "$tmpfile"
process "$tmpfile"
# Automatically cleaned up on exit

Temporary Directories

1
2
3
4
5
tmpdir=$(mktemp -d)
trap "rm -rf $tmpdir" EXIT

cd "$tmpdir"
# Do work...

๐Ÿงช File Testing

Test Meaning Example
-e Exists [ -e file.txt ]
-f Regular file [ -f file.txt ]
-d Directory [ -d /tmp ]
-r Readable [ -r file.txt ]
-w Writable [ -w file.txt ]
-x Executable [ -x script.sh ]
-s Size > 0 [ -s file.txt ]
-L Symbolic link [ -L link ]
-nt Newer than [ f1 -nt f2 ]
-ot Older than [ f1 -ot f2 ]

๐Ÿงพ Summary

  • Always validate paths before operations.
  • Use -- to prevent option injection.
  • Prefer find -delete over rm -rf for safety.
  • Use mktemp for temporary files.
  • Trap cleanup to avoid leftover temp files.
  • Test file properties before acting.

๐Ÿ‘‰ Continue to: Text Processing