Creating & Deleting Files — Terminal

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…

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:

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

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

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

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

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

⚠️ One slip and the file is gone

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.

What you will learn