Files and Directories
Create, copy, move, and delete files and directories with touch, mkdir, cp, mv, rm, and rmdir.
- Create files with touch and directories with mkdir
- Copy files and directories with cp
- Move and rename files with mv
- Delete files with rm and empty directories with rmdir
- Understand that rm is permanent — there is no trash
Navigation tells you where things are. Now you need to create, reorganize, and remove them. These six commands are the foundation of every file management task in the shell — and they're the same commands AI agents use when they create files, organize output, and clean up after tasks.
Creating files and directories
touch notes.txt # create an empty file (or update its timestamp if it exists)
touch a.txt b.txt c.txt # create multiple files at once
mkdir projects # create a directory
mkdir -p a/b/c # create nested directories in one command (-p = parents)touch is named for what it does: it "touches" a file, creating it if it doesn't exist. A common pattern when writing scripts is to touch a file as a placeholder before you have content for it.
mkdir -p is essential for building directory trees. Without -p, mkdir a/b/c fails if a or a/b don't exist yet.
Copying files
cp original.txt copy.txt # copy a file to a new name
cp notes.txt ~/backup/notes.txt # copy to a different location
cp -r my-folder/ backup-folder/ # copy a whole directory (-r = recursive)cp requires -r for directories. Without it, the shell refuses to copy a folder and tells you why.
cp overwrites silently. If copy.txt already exists, cp original.txt copy.txt replaces it without asking. Use cp -i (interactive) to get a confirmation prompt before overwriting. This is a habit worth building early.
Moving and renaming
mv old-name.txt new-name.txt # rename a file
mv file.txt ~/documents/ # move to a different directory
mv file.txt ~/documents/new.txt # move and rename at once
mv folder-a/ folder-b/ # rename a directorymv does double duty: moving and renaming are the same operation at the filesystem level. There's no separate rename command needed.
Deleting files and directories
rm file.txt # delete a file
rm -i file.txt # ask for confirmation first (-i = interactive)
rmdir empty-folder/ # delete an empty directory
rm -r full-folder/ # delete a directory and everything inside itrm is permanent. There is no trash. Unlike clicking "Delete" in a file manager, rm removes files immediately and irreversibly. There is no recycle bin, no undo, no recovery without backups. This is the most important safety warning in all of Bash. Use rm -i until you're completely confident, and be especially careful with rm -r on directories. Double-check the path before pressing Enter.
Putting it together: building a directory tree
Here's a realistic sequence for setting up a new project:
mkdir -p my-project/src my-project/tests my-project/docs
touch my-project/src/main.sh
touch my-project/README.md
ls -R my-project/ls -R (recursive) shows the full tree:
my-project/:
README.md docs src tests
my-project/docs:
my-project/src:
main.sh
my-project/tests:Now reorganize it:
cp my-project/README.md my-project/docs/README.md
mv my-project/src/main.sh my-project/src/app.sh
rm my-project/README.md
ls -R my-project/Check your understanding
- 1.What does the -p flag do for mkdir?
- 2.Files deleted with rm can be recovered from the trash.
- 3.You want to rename report.txt to final-report.txt. Which command does this?
Do it yourself
Build and dismantle a directory tree in /tmp where mistakes don't matter:
cd /tmp
mkdir -p practice/src practice/tests
touch practice/src/hello.sh practice/tests/test1.sh
ls -R practice/
mv practice/src/hello.sh practice/src/main.sh
cp practice/tests/test1.sh practice/tests/test2.sh
rm practice/tests/test1.sh
ls -R practice/
rm -r practice/After the last command, ls practice/ should fail with "No such file or directory" — confirming it's gone.
Where to go next
You can now build and tear down directory structures confidently. Next: variables and environment — how the shell stores values in variables, what environment variables are, how $PATH works, and why quoting rules matter.