Create, Copy, Move and Delete Files in the Terminal
Once you can move between folders, the next step is handling files. Five commands cover most daily work, and they all follow the same pattern: command, options, then one or more file names.
Creating and reading
touch notes.txtcreates an empty file (or updates the timestamp of an existing one).echo "Buy milk" > notes.txtwrites a line into a file, replacing what was there. The>sign is a redirection, covered properly in the next chapter.cat notes.txtprints a file's contents. The name comes from "concatenate":cat a.txt b.txtprints both one after the other.
For long files, less notes.txt lets you scroll and search, and q quits.
Copying: cp
cp source destination copies a file. If the destination is an existing folder, the copy keeps its name and goes inside it. To copy a whole folder and everything in it, add -r (recursive): cp -r photos backup.
Moving and renaming: mv
There is no separate rename command. mv old.txt new.txt renames, and mv new.txt archive/ moves the file into a folder. Like cp, mv silently replaces a destination file with the same name; mv -i asks first.
Deleting: rm
rm notes.txt deletes a file. rm -r folder deletes a folder and all its contents. There is no trash can: deleted files are gone. Two safety habits:
- Run
lswith the same pattern first to see exactly what would be removed. - Use
rm -ito be asked about each file, especially with wildcards.
rmdir folder removes a folder only if it is empty, which makes it a safe choice for cleanup.
touch empty.txt
echo "Buy milk" > notes.txt
cat notes.txt
cp notes.txt backup.txt
mkdir archive
mv backup.txt archive/old-notes.txt
rm notes.txt
ls
ls -R
cat archive/old-notes.txt
rmdir archive
rm archive/old-notes.txt
rmdir archive
ls Transcript (verified by our test suite in a fresh temp folder)
$ touch empty.txt $ echo "Buy milk" > notes.txt $ cat notes.txt Buy milk $ cp notes.txt backup.txt $ mkdir archive $ mv backup.txt archive/old-notes.txt $ rm notes.txt $ ls archive empty.txt $ ls -R archive empty.txt ./archive: old-notes.txt $ cat archive/old-notes.txt Buy milk $ rmdir archive rmdir: archive: Directory not empty $ rm archive/old-notes.txt $ rmdir archive $ ls empty.txt
The transcript creates a file, copies it, renames the copy into a new folder and deletes the original. The final ls -R lists every folder recursively, so you can see where each file ended up.
Wildcards
The shell expands * to matching names before the command runs: ls *.txt lists every file ending in .txt, and cp *.jpg photos/ copies all JPEGs. This is powerful and is exactly why rm * deserves a second look before you press Enter.
Common mistakes
cp folder backupwithout-rrefuses to copy a directory.- Overwriting a file with
>when you meant to add to it with>>. - Running
rm -rfrom the wrong directory. Check withpwdfirst.
Summary
touchandecho ... >create files;catshows them.cpcopies (-rfor folders);mvmoves and renames.rmdeletes permanently; check first and consider-i.
Quiz: Working with files