mkdir, cp, mv, rm in Linux: working with files

Updated November 1, 2026

mkdir, cp, mv and rm create directories and copy, move and delete files.

Common cases

mkdir -p a/b/c        # create the whole directory tree
cp file file.bak      # copy it next to the original
cp -r src dst         # copy a directory with -r
mv old new            # move or rename
rm file               # delete a file
rm -r dir             # delete a directory and its contents

Sample output

$ mkdir -p a/b/c
$ ls
a
$ cp -r a copy
$ mv copy backup
$ ls
a  backup
$ rm -r backup
$ ls
a

How it works: mv is a rename

Within one filesystem mv does not move the data: it only changes the name entry in the directory, which is why moving and renaming are the same operation. cp always makes a fresh copy of the data. rm removes a name, and when a file's last name is gone and no process holds it open, its inode is freed. rm has no trash can: what is deleted is gone, so before rm *.tmp it helps to check the pattern with ls *.tmp first.

Common mistakes

When the last argument of mv is an existing directory, the file moves INSIDE it and nothing is printed. A directory holds archive and docs. After mv docs archive the command ls prints one line, archive, and ls archive prints docs: the directory was not renamed, it moved in. The opposite case shows up at once - mv *.txt backup with no backup around gives mv: target 'backup': No such file or directory.

cp without -r on a directory copies nothing at all. The answer reads like a warning: cp: -r not specified; omitting directory 'docs'. It is an error, the exit code is 1, and not one byte reached backup. Copy a directory with cp -r docs backup.

cp overwrites the destination without asking. Start with echo old > b.txt; after cp a.txt b.txt the command cat b.txt prints new, and the old content is gone for good. cp -i asks first, cp -n refuses to overwrite.

Used in

You need this in the lesson Files and directories. The theory is the chapter Files and directories.

See also

Filename globbing, The ls command.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.