Working with files & directories
mkdir, touch, cp, mv, rm, wildcards and brace expansion, and how to avoid deleting the wrong thing.
Now you'll create, copy, move, rename and delete files and directories. These commands are simple, but some of them are irreversible: Linux has no recycle bin on the command line. Learn the safe habits from day one.
Creating directories: mkdir#
Creating files: touch and redirection#
touch creates an empty file, or updates the timestamp of an existing one:
>overwrites without asking.>>appends. You'll learn redirection properly in Pipes & redirection. To avoid accidental overwrites,set -o noclobbermakes>refuse to replace existing files.
Copying: cp#
Watch the trailing-directory rule. If the destination is an existing directory, files go inside it. Otherwise the destination is the new name.
Moving and renaming: mv#
Linux has no separate rename command for single files. Renaming is moving:
mv accepts -i (ask), -n (no overwrite) and -v (verbose) like cp. Moving within the same filesystem is instant, even for huge files, because only the name changes.
Deleting: rm and rmdir#
There's no undo. Deleted files don't go to a trash can. Safety habits:
- Use
rm -i(or-I, which asks once for big deletions) when unsure. - Run
lswith the same pattern first:ls *.logbeforerm *.log. - Be very careful with
rm -rf.-fsilences all warnings. Never runrm -rfon a path built from a variable without quoting it and checking that it's set:rm -rf "${BUILD_DIR:?}/". - On desktops,
gio trash file(or thetrash-clipackage) sends files to the trash instead.
Wildcards (globbing)#
The shell expands wildcards into matching file names before the command runs:
Because the shell does the expansion, rm *.txt really runs rm hello.txt report1.txt …. You can preview any glob with echo.
Brace expansion#
Braces generate words. Unlike globs, they don't look at existing files:
cp file{,.bak} is a classic shortcut for "make a backup copy next to the original".
Names with spaces and special characters#
A file whose name starts with - looks like an option. Use ./-file or -- to end option parsing: rm -- -file.
Checking what a file is#
file inspects the content, not the extension. photo.jpg here is an empty file, whatever its name says.
Putting it together#
A safe, typical workflow for tidying a downloads folder:
Common mistakes#
rm -rfwith a typo or an empty variable (rm -rf "$DIR"/withDIRunset). Double-check, and uselswith the same arguments first.- Overwriting files with
cpormv, which replace targets silently. Use-i(ask) or-n(never overwrite) when it matters. - Forgetting
-rwhen copying directories:cp folder backupfails with "omitting directory". - Unquoted names with spaces:
rm my file.txttries to delete two files. Quote it:rm "my file.txt". - Expecting a trash can.
rmdeletes permanently.
What's next#
Next you'll read what's inside files with cat, less, head and tail, including following log files live.
Check your understanding
Quick quiz
1.What does
mv report.txt archive/do ifarchiveis an existing directory?2.Which command copies a directory and everything inside it?
3.Why is
rm -rf $DIR/dangerous in a script?
Finished reading?
Mark this lesson complete to track your progress.