Filesystem Fundamentals · #9 of 19
File Operations
touch, mkdir, rm, cp, mv
Creating Files and Directories
touch — Create Empty File (or Update Timestamp)
touch newfile.txt # Create empty file
touch file1.txt file2.txt # Create multiple files
touch existing.txt # Update modification time
mkdir — Make Directory
mkdir mydir # Create directory
mkdir -p a/b/c/d # Create parent directories too
mkdir dir1 dir2 dir3 # Create multiple directories
Copying
cp — Copy Files
cp source.txt dest.txt # Copy file
cp file.txt /path/to/ # Copy to directory
cp -r mydir/ backup/ # Copy directory recursively
cp -i file.txt backup/ # Ask before overwriting
cp -v *.txt backup/ # Verbose (show what's copied)
Important Flags
-ror-R— Recursive (for directories)-i— Interactive (confirm overwrites)-v— Verbose-p— Preserve permissions and timestamps
Moving and Renaming
mv — Move or Rename
mv oldname.txt newname.txt # Rename file
mv file.txt /path/to/ # Move to directory
mv -i source.txt dest.txt # Ask before overwriting
mv *.txt Documents/ # Move multiple files
mv dir1/ dir2/ # Rename directory
Moving and renaming are the same operation in Linux!
Deleting
rm — Remove Files
rm file.txt # Delete file
rm -r directory/ # Delete directory and contents
rm -f file.txt # Force (no confirmation)
rm -rf directory/ # Force delete directory (DANGEROUS)
rm -i *.txt # Ask for each file
rmdir — Remove Empty Directory
rmdir empty_dir # Only works if empty
Warning: rm is Permanent!
There’s no trash can. Files are gone forever.
# NEVER do this:
rm -rf / # Deletes everything
rm -rf ~ # Deletes your home
rm -rf * # Deletes current directory contents
Viewing File Contents
cat — Display Entire File
cat file.txt # Print file contents
cat file1.txt file2.txt # Concatenate files
head / tail — View Start or End
head file.txt # First 10 lines
head -n 20 file.txt # First 20 lines
tail file.txt # Last 10 lines
tail -n 20 file.txt # Last 20 lines
tail -f logfile # Follow (watch for new lines)
less — Scrollable Viewer
less file.txt # Scroll with arrows
# Press 'q' to quit
# Press '/' to search
# Press 'n' for next match
Symbolic Links
ln — Create Links
ln -s /path/to/original link_name # Create symlink
ls -l link_name # Shows -> pointing to original
Symlinks are like shortcuts. If you delete the original, the link breaks.
File Information
file — Determine File Type
file document.pdf # PDF document
file script.sh # Bourne-Again shell script
file /bin/ls # ELF executable
stat — Detailed File Info
stat file.txt # Size, permissions, timestamps, inode
du — Disk Usage
du -h file.txt # Size of file
du -sh directory/ # Size of directory
du -sh */ # Size of all subdirectories
Try It!
Practice file operations in the terminal:
Exercises:
- Create a directory:
mkdir practice - Create files:
touch practice/file1.txt practice/file2.txt - Copy a file:
cp practice/file1.txt practice/backup.txt - Rename:
mv practice/backup.txt practice/copy.txt - Delete the practice directory:
rm -r practice - View system files:
cat /etc/hostname
Try it in the terminal
A sandboxed shell with this lesson’s commands. Type help to see what’s available.