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 getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.

Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux tmux Command: Detach and Reattach Terminal Sessions

Linux tmux Command: Detach and Reattach Terminal Sessions

Learn how to detach and Reattach Terminal Sessions with the Linux tmux command, including practical examples, key options, and important precautions.

Linux cp Command: Copy Files and Directories

Linux cp Command: Copy Files and Directories

Learn how to copy files and directories with Linux cp, control overwrites, preserve attributes, handle symlinks, and verify important copies.

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Compare find and locate for file searches, then use which, whereis, and Bash type to distinguish executable paths from shell command resolution.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

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.