Free Ebook cover Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

New course

10 pages

Creating, Copying, Moving, and Removing Files and Folders

Capítulo 3

Estimated reading time: 7 minutes

+ Exercise

Creating Empty Files with touch

The touch command is a quick way to create an empty file. If the file already exists, touch updates its timestamps (useful for build tools and scripts), without changing the file contents.

Create a new empty file

touch notes.txt

If notes.txt does not exist, it will be created as an empty file in the current directory.

Create multiple files at once

touch todo.txt ideas.txt

This is handy when you want to scaffold a set of files quickly.

Creating Directories with mkdir (and -p)

Use mkdir to create folders (directories). By default, it creates only the final directory name you provide, and it fails if parent directories are missing.

Create a single directory

mkdir projects

Create nested directories with -p

The -p option creates parent directories as needed and does not error if the directory already exists.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

mkdir -p projects/demo/{src,assets,docs}

In many shells, {src,assets,docs} expands into three directory paths. If your shell does not support brace expansion, create them separately.

Copying Files and Folders with cp (including -r)

cp duplicates files. You can copy a file to a new name, or copy it into a directory. To copy directories, you must use a recursive option such as -r (or -R on many systems).

Copy a file to a new file name

cp notes.txt notes.backup.txt

Copy one or more files into a directory

mkdir -p backups cp notes.txt todo.txt backups/

Copy a directory tree with -r

cp -r projects/demo projects/demo_copy

This creates a new directory demo_copy containing the same contents as demo.

Safer copying habits

  • cp -i: interactive; asks before overwriting an existing destination file.
  • cp -n: no-clobber; never overwrite existing files.
  • cp -v: verbose; prints what is being copied (useful while learning).
cp -iv notes.txt backups/notes.txt

Moving and Renaming with mv

mv moves files/directories from one place to another. Renaming is just a “move” within the same directory: you move a file from its old name to its new name.

Rename a file

mv notes.txt notes-2026-01.txt

Move a file into a directory

mv notes-2026-01.txt docs/

Move and rename in one step

mv todo.txt docs/tasks.txt

Safer moving habits

  • mv -i: asks before overwriting.
  • mv -n: do not overwrite existing files.
  • mv -v: show what is moved.
mv -iv docs/tasks.txt docs/tasks-old.txt

Removing Files and Directories: rm and rmdir

Deletion on the command line is powerful and immediate. There is typically no “trash can” when using rm. Build safe habits: preview what you will delete, use interactive flags, and avoid risky patterns.

Remove a file with rm

rm notes.backup.txt

Remove an empty directory with rmdir

rmdir only removes empty directories, which makes it a safer choice when you expect a folder to be empty.

rmdir old_empty_folder

Remove a directory tree with rm -r

To delete a directory and everything inside it, use recursive removal. This is the command that can do the most damage if used carelessly.

rm -r projects/demo_copy

Safety options you should know

  • rm -i: prompt before every removal.
  • rm -I: prompt once before removing many files (GNU rm); less noisy than -i.
  • rm -r: recursive; required for directories.
  • rm -f: force; do not prompt and ignore missing files (use with extreme caution).
  • rm -v: verbose; show what is removed.
rm -iv *.tmp

Avoid destructive patterns

These habits reduce the chance of deleting the wrong thing:

  • Prefer working inside a specific project directory rather than running rm from a broad location.
  • When using wildcards, first run a preview with ls using the same pattern to see what matches.
  • Avoid combining broad wildcards with recursion unless you are absolutely sure of the matches.
ls *.log rm -i *.log

Wildcard Basics: *, ?, and []

Wildcards (also called “globs”) let you match multiple file names at once. The shell expands these patterns into a list of matching paths before the command runs.

*: match any number of characters

ls *.txt

Matches notes.txt, todo.txt, etc.

?: match exactly one character

ls file?.txt

Matches file1.txt and fileA.txt, but not file10.txt.

[]: match one character from a set or range

ls report[12].md ls image[0-9].png

report[12].md matches report1.md and report2.md. image[0-9].png matches a single digit in that position.

Preview before you act

Because the shell expands patterns, you can often preview exactly what a command will receive by using echo or ls with the same pattern.

echo *.txt ls *.txt

Quoting and Spaces in Names

Spaces in file names are common (for example, downloaded documents). The shell treats spaces as separators between arguments, so you must quote paths that contain spaces (or escape the spaces).

Use quotes around paths with spaces

mkdir "Project Files" touch "Project Files/meeting notes.txt"

Copy or move quoted paths

cp "Project Files/meeting notes.txt" backups/ mv "Project Files/meeting notes.txt" "Project Files/meeting-notes.txt"

Single quotes vs double quotes

Both protect spaces. Double quotes still allow some expansions (like $HOME), while single quotes treat everything literally.

cp "$HOME/Downloads/My File.txt" . cp '$HOME/Downloads/My File.txt' .

In the second command, $HOME is not expanded because it is inside single quotes.

Mini-Lab: Scaffold a Project, Copy Templates, Rename, and Clean Up Safely

This mini-lab practices creating directories and files, copying templates, renaming, moving, and deleting only what you intend using patterns and interactive prompts.

1) Create a project directory structure

mkdir -p lab-project/{templates,src,build,docs}

2) Create a few template files

touch lab-project/templates/index.html lab-project/templates/app.js lab-project/templates/README.md

3) Copy templates into the working folders

Copy individual files into src and docs.

cp -v lab-project/templates/index.html lab-project/src/ cp -v lab-project/templates/app.js lab-project/src/ cp -v lab-project/templates/README.md lab-project/docs/

Now practice copying a whole directory (for example, making a backup of templates).

cp -rv lab-project/templates lab-project/templates.bak

4) Rename and move files with mv

Rename README.md to a more specific name, and move it if desired.

mv -iv lab-project/docs/README.md lab-project/docs/Project-Notes.md

Create a file with spaces and then rename it safely using quotes.

touch "lab-project/docs/Meeting Notes.md" mv -iv "lab-project/docs/Meeting Notes.md" "lab-project/docs/Meeting-Notes.md"

5) Create some build artifacts to clean up

These represent files you might want to delete without touching sources.

touch lab-project/build/app.tmp lab-project/build/app.log lab-project/build/app.o lab-project/build/keep.me

6) Clean up only specific items using patterns (with preview)

First preview what will match, then remove interactively.

ls lab-project/build/*.tmp lab-project/build/*.log rm -iv lab-project/build/*.tmp lab-project/build/*.log

Remove only single-letter object files like a.o would match ?.o. Here we have app.o, which will not match ?.o, demonstrating how patterns can be made more specific.

ls lab-project/build/?.o

Remove app.o explicitly (or with a safe pattern you have previewed).

ls lab-project/build/*.o rm -iv lab-project/build/*.o

7) Remove empty directories with rmdir (when applicable)

If a directory is empty, rmdir is a safe way to remove it. Try removing a directory only after you have deleted its contents.

rmdir lab-project/templates.bak

If it is not empty, rmdir will fail. In that case, inspect what remains and decide whether you truly want to remove it recursively.

8) Optional: remove the entire lab project (only if you intend to)

Use an interactive recursive delete so you can confirm each step.

rm -rI lab-project

Now answer the exercise about the content:

You want to delete only the .tmp and .log files inside lab-project/build while avoiding accidental deletion. Which approach best matches safe command-line habits?

You are right! Congratulations, now go to the next page

You missed! Try again.

Safe deletion includes previewing wildcard matches with ls and using interactive removal (like rm -i) so you confirm what gets deleted. This targets only .tmp and .log files.

Next chapter

Viewing and Inspecting File Contents from the Terminal

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.