Essential File Operations on the Command Line

File operations are the bread and butter of command line work. Whether you’re organizing a project, deploying code, or cleaning up build artifacts, you’ll reach for these commands constantly. The key is learning not just how they work, but how to use them safely — because unlike a GUI file manager, the terminal doesn’t have a recycle bin.

Creating files and directories

touch creates empty files or updates timestamps on existing ones. mkdir creates directories, and with the -p flag, it creates nested directory structures in one shot:

touch file.txt                # Create empty file
touch file1.txt file2.txt     # Create multiple files
mkdir mydir                   # Create directory
mkdir -p path/to/nested/dir   # Create full path at once

touch doesn’t overwrite existing files — it only updates their modification timestamp. This makes it safe to use even if you’re not sure whether the file already exists.

Copying with cp

cp copies files and directories. The -r flag is required for directories (recursive copy), and -i prompts before overwriting existing files:

cp file.txt backup.txt        # Copy file
cp -r src/ dest/              # Copy directory recursively
cp -i file.txt dest/          # Prompt before overwrite
cp -v file.txt dest/          # Verbose — show what's copied

Moving and renaming with mv

mv handles both moving files to new locations and renaming them — it’s the same operation from the filesystem’s perspective:

mv old.txt new.txt            # Rename file
mv file.txt /path/to/dest/    # Move file
mv -i src dest                # Prompt before overwrite

Renaming and moving are the same command because from the filesystem’s perspective, they’re the same operation — changing where a file’s name points.

Deleting with rm

rm removes files permanently. There is no undo, no trash can, no recovery without backups. This is the one command where developing safe habits from day one matters:

rm file.txt                   # Delete file
rm -r directory/              # Delete directory recursively
rm -i file.txt                # Prompt before delete — use this!

Add alias rm="rm -i" to your shell configuration. The -i flag prompts for confirmation before every deletion, giving you a safety net against typos that could cost you hours of work.

The difference between rm file.txt and rm -rf / is a few characters and a moment of inattention. Always double-check your rm commands before pressing Enter, and always use -i for interactive confirmation.


Ready to practice? Explore the project repository for the full file operations reference and interactive exercises.

Essential File Operations on the Command Line
Essential File Operations on the Command Line