LESSON · 1 OF 6

Creating, copying, and moving files

Creating, copying, and moving files

Three commands cover most of the "put a file here" moves you'll make.

touch

touch creates an empty file.

bash
touch /root/notes.txt

If the file already exists, touch updates its modification time and leaves the contents alone. That second behaviour is useful sometimes; usually you just want a new empty file.

cp

cp copies a file from one place to another.

bash
cp /etc/hostname /tmp/hostname-backup

If the destination is a folder, the file is placed inside it with the same name:

bash
cp /etc/hostname /tmp/

Copying a whole folder needs -r (recursive):

bash
cp -r /etc/apt /tmp/apt-backup

Without -r, cp refuses to copy directories.

mv

mv is both "move to a different folder" and "rename in place". Linux doesn't distinguish, renaming a file is just moving it to a new name in the same folder.

bash
mv /tmp/hostname-backup /tmp/hostname.txt      # rename
mv /tmp/hostname.txt /root/                    # move to /root

Unlike cp, mv handles folders without needing -r. Moving a folder just updates a pointer, so it's fast even for huge trees.

Spin up a fresh environment and practice live.
linux-devops-basic · fresh machine · ready in under a minute