Chapter 3: File Ninja

🥷 Chapter 3: File Ninja — File & Directory Management

⭐ XP: 400 | 🏆 Badge: File Wizard

Core Commands

ls -la          # List all files with permissions
cd /path        # Change directory
pwd             # Print working directory
mkdir -p a/b/c  # Create nested directories
touch file.txt  # Create empty file
cp -r src/ dst/ # Copy recursively
mv old new      # Move/rename
rm -rf dir/     # Remove directory (DANGEROUS - no undo!)
find / -name '*.log' -type f  # Find files
locate filename  # Fast search (uses database)

Viewing Files

cat file.txt     # Show entire file
less file.txt    # Paginate through file (q to quit)
head -20 file    # First 20 lines
tail -f /var/log/syslog  # Follow live log
grep 'pattern' file      # Search inside file
grep -r 'term' /etc/     # Recursive search

File Permissions

# rwxrwxrwx = owner/group/other
chmod 755 script.sh      # rwxr-xr-x
chmod u+x script.sh      # Add execute for owner
chown user:group file    # Change ownership
chown -R user:group dir/ # Recursive ownership change
Octal Symbolic Meaning
7 rwx read+write+execute
6 rw- read+write
5 r-x read+execute
4 r– read only
0 no permissions

Pipes and Redirection

command > file.txt    # Redirect stdout (overwrite)
command >> file.txt   # Append stdout
command 2> error.log  # Redirect stderr
command | grep term   # Pipe to grep
command | tee file    # Output to screen AND file

Archive and Compression

tar -czf backup.tar.gz /dir/   # Create compressed archive
tar -xzf backup.tar.gz         # Extract archive
tar -tzf backup.tar.gz         # List contents without extracting
zip -r archive.zip /dir/       # Create zip
unzip archive.zip               # Extract zip

Lab 3.1 — Directory Structure Setup

mkdir -p ~/project/{src,docs,tests,logs}
touch ~/project/src/{main.sh,config.conf}
ls -laR ~/project/
find ~/project -name '*.conf'

Lab 3.2 — Permission Workshop

mkdir ~/perm-lab
cd ~/perm-lab
touch private.txt shared.txt public.txt
chmod 600 private.txt   # Owner read/write only
chmod 664 shared.txt    # Owner rw, group r, others r
chmod 644 public.txt    # Standard web file permissions
ls -l

📋 Certification Notes (Linux+ / LPIC-1)

  • Understand octal AND symbolic permission notation
  • Know sticky bit (chmod +t /tmp), SUID, SGID
  • find with -perm, -user, -mtime flags are exam favourites
  • Hard links vs symbolic links: ln file link vs ln -s file link