03 - Files and Directories¶
What this session is¶
About 30 minutes. You'll learn to create, copy, move, rename, and delete files and directories.
Create a directory: mkdir¶
mkdir creates a directory. mkdir -p path/to/nested creates intermediate directories as needed:
That creates all four (practice, a, b, c).
Create an empty file: touch¶
touch creates an empty file if it doesn't exist. If it does exist, touch updates its modification time but doesn't change the content. Useful for "make sure this file exists."
Copy: cp¶
cp source destination
cp hello.txt hello2.txt # copy file to file
cp hello.txt ~/Documents/ # copy file into a directory (keeps name)
cp -r mydir mydir2 # copy directory recursively
-r (recursive) is required when copying directories.
Move and rename: mv¶
mv is both "move" and "rename" - same command, depending on context:
mv old.txt new.txt # rename
mv hello.txt ~/Documents/ # move into directory
mv ~/Documents/foo.txt . # move from there to here ('.' = current)
mv dir1 dir2 # rename or move directory
No -r needed for mv - directories are moved in place.
Delete: rm¶
rm hello.txt # delete a file
rm file1 file2 file3 # delete multiple
rm -r mydir # delete a directory and everything in it (recursive)
rm -f file # force - no errors if file doesn't exist
rm -rf path # recursive + force - combine carefully
The most dangerous command in Linux
rm -rf / (with a typo, or as root) deletes your entire filesystem. There's no undo, no trash bin. The shell does exactly what you tell it.
Read rm commands twice before pressing Enter. Be especially careful with variables: rm -rf $foo/ where $foo is empty becomes rm -rf / - disaster.
There's no trash for rm. Files are gone. Some distributions have trash-cli (trash-put instead of rm) - install if you want a safety net.
Wildcards (globs)¶
The shell expands * and ? into matching filenames before running the command:
ls *.txt # all .txt files
ls *.tx? # .txt, .tx1, .tx5, etc - ? matches one character
ls a* # all starting with 'a'
ls *2024* # all containing '2024'
rm /tmp/*.log # all .log files in /tmp
Two globs you'll meet:
- * - zero or more characters (not crossing /).
- ? - exactly one character.
- ** (in some shells) - match across directories.
A few more handy ones¶
tree- show directory contents as a tree. Install if not present:sudo apt install tree/brew install tree.du -sh dir- disk usage (size) of a directory, human-readable.df -h- disk free across all mounted filesystems.stat file- detailed metadata about a file.
Exercise¶
In ~/practice:
mkdir -p projects/blog/posts- create nested dirs.cd projects/blog.touch index.html style.css script.js- create three files.mkdir posts/2026andtouch posts/2026/first-post.md posts/2026/second-post.md.ls -la- see the whole tree.cp index.html backup.html- copy.mv backup.html index.html.bak- rename.cp -r posts/2026 posts/2027- copy a directory.ls posts/- verify.rm index.html.bak- delete the backup.rm -r posts/2027- delete the copied directory.- Bonus:
tree .(installtreeif not present). See the final structure.
Don't actually rm -rf ~/practice - leave it for the next page.
What you might wonder¶
"Why no trash?" Unix tradition: be explicit. The convenience of "I can recover" creates the bad habit of "I'll just delete and check later." Be deliberate; verify before deleting.
"What's the difference between cp and cp -r?"
cp only copies files. cp -r recursively copies directories and everything in them. Tools like cp won't operate on directories without the explicit recursive flag - safety feature.
"What does the shell do with *?"
The shell expands it to a list of matching files before running the command. rm *.log becomes rm a.log b.log c.log (the shell does the substitution, then rm runs with those arguments).
"What if I delete something important?" If you're using a filesystem with snapshots (ZFS, btrfs with snapshots, Time Machine on macOS), you might recover. Otherwise: gone. Backups exist for this reason.
Done¶
- Create directories (
mkdir,mkdir -p). - Create empty files (
touch). - Copy files and directories (
cp,cp -r). - Move/rename (
mv). - Delete (
rm,rm -r) - carefully. - Use globs (
*,?).