Skip to content

Basic File Operations


Navigating the filesystem gets you somewhere. It doesn’t let you change anything once you’re there. This chapter covers the five commands that do the actual work of building and rearranging a filesystem — and one command in particular that deserves real caution, since it’s the first one in this course with no built-in safety net.

Creating Directories: mkdir

mkdir (“make directory”) creates a new, empty directory at the path you give it.

mkdir projects

Run from your home directory, this creates ~/projects. If you try to create a directory whose parent doesn’t exist yet — say, mkdir projects/2024/notes when projects doesn’t exist — mkdir will refuse and complain. Tell it to create the whole chain at once with -p:

mkdir -p projects/2024/notes

-p creates every missing directory along the path, not just the final one. It’s also safe to use even when the directories already exist — unlike running plain mkdir on something that already exists, mkdir -p won’t error out, which makes it the safer default for scripts.

Creating Empty Files: touch

touch creates an empty file if it doesn’t exist yet:

touch notes.txt

If notes.txt already exists, touch doesn’t erase it or complain — it just updates the file’s “last modified” timestamp to right now, leaving the content untouched. That’s actually touch’s original purpose; using it to create empty files is really just a convenient side effect.

Copying: cp

cp copies a file (or directory) from one location to another, leaving the original in place.

cp notes.txt notes-backup.txt

This creates notes-backup.txt as a duplicate of notes.txt in the same location. You can also copy into a different directory:

cp notes.txt projects/notes.txt

By default, cp only works on files — trying to copy a directory this way will fail with a complaint about it being a directory. To copy a directory and everything inside it, add -r (recursive):

cp -r projects projects-backup

Warning

cp will silently overwrite an existing destination file with no confirmation. If notes-backup.txt already existed before you ran the command above, its previous contents are gone the instant cp finishes — there’s no prompt warning you first.

Moving And Renaming: mv

mv moves a file or directory to a new location — and, since Linux doesn’t distinguish “renaming” as a separate operation, mv is also how you rename things.

mv notes.txt final-notes.txt

Run in the same directory, this renames notes.txt to final-notes.txt. Run with an actual destination directory instead, it moves the file there:

mv final-notes.txt projects/

Unlike cp, mv works on directories without needing a -r flag — moving a directory doesn’t require copying its contents one by one, since it’s just changing where the same data is referenced from.

Warning

Just like cp, mv overwrites an existing destination with no warning. Double-check the destination before running it, especially when renaming — mv report.txt report-final.txt will destroy an existing report-final.txt without telling you.

Removing: rm

This is the one to slow down for. rm deletes files, and unlike deleting something through a graphical file manager, there is no trash can, no undo, and no confirmation by default.

rm notes.txt

That file is gone the moment this command finishes. Not “moved somewhere recoverable” — gone, immediately, with the space it occupied available to be overwritten by anything else.

To remove a directory, rm alone will refuse, the same way cp does. You need:

rm -r projects

-r removes the directory and everything inside it, recursively — every file, every subdirectory, all of it, without asking about any of them individually.

There’s also -f (“force”), which suppresses any warnings rm would otherwise show — for example, it stops rm from asking before removing a file you don’t have write permission to modify (assuming you have permission to delete it at all, which is a distinction covered in the Permissions section). Combined together, -rf is one of the most quoted, most feared, and most genuinely dangerous flag combinations in Linux, precisely because it removes every point where the system might otherwise have given you a chance to stop.

Warning

Before running any rm -r or rm -rf command, run ls on the same path first — literally the same path, copy-pasted, with ls instead of rm -r in front of it — and actually read the output. This costs you two seconds and is the single most effective habit for avoiding an unrecoverable mistake. A mistyped path with ls shows you an error or the wrong file list. A mistyped path with rm -rf shows you nothing, because there’s nothing left to show.

Interactive Confirmation, If You Want It

If you’d rather have a safety net while you’re still building confidence with these commands, opetion -i adds a confirmation prompt to cp, mv, and rm alike:

rm -i notes.txt

This asks remove notes.txt? before doing anything, and only proceeds if you answer y. It’s slower, and most experienced users turn it off once they trust their own habits — but there’s no downside to leaning on it now while these commands are still new.

A Quick Comparison

CommandNeeds -r for directories?Overwrites silently?Recoverable?
mkdirN/A (creates directories)No (errors instead, unless -p)N/A
touchN/A (files only)No (updates timestamp only)N/A
cpYes (-r)YesOriginal stays untouched
mvNoYesOriginal is gone from its old location
rmYes (-r)N/A (deletes, doesn’t overwrite)No

What’s Next

You can now create, duplicate, relocate, and remove things — everything except actually editing a file’s contents. The next chapter covers exactly that: a short, dedicated look at two text editors, nano and vim, so you’re never stuck staring at an unfamiliar one on someone else’s server.

Last updated on