LearnWithShobhan

Linux file commands: touch, cat, less, mkdir, cp, mv and rm (with the flags that matter)

Shobhan Biswas··6 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

You can do 90% of your daily file work in the terminal with seven commands. The commands themselves are easy. What trips people up is the handful of flags that decide whether a command quietly overwrites your work, refuses to touch a folder, or deletes something for good.

This guide walks through each command with the flags that matter. The examples assume you are in a scratch folder, so try them as you read.

Create files: touch (or an editor)

touch creates an empty file. Pass the name as an argument:

bash
$ touch notes.txt
$ touch report
$ ls
notes.txt report

The second file has no extension, and that is fine. In Unix an extension is only a hint for you and for graphical apps. It does not change how the file is stored. (If the file already exists, touch just updates its modified time.)

The other way to create a file is to open an editor with the name: nano notes.txt, type, then Ctrl + X to save.

Read files: cat and less

cat prints a file. Give it several files and it prints them one after another, which is where the name comes from: it is short for concatenate.

bash
$ cat file1.txt
hello from file 1
$ cat file1.txt file2.txt
hello from file 1
hello from file 2

cat is perfect for short files. For a long file it dumps everything and you scroll back with your mouse. For anything big, use a pager:

CommandWhat you get
more big.txtPage by page, forward only
less big.txtPage by page, forward and back, plus search

There is an old joke behind it: "less is more". less came later and does more, so use less.

Key inside lessAction
Space or fNext page
bPrevious page
/wordSearch forward (n jumps to the next hit)
?wordSearch backward
qQuit

Create directories: mkdir

bash
$ mkdir photos docs
$ mkdir animal/cat
mkdir: animal: No such file or directory
$ mkdir -p animal/cat
$ ls animal
cat

You can pass several names at once. The second command fails because animal does not exist yet. The -p flag (parents) creates every missing folder along the way, and it does not complain if the folder is already there. You will use mkdir -p all the time in scripts.

Move and rename: mv

One command does both jobs. What changes is the destination.

Diagram: mv file1.txt directory1/ moves the file into the folder, mv file2.txt notes.txt only changes its name
If the destination is a directory the file moves into it. If it is a new name, the file is renamed.
bash
$ mv file1.txt directory1/ # move into a directory
$ mv file1.txt directory1/copy.txt # move AND rename in one step
$ mv file2.txt notes.txt # rename only
$ mv directory1 directory2 # a directory moves the same way

You can also point at a path without being inside the folder first: ls directory1 works from where you stand, and so does mv. Whether a path is written from / or from where you are is the idea behind absolute and relative paths.

The trap: mv overwrites silently

If directory1/file1.txt already exists and you move another file1.txt there, the old one is replaced without a question. Two flags protect you:

bash
$ mv -i file1.txt directory1/
overwrite directory1/file1.txt? (y/n [n]) n
not overwritten
$ mv -v notes.txt directory1/
notes.txt -> directory1/notes.txt

-i (interactive) asks before overwriting. -v (verbose) prints what it did. Combined as mv -iv, they make a good default.

Copy: cp

cp has the same shape as mv: source first, destination second.

bash
$ cp file1.txt directory2/
$ cp -i file1.txt directory1/
overwrite directory1/file1.txt? (y/n [n]) n

Same warning as mv: without -i it overwrites. The difference shows up with directories. Unix refuses to copy one unless you say so, because copying a huge folder by accident is expensive:

bash
$ cp directory2 directory1/
cp: directory2 is a directory (not copied).
$ cp -r directory2 directory1/
$ ls directory1
directory2 file1.txt

-r means recursive: copy the folder and everything nested inside it. (mv does not need a flag for directories, because moving only changes where the folder is listed.)

Delete: rm has no undo

Deleting from Finder or File Explorer sends the file to the Trash. rm does not.

Diagram: deleting in a file manager moves the file to Trash and can be restored, rm removes it with no undo
Once rm succeeds, the file is gone. There is no Trash to open.
bash
$ rm delete-me.txt
$ rm directory2
rm: directory2: is a directory
$ rm -r directory2

Like cp, rm needs -r for a directory. Then the safety flags:

FlagMeaningWhen
-iAsk before each deletionMake this your default
-rDelete a directory and everything insideNeeded for folders
-fForce: never ask, hide errors about missing or read-only filesOnly when you are certain

A good habit is to make rm -i the default with an alias. You set that once in your shell config file, which is covered in Customize your shell: .bashrc vs .zshrc, aliases and the PS1 prompt.

Cheat sheet

TaskCommand
Create an empty filetouch file
Show a short filecat file
Page through a long fileless file
Create a folder (and parents)mkdir -p a/b/c
Move a file or foldermv source destination
Renamemv old new
Copy a filecp source destination
Copy a foldercp -r source destination
Delete a file, asking firstrm -i file
Delete a folderrm -r folder

Key takeaways

  • ✓touch creates files, cat prints them, less pages through long ones.
  • ✓mkdir -p creates a whole nested path in one go.
  • ✓mv moves or renames depending on the destination, and it overwrites silently unless you add -i.
  • ✓cp needs -r for directories, and rm needs -r too.
  • ✓rm skips the Trash. Use rm -i, and slow down on rm -rf.

Practice with someone who shows you the mistakes

Reading about mv is easy. Watching it overwrite a file you were not expecting to lose, and then learning how to prevent it, is what makes the habit stick. CLI Mastery covers all of this, and then pipes, permissions, scripting and a real server on AWS. 57 lessons, lifetime access. You can start with the free path lesson.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading