In a file manager (an app for managing files, such as File Explorer or Finder), a file you delete goes to the bin first, and you can still restore it. The terminal has no bin: rm, the command that deletes files, works like a paper shredder, so what goes in is gone. The other commands are friendlier. Picture a filing cabinet in an office. A sheet of paper is a file, a folder in the cabinet is a folder on the computer, and the label on a sheet is the file name:
touchtakes a blank sheet and puts it in a folder.mkdiradds a new folder.cpmakes a photocopy of a sheet.mvmoves a sheet to another folder or changes its label.
With these commands you create, fill, copy, move and delete files without opening a file manager.
Every example below runs in Budi's ~/web-project folder. At the start, that folder holds error.log, index.html, logs, script.js, src and style.css.
Making an empty file: touch
budi@laptop:~/web-project$ touch about.html
budi@laptop:~/web-project$ ls
about.html error.log index.html logs script.js src style.css
budi@laptop:~/web-project$ ls -l about.html
-rw-rw-r-- 1 budi budi 0 Oct 8 09:16 about.html
touch about.htmlmakes an empty file, 0 bytes. When it works,touchshows nothing.- If the file already exists,
touchonly sets its last-changed time (the time column inls -l) to now. Its content doesn't change. - Several files with one command:
touch a.css b.js. Every name separated by a space becomes one file.
Making folders: mkdir and mkdir -p
budi@laptop:~/web-project$ mkdir assets/img
mkdir: cannot create directory ‘assets/img’: No such file or directory
budi@laptop:~/web-project$ mkdir -p assets/img
budi@laptop:~/web-project$ ls assets
img
mkdir namemakes one folder. Its parent folder must already exist. In this exampleassetsdoesn't exist yet, somkdir assets/imgis refused.-p(parents) makes every folder in the path that doesn't exist yet:assetsfirst, thenimg. Ifassets/imgalready exists,mkdir -pshows no error.
Writing into a file: > and >>
In the example below, the first two commands fill notes.txt with two lines. Then Budi runs echo "Hello" > notes.txt once more. Guess how many lines are left, then look at the file content that cat shows on the last line.
budi@laptop:~/web-project$ echo "Hello" > notes.txt
budi@laptop:~/web-project$ echo "Second line" >> notes.txt
budi@laptop:~/web-project$ cat notes.txt
Hello
Second line
budi@laptop:~/web-project$ echo "Hello" > notes.txt
budi@laptop:~/web-project$ cat notes.txt
Hello
echonormally shows text on the screen.>sends that output into a file instead: the file is created if it doesn't exist, and its content is overwritten if it does.>>adds the output to the end of the file. The old content stays.cat notes.txtshows what's in the file. Reading files is covered properly in the next lesson.
The fourth command uses > again, so the two lines are gone. Only one line is left in the file, Hello.
Copying and moving: cp and mv
budi@laptop:~/web-project$ cp index.html index-old.html
budi@laptop:~/web-project$ mv about.html about-us.html
budi@laptop:~/web-project$ cp src src-backup
cp: -r not specified; omitting directory 'src'
budi@laptop:~/web-project$ cp -r src src-backup
cp source destinationcopies.index-old.htmlbecomes a copy ofindex.html.mv source destinationmoves. If the destination is a new name in the same folder, that's a rename. If the destination is a folder, the file goes into that folder:mv about-us.html src/.- Copying a folder needs
-r(recursive), so that what's inside the folder is copied too.mvdoesn't need-r. cpandmvoverwrite a destination file that already exists without asking. Add-iif you want to be asked first.
Deleting: rm, rm -r and rmdir
budi@laptop:~/web-project$ rm index-old.html
budi@laptop:~/web-project$ rm assets
rm: cannot remove 'assets': Is a directory
budi@laptop:~/web-project$ rmdir assets
rmdir: failed to remove 'assets': Directory not empty
budi@laptop:~/web-project$ rm -r assets
budi@laptop:~/web-project$ rm -i about-us.html
rm: remove regular empty file 'about-us.html'? y
rm filedeletes a file. There's no Recycle Bin (the bin) and no undo. If you want to be able to get the file back, use a command that moves it to the file manager's bin:trash fileon macOS 15 or later,gio trash fileon Linux with the GNOME desktop (the default look of Ubuntu), ortrash-put fileafter installing the program trash-cli.- Plain
rmrefuses a folder.rm -r folderdeletes a folder and everything in it. rmdir folderonly deletes an empty folder. It's safe for tidying up, becausermdirrefuses while something is still inside the folder.-i(interactive) asks first for each file. Answeryto delete, ornto cancel.-f(force) is the opposite: it never asks, and shows nothing if the file doesn't exist.rm -rf folder/combines-rand-f: delete the folder and its contents, without asking. It's the most dangerous command you'll see often, for examplerm -rf node_modules/.
⚠️ One slip and the file is gone
>when you meant>>. One character short, and the old content is gone right away.- Spaces in file names.
touch meeting notes.txtmakes two files,meetingandnotes.txt. For one name with a space, use quotes:touch "meeting notes.txt". On Linux,lsthen shows it in quotes too:'meeting notes.txt'. rm -rfin the wrong folder. Check withpwdandlsbefore deleting. A modern rm refusesrm -rf /, butrm -rf *(*means every name in the folder you are in) in the wrong folder still deletes everything.- Overwriting without noticing.
cp index.html style.cssquietly replaces what's instyle.css. Usecp -iormv -iwhen unsure.
TL;DR: touch makes an empty file (or updates its last-changed time), mkdir makes a folder, and mkdir -p also makes its missing parent folders. echo "text" > file writes the text into the file and overwrites the old content, while >> adds the text to the end of the file. cp copies (a folder needs -r so its contents come too), and mv moves or renames. rm deletes a file forever, with no bin. rm -r deletes a folder and everything in it, rmdir only deletes an empty folder, and -i asks first. rm -rf force-deletes without asking, so always check pwd before running it.
⚡ Make files and folders
Write:
touch about.html
mkdir -p assets/img
Meaning: touch makes an empty file, 0 bytes, here about.html. mkdir makes a folder. With -p, mkdir also makes any parent folders that don't exist yet: assets first, then img inside it.
⚡ Overwrite or add
Write:
echo "Hello" > notes.txt
echo "Second line" >> notes.txt
Guess first: > writes the output of echo into a file, and >> adds it to the end of the file. After those two lines, you run echo "Hello" > notes.txt again. How many lines are left?
One line, Hello. > overwrites the whole file, while >> adds to the end.
Meaning: If you write > when you meant >>, the old content is gone.
⚡ Copy and move
Write:
cp index.html index-old.html
mv about.html about-us.html
cp -r src src-backup
Meaning: cp copies: index-old.html becomes a copy of index.html. mv moves. When the destination is a new name in the same folder, mv renames: about.html becomes about-us.html. Copying a folder such as src needs -r so its contents get copied too. Both cp and mv overwrite a destination file that already exists without asking.
⚡ Deleting is forever
Write:
rm -i about-us.html
Meaning: rm deletes a file, and there's no Recycle Bin (the bin) and no undo. -i asks first, before deleting. rm -r deletes a folder and everything in it, and rmdir only deletes an empty folder. Before rm -rf (delete a folder and its contents without asking), always check pwd.