Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Use mkdir to make a directory, touch to prepare an empty file, cp and mv to copy or move it, and rm and rmdir to remove files and empty directories. This lesson keeps all changes inside a new practice folder in your home directory and checks the result after each operation.

Prepare a safe practice directory

These steps assume Bash and an ordinary user account. ~/linux-course-07-practice is meant to be a new, empty directory. If it already exists, choose another name rather than touching existing data. The first mkdir command will report an error if the name is taken; treat that as a reason to stop, not as a step to skip.

mkdir ~/linux-course-07-practice

On success, mkdir normally prints nothing. Check that the directory was created with ls -ld. The rest of the exercise assumes that this step succeeded and that the directory was empty.

ls -ld ~/linux-course-07-practice

Create two subdirectories, then list the practice directory:

mkdir ~/linux-course-07-practice/drafts ~/linux-course-07-practice/archive
ls ~/linux-course-07-practice

You should see archive and drafts. mkdir -p is useful when creating deeper paths, but it also accepts an existing directory, so this exercise does not use it.

Create and check an empty file

touch creates an empty file when its name does not exist. On an existing file, it can update timestamps instead, so it should not be understood as a command that only creates new files.

touch ~/linux-course-07-practice/drafts/notes.txt
ls -l ~/linux-course-07-practice/drafts/notes.txt

Because the new file has no content, ls -l will generally show a size of 0 bytes. Permissions and dates depend on your system. touch does not write text into the file; that requires an editor or another command.

Compare copying and renaming

cp leaves the source in place and creates a separate copy. This command copies notes.txt from drafts to archive. With GNU cp, -i asks before overwriting an existing destination of the same name.

cp -i ~/linux-course-07-practice/drafts/notes.txt ~/linux-course-07-practice/archive/notes.txt
ls -1 ~/linux-course-07-practice/drafts
ls -1 ~/linux-course-07-practice/archive

The two ls -1 commands produce the following lines in order: first the contents of drafts, then those of archive. Both contain notes.txt. Changing the content of one copy later does not automatically update the other.

notes.txt
notes.txt

mv moves a file to another location or changes its name within the same directory. Here it renames the original file to notes-old.txt; the archived copy keeps its original name.

mv -i ~/linux-course-07-practice/drafts/notes.txt ~/linux-course-07-practice/drafts/notes-old.txt
ls -1 ~/linux-course-07-practice/drafts
ls -1 ~/linux-course-07-practice/archive

Now the first output line, from drafts, is notes-old.txt. The second, from archive, remains notes.txt. The source name changed without changing the copy.

notes-old.txt
notes.txt

If the last argument to cp or mv is an existing directory, the file is placed inside that directory under its name. Check whether the destination is a file or directory before running the command. Do not approve an -i overwrite prompt without reading it.

Remove a file and an empty directory

rm removes a named file. It does not send the file to a recycle bin by default, so check the path with ls first. Here -i requests confirmation before deletion. Read the file name in the prompt and agree only if it is the intended target.

ls -l ~/linux-course-07-practice/drafts/notes-old.txt
rm -i ~/linux-course-07-practice/drafts/notes-old.txt

If you confirmed the deletion, drafts is now empty. Use rmdir, which removes only an empty directory:

rmdir ~/linux-course-07-practice/drafts
ls ~/linux-course-07-practice

The final listing still contains archive. Because archive contains notes.txt, trying to remove it with rmdir would fail. Keep the practice data until you no longer need it, then check the exact path before cleaning it up separately. A blanket rm -rf is not a good shortcut for this beginner exercise.

Checks to make before changing files

  • Current location: Run pwd when a relative path depends on where you are.
  • Target: Use ls -ld to inspect the named file or directory itself.
  • Overwriting: Back up important data and check the destination for an existing name before copying or moving.
  • Deletion: rm does not provide a built-in recovery mechanism; do not guess a short path and run it.

For more options and edge cases, see the guides to touch, mkdir, cp, mv, rm, and rmdir.

Official documentation

Consult the GNU Coreutils manual for these commands and their options. Check man or --help on your own system when the installed implementation matters.

More in This Category
Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Learn how to use the Linux sudo command to run approved commands with elevated privileges or as another user, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux top Command: Monitor Processes in Real Time

Linux top Command: Monitor Processes in Real Time

Learn how to monitor Processes in Real Time with the Linux top command, including practical examples, key options, and important precautions.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux Tutorial / Choosing a Linux Distribution

Linux Tutorial / Choosing a Linux Distribution

Explore major Linux distributions and learn when to choose Ubuntu LTS or Rocky Linux for study and server practice. Compare package management, release policies, and practical selection criteria.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux tee Command: Display and Save Pipeline Output

Linux tee Command: Display and Save Pipeline Output

Learn how to display and Save Pipeline Output with the Linux tee command, including practical examples, key options, and important precautions.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.