๐ Extended Filesystem Operations
Shell scripts frequently manipulate files and directories. This section covers safe, efficient filesystem operations.
๐งญ Common Operations
Creating Directories
| # 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
| mv old_name new_name
mv file.txt /new/location/
mv -i old new # Interactive
|
Removing
| 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
| # 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
| 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
| chown user:group file.txt
chown -R user:group directory/
chgrp group file.txt
|
๐งช Path Manipulation
| path="/home/user/documents/file.txt"
dirname "$path" # /home/user/documents
basename "$path" # file.txt
basename "$path" .txt # file (strip extension)
|
Real Paths
| realpath ../relative/path # /full/resolved/path
readlink -f symlink # Follow symlinks
|
Building Paths Safely
| # โ 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
| # Create secure temp file
tmpfile=$(mktemp)
trap "rm -f $tmpfile" EXIT
echo "data" > "$tmpfile"
process "$tmpfile"
# Automatically cleaned up on exit
|
Temporary Directories
| 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