File and Directory Operations
Challenge Gallery
Quick Reference
| Command | What It Does |
|---|---|
mkdir dirname |
Create a directory |
mkdir -p a/b/c |
Create nested directories (parents too) |
touch filename |
Create empty file (or update timestamp) |
cp source dest |
Copy a file |
cp -r srcdir destdir |
Copy a directory recursively |
mv source dest |
Move or rename a file/directory |
rm filename |
Delete a file (permanent) |
rm -r dirname |
Delete a directory and everything in it |
rm -i filename |
Delete with confirmation prompt |
rmdir dirname |
Delete an empty directory only |
mv Does Double Duty
| Usage | Effect |
|---|---|
mv old.c new.c |
Rename (same directory) |
mv file.c ../ |
Move to parent directory |
mv file.c /tmp/file.c |
Move to a different location |
How It Works
Creating Files and Directories
student@ubuntu:~$ mkdir cscd240
student@ubuntu:~$ mkdir -p cscd240/labs/lab1
student@ubuntu:~$ cd cscd240/labs/lab1
student@ubuntu:~/cscd240/labs/lab1$ touch hello.c
student@ubuntu:~/cscd240/labs/lab1$ ls
hello.c
student@ubuntu:~$ mkdir -p cscd240/labs/lab1
student@ubuntu:~$ cd cscd240/labs/lab1
student@ubuntu:~/cscd240/labs/lab1$ touch hello.c
student@ubuntu:~/cscd240/labs/lab1$ ls
hello.c
Copying, Moving, Renaming
# Copy a file
student@ubuntu:~/cscd240/labs/lab1$ cp hello.c hello_backup.c
# Rename a file (mv = move OR rename)
student@ubuntu:~/cscd240/labs/lab1$ mv hello_backup.c hello_v2.c
# Move a file to another directory
student@ubuntu:~/cscd240/labs/lab1$ mv hello_v2.c ~/cscd240/
# Copy an entire directory
student@ubuntu:~/cscd240/labs/lab1$ cd ~/cscd240
student@ubuntu:~/cscd240$ cp -r labs/lab1 labs/lab1_backup
student@ubuntu:~/cscd240$ ls labs/
lab1 lab1_backup
student@ubuntu:~/cscd240/labs/lab1$ cp hello.c hello_backup.c
# Rename a file (mv = move OR rename)
student@ubuntu:~/cscd240/labs/lab1$ mv hello_backup.c hello_v2.c
# Move a file to another directory
student@ubuntu:~/cscd240/labs/lab1$ mv hello_v2.c ~/cscd240/
# Copy an entire directory
student@ubuntu:~/cscd240/labs/lab1$ cd ~/cscd240
student@ubuntu:~/cscd240$ cp -r labs/lab1 labs/lab1_backup
student@ubuntu:~/cscd240$ ls labs/
lab1 lab1_backup
Removing Files
# Safe: interactive mode asks before deleting
student@ubuntu:~/cscd240$ rm -i hello_v2.c
rm: remove regular file 'hello_v2.c'? y
# Remove a directory and everything inside it
student@ubuntu:~/cscd240$ rm -r labs/lab1_backup
# Verify it's gone
student@ubuntu:~/cscd240$ ls labs/
lab1
student@ubuntu:~/cscd240$ rm -i hello_v2.c
rm: remove regular file 'hello_v2.c'? y
# Remove a directory and everything inside it
student@ubuntu:~/cscd240$ rm -r labs/lab1_backup
# Verify it's gone
student@ubuntu:~/cscd240$ ls labs/
lab1
Common Pitfalls
- No undo, no trash can –
rmis permanent. Double-check before you press Enter. rm *.ovsrm * .o– that accidental space turns one safe command into a catastrophe.- Forgetting
-r–cpandrmboth refuse to work on directories without-r. mkdirwithout-p– fails if parent directories don’t exist. Use-pfor nested paths.mvoverwrites silently – if the destination file exists, it’s replaced without warning. Usemv -ito get a prompt.