student@ubuntu:~$
shell 2/5 25 XP

File and Directory Operations

0%

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

Terminal
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

Copying, Moving, Renaming

Terminal
# 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

Removing Files

Terminal
# 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

Common Pitfalls

  • No undo, no trash canrm is permanent. Double-check before you press Enter.
  • rm *.o vs rm * .o – that accidental space turns one safe command into a catastrophe.
  • Forgetting -rcp and rm both refuse to work on directories without -r.
  • mkdir without -p – fails if parent directories don’t exist. Use -p for nested paths.
  • mv overwrites silently – if the destination file exists, it’s replaced without warning. Use mv -i to get a prompt.

Unlocks

Complete this skill to see what it unlocks.