The Ultimate Guide: Master Linux File Manipulation with cp, mv, rm, cat & touch

The CyberSec Guru

Linux 101 - Master Linux File Manipulation

If you like this post, then please share it:

Buy me A Coffee!

Support The CyberSec Guru’s Mission

🔐 Fuel the cybersecurity crusade by buying me a coffee! Why your support matters: Zero paywalls: Keep the main content 100% free for learners worldwide.

“Your coffee keeps the servers running and the knowledge flowing in our fight against cybercrime.”☕ Support My Work

Buy Me a Coffee Button

In our journey so far, we’ve covered incredible ground. In Day 1, we learned what Linux is. In Day 2, we installed it. In Day 3, we faced the “scary black screen” and learned why the CLI is a superpower. And in Day 4, we became masters of our domain. You learned how to navigate the entire “world” of the Linux Filesystem (FHS) with your new “GPS” (pwd), “Eyes” (ls), and “Legs” (cd).

You are no longer a “Terminal tourist.” You are a “Terminal navigator.”

Today, the training wheels come off.

Until now, we have been passive observers. We’ve been “looking” (ls) and “moving” (cd), but we haven’t changed anything. We’ve been in “read-only” mode.

Today, you become an architect, a builder, and a destroyer. You are going to learn how to manipulate the filesystem itself. You will learn to create, view, copy, move, rename, and—most importantly—delete files and directories.

This is the day you gain real power. And with that power comes real, irreversible responsibility.

In the GUI (Graphical User Interface), you are protected. When you “delete” a file, it goes to a “Trash Can.” You can get it back. In the CLI (Command-Line Interface), there is no Trash Can. Deletion is not “delete.” It is “shred.” It is “vaporize.” It is permanent.

This is not a game. These commands are real, and they do exactly what you tell them to, for better or for worse.

In this ultimate guide, we will master the seven fundamental commands of file manipulation. We will leave no flag unturned, no option unexplored. By the end of this, you will have the complete “builder’s toolkit” for the command line.

The “Builders”:

📬 Stay Ahead of Cyber Threats

Get the latest cybersecurity news, critical vulnerabilities, threat intelligence, tutorials, and exclusive giveaways delivered straight to your inbox. No spam. Unsubscribe anytime.

Subscribe to the Newsletter →
  • mkdir (Make Directory)
  • touch (Create/Update File)

The “Viewer”:

  • cat (Concatenate / View File)

The “Movers”:

  • cp (Copy)
  • mv (Move / Rename)

The “Destroyers”:

  • rmdir (Remove Empty Directory)
  • rm (Remove)

Let’s begin.

The “Builders” – mkdir & touch

Before you can organize, you need something to organize. Our first two commands are the “builders.” They create the structure (directories) and the content (files) for our world.

mkdir (Make Directory): Your Digital “New Folder” Button

In your GUI, you right-click and hit “New Folder.” In the CLI, you use mkdir. This command’s job is simple: it creates a new, empty directory.

Let’s use the core loop from Day 4.

  1. cd (Go to your home directory)
  2. pwd (Confirm you are at /home/yourname)
  3. ls -l (Look at what’s there)
  4. mkdir my-project (This is our command)
  5. ls -l (Look again)

You will now see a new line: drwxr-xr-x 2 yourname yourname 4096 Oct 31 10:30 my-project

You did it. You created a new directory. You can cd my-project and you are now “inside” your new, empty room.

But this is the “basic” way. The real power of mkdir comes from one simple, game-changing option: -p.

The Killer Flag: mkdir -p (parents)

Let’s say you want to organize a new project. You want to create a full structure, like: projects/client-A/invoices.

Let’s try the “basic” way from inside your home directory: mkdir projects/client-A/invoices

The shell will hit you with an error: mkdir: cannot create directory 'projects/client-A/invoices': No such file or directory

What does this mean? It means mkdir is “dumb.” It tried to “walk” into the projects directory to create client-A, but projects doesn’t exist. So it failed.

This is where mkdir -p comes in. The -p flag stands for parents. It tells mkdir: “I don’t care if the parent directories don’t exist. Create them for me.

Let’s try again, the pro way: mkdir -p projects/client-A/invoices

This time… silence. No error. Now run ls -R projects:

projects:
client-A

projects/client-A:
invoices

projects/client-A/invoices:
'No such file or directory' error for a basic 'mkdir' command
‘No such file or directory’ error for a basic ‘mkdir’ command

In one command, you created three nested directories. This is the only way you should create directories from now on. It’s “idempotent”—a fancy word that means “it won’t fail if it’s already done.” If projects/client-A already exists, mkdir -p will just shrug, see that it’s there, and only create invoices. It’s safer, smarter, and faster.

The “Feedback” Flag: mkdir -v (verbose)

When you run mkdir -p, silence means success. But sometimes, you want feedback. You want the command to tell you what it’s doing. For this, we add the -v (verbose) flag.

Let’s combine them for the ultimate mkdir command: mkdir -pv projects/client-B/assets

The output will now tell you what it’s doing: mkdir: created directory 'projects/client-B' mkdir: created directory 'projects/client-B/assets'

You are now a master of mkdir.

touch: The “File Creator” with a Secret Identity

Now that we have “folders,” we need “files.” touch is the standard way to create an empty file.

Go into your new project: cd projects/client-A touch README.md touch notes.txt touch main.py

Now run ls -lh:

-rw-r--r-- 1 yourname yourname 0 Oct 31 10:35 main.py
-rw-r--r-- 1 yourname yourname 0 Oct 31 10:35 notes.txt
-rw-r--r-- 1 yourname yourname 0 Oct 31 10:35 README.md

You’ve just created three new files. Notice their size: 0 bytes. touch just creates the “container” (the inode, in Linux terms). It’s an empty placeholder, ready for you to add content to (which we’ll do in a moment). You can also create many files at once.

This is the “beginner” use case for touch. But it’s a lie.

The Real Job of touch: The touch command’s real purpose is not to “create” files. It’s to “touch” them, to update their timestamps.

Let’s do an experiment.

  1. ls -lh notes.txt -rw-r--r-- 1 yourname yourname 0 Oct 31 10:35 notes.txt (Note the time: 10:35)
  2. Wait one minute.
  3. Now, “touch” the file that already exists: touch notes.txt
  4. ls -lh notes.txt -rw-r--r-- 1 yourname yourname 0 Oct 31 10:36 notes.txt

The file size is still 0, but the timestamp is now 10:36. You “bumped” the file, “touching” it to update its “Last Modified” time to right now.

Why is this useful? In the world of programming, many build-systems (like make) work by checking timestamps. They look at all your source code files and your final program. If main.py is newer than program.exe, the build system knows it needs to re-compile. touch is a way to “trick” the build system into rebuilding a file by making it seem “new.”

If you only want to create a new, empty file, touch is a wonderful side-effect. If the file doesn’t exist, touch says, “Well, I can’t update its timestamp… so I’ll just create it for you with the current time.”

Advanced touch (The Timestamp Flags): touch is a time-machine.

  • touch -a notes.txt: Updates the Access Time only (the “Last Read” time).
  • touch -m notes.txt: Updates the Modification Time only (the “Last Written” time). This is the default.
  • touch -d "2 days ago" notes.txt: Set the timestamp to a specific time. ls -l will now show it was “modified” two days ago.
  • touch -r old-file.txt new-file.txt: The -r (reference) flag is the coolest. It copies the exact timestamp from old-file.txt and applies it to new-file.txt.

You are now a “builder.” You can create the entire scaffolding of a project (mkdir -p) and all its empty files (touch) without ever leaving your keyboard.

The “Viewer” – cat (Concatenate)

Now that you have files, you need to see what’s in them. (First, let’s put something in them. We’ll use the echo command from Day 3, with the > (redirection) operator we touched on).

echo "Hello, this is file A." > file-A.txt echo "And this is file B." > file-B.txt

Now we have two, non-empty files. The simplest way to “view” a file is with cat. cat file-A.txt

The terminal will print: Hello, this is file A.

cat “reads” the file and “streams” its contents to “Standard Output” (which is, by default, your screen).

But cat‘s name is not “view.” It’s cat (Concatenate). cat‘s real job is to chain files together, one after the other. cat file-A.txt file-B.txt

The output will be:

Hello, this is file A.
And this is file B.

It “catenated” them. This is wildly powerful.

The Real Use of cat: cat is a “builder,” not a “viewer.” We use it with the redirection operator (>).

1. Combining Files: cat file-A.txt file-B.txt > combined-file.txt This command does not print to the screen. The > catches all the output and redirects it into a new file. If you now cat combined-file.txt, you’ll see:

Hello, this is file A.
And this is file B.

You just combined two files into one.

2. Appending Files: What if you want to add to combined-file.txt? If you use > again, it will overwrite and destroy the contents. The secret is >> (the “append” operator).

echo "This is a new, third line." > file-C.txt cat file-C.txt >> combined-file.txt cat combined-file.txt

Output:

Hello, this is file A.
And this is file B.
This is a new, third line.

You used cat to append one file to the end of another. This is how log files are built.

The DANGER of cat (and the Right Way to View Files)

cat is a “dumb” tool. It “dumps.” This is fine for small text files. But what if you cat a 500MB log file? It will flood your terminal for a full minute. What if you cat a binary file, like a program or a JPG image?

DO NOT DO THIS, BUT UNDERSTAND IT: cat /bin/ls

Your terminal will break. You will see a waterfall of gibberish, control characters, and beeps. cat is trying to “read” machine code as text, and your terminal emulator tries to interpret that gibberish, breaking its own settings. (If you do this, type reset and press Enter. It might fix it. You’ll probably have to just close the terminal window.)

Terminal Showing the Screen Filled with Unreadable Gibberish
Terminal Showing the Screen Filled with Unreadable Gibberish

The “Real” Viewers: less and more cat is for small files or concatenating. The professional tool for “viewing” is less. less combined-file.txt

This will open the file in a “pager.” It only shows you one screen at a time.

  • Use the Arrow Keys or j (down) / k (up) to scroll.
  • Press / to search for a word.
  • Press q to (q)uit and return to your shell.

less is “less-is-more” (its name is a joke on the older more command). It’s safe, fast, and can open gigabyte-sized files instantly. Rule: cat for tiny files (under 50 lines). less for everything else.

Useful cat Flags: Even as a viewer, cat has tricks:

  • cat -n combined-file.txt: n (numbers) all lines. 1 Hello, this is file A. 2 And this is file B. 3 This is a new, third line.
  • cat -b combined-file.txt: b (body) only numbers non-blank lines. (More useful for code).

The “Movers” – cp & mv

This is the core of your toolkit. You can make files, you can see files. Now, you need to manage them. cp and mv are “copy/paste” and “cut/paste.”

cp (Copy): The “Cloning” Tool

cp is your “copy/paste” command. It leaves the original and creates a new, identical clone. The grammar is critical: cp [OPTIONS] SOURCE DESTINATION

This grammar has three main “forms.”

Form 1: File to File (Rename/Backup) cp file-A.txt file-A_backup.txt ls will now show both files. file-A.txt and file-A_backup.txt. This is the simplest “make a quick backup” command.

Form 2: File(s) to Directory (The “Put-in-a-Box” Command) This is the most common use. mkdir my-backups cp file-A_backup.txt my-backups/ ls my-backups/ file-A_backup.txt The file is now inside the my-backups directory.

You can also copy many files at once. cp file-A.txt file-B.txt combined-file.txt my-backups/ All three files are now copied into my-backups/. Rule: When copying multiple items, the last argument MUST be a directory.

Form 3: Directory to Directory (The “Recursive” Command) This is the “Aha!” moment that all beginners hit. Let’s go to our project: cd projects/client-A We have files in here. Let’s go back up: cd .. Now, let’s try to copy the entire client-A directory: cp client-A/ client-A-backup/

The shell hits you with an error: cp: -r not specified; omitting directory 'client-A/'

This is a safety feature. cp is telling you, “Whoa, client-A is a directory. I don’t copy directories by default. What if it has 10,000 files in it? Are you sure?”

To be sure, you must use the -r (recursive) flag. cp -r client-A/ client-A-backup/

 'Omitting directory' error for 'cp', contrasted with the success of the 'cp -r' command
‘Omitting directory’ error for ‘cp’, contrasted with the success of the ‘cp -r’ command

This command tells cp: “Yes, I’m sure. Go into client-A, grab everything inside it, go into all its sub-folders, grab everything in them, and copy them all to a new directory called client-A-backup.”

This is the only way to copy a directory.

The “Pro” cp Flags (The “Backup” Toolkit)

cp -r is good, but “pro” backups are more complex. What about permissions? What about timestamps? A simple cp -r loses all that. The new files will be stamped with today’s date.

  • <b>-v</b> (verbose): Tells you every single file it copies. cp -rv client-A/ client-A-backup/ 'client-A/notes.txt' -> 'client-A-backup/notes.txt'
  • <b>-i</b> (interactive): The “safety” flag. If client-A-backup already has a notes.txt, this will prompt you: cp: overwrite 'client-A-backup/notes.txt'? You must press y or n. This is critical for not accidentally destroying work.
  • <b>-p</b> (preserve): This is the “time-machine” flag. It preserves the original file’s metadata: modification time, access time, and (most importantly) permissions.
  • <b>-a</b> (archive): This is the super-flag. This is the one all system administrators use. cp -a client-A/ client-A-backup/ cp -a is a “shortcut” for cp -r -p (and some other advanced flags). It means: “Make an archive-quality, perfect clone. Be recursive, and preserve all the timestamps and permissions.”

Rule: When backing up a project, cp -a is the command you want.

mv (Move): The “Two-in-One” Tool (Move & Rename)

mv is your “cut/paste” command. But it’s also your “rename” command. This confuses beginners, but it’s pure logic.

To the Linux filesystem, “renaming” a file from old.txt to new.txt is the same as “moving” it from one name to the other.

The grammar is identical to cp: mv [OPTIONS] SOURCE DESTINATION

Use Case 1: Rename a File You are not providing a directory. You are providing two file names. mv old-name.txt new-name.txt ls The old-name.txt is gone. It has been renamed to new-name.txt.

Use Case 2: Move a File You are providing a file and a directory. mv new-name.txt my-backups/ ls The new-name.txt is gone from the current directory. It has been moved into my-backups/.

Use Case 3: Move and Rename a File (The “Pro” Move) You are providing a file, a directory, and a new name. mv combined-file.txt my-backups/FULL-BACKUP.txt ls The combined-file.txt is gone. ls my-backups/ FULL-BACKUP.txt (This is the new name of the file inside its new location).

The BIG mv vs. cp Difference

Let’s try to move a directory: mv client-A-backup/ client-B-backup/

It… just works. Instantly. Why? mv does not need a -r (recursive) flag. cp -r has to read every byte from client-A and write a new copy to client-A-backup. This takes time (if the directory is 50GB, it takes a long time). mv does not move any data. It just “renames” the directory. It’s an instantaneous operation. It’s just “peeling the label” off one box and “sticking it” on another.

“Pro” mv Flags (The “Safety” Toolkit): mv is dangerous because it can overwrite files without asking. mv file-A.txt file-B.txt (This is “rename”). What if file-B.txt already exists? …mv will instantly destroy it and replace it with file-A.txt. No prompt. No warning.

This is why you must know these flags:

  • <b>-i</b> (interactive): The most important mv flag. mv -i file-A.txt file-B.txt If file-B.txt exists, it will prompt you: mv: overwrite 'file-B.txt'? This flag will save your career one day. Many pros “alias” mv to mv -i so it’s always on.
  • <b>-n</b> (no-clobber): The opposite. This is for scripts. It means “if the destination already exists, just fail silently. Do not overwrite.”
  • <b>-v</b> (verbose): Tells you what it did. mv -v old-name.txt new-name.txt renamed 'old-name.txt' -> 'new-name.txt'

The “Destroyers” – rmdir & rm

This is the most important, and most dangerous, part of this guide. You must read this section, re-read it, and then read it again.

The Great “Trash Can” Lie: In your GUI (Windows, macOS), you “delete” a file. It goes to a “Trash Can” or “Recycle Bin.” You can open it and “restore” the file. This has made you careless. You think “delete” is a temporary state. In the Linux CLI, THERE IS NO TRASH CAN. rm is not “move to trash.” rm is “remove.” It is “shred.” It is “vaporize.” When you use rm, the file is gone. Instantly. Irreversibly. There is no “undo.”

GUI Delete vs CLI Delete
GUI Delete vs CLI Delete

rmdir (Remove Directory): The “Safe” Destroyer

Let’s start with the “safe” command. rmdir is the opposite of mkdir. But it has one, critical safety-feature: it only works on empty directories.

mkdir temp-folder ls (You see temp-folder) rmdir temp-folder ls (It’s gone) That worked.

Now, let’s try this: mkdir temp-folder-2 touch temp-folder-2/file.txt (We put a file in it) rmdir temp-folder-2

The shell hits you with an error: rmdir: failed to remove 'temp-folder-2': Directory not empty

This is a good thing! rmdir is protecting you. It’s saying, “I can’t delete this, it has stuff in it! Go check it yourself.” This makes rmdir a fantastic, safe command for “cleaning up” empty folders you know you don’t need.

rm (Remove): The “Real” Destroyer

This is the command with all the power. rm‘s job is to delete.

  • Basic Usage (Deleting Files): touch file-to-delete.txt rm file-to-delete.txt ls (It’s gone. Forever.)
  • Deleting Multiple Files: rm file-A.txt file-B.txt (Both are gone)

This is simple enough. The danger comes when you combine it with directories and flags.

The Safety Flag: rm -i (interactive)

Just like with cp and mv, rm can be run “interactively.” rm -i file-to-delete.txt rm: remove regular empty file 'file-to-delete.txt'? You must type y (yes) or n (no).

Rule for Beginners: Always use rm -i. Go into your .bashrc file (which we learned about in Day 4!) and add this line to the bottom: alias rm='rm -i' Save the file, restart your shell, and now every time you type rm, the system will actually run rm -i. This alias will save you.

The “Directory” Flag: rm -r (recursive)

This is where the power (and danger) escalates. You try to delete your project: rm client-A-backup/

The shell, like with cp, will stop you: rm: cannot remove 'client-A-backup/': Is a directory rm by default will not touch directories. This is its last safety net. To “cut” that safety net, you use the -r (recursive) flag.

rm -r client-A-backup/ This command says, “Go into client-A-backup. Delete everything inside it. Then, delete the directory itself.” If you combine it with -i, you’ll be prompted for every single file and every subdirectory. rm -ri client-A-backup/ rm: descend into directory 'client-A-backup/'? y rm: remove regular empty file 'client-A-backup/notes.txt'? y …this is tedious, but very safe.

The “Force” Flag: rm -f (force)

This is the “shut up and do it” flag. rm -f overrides many protections.

  • If a file doesn’t exist, rm will normally complain. rm -f will not. It will just shrug and move on.
  • If a file is “write-protected,” rm will prompt you. rm -f will not. It will delete it anyway.
  • Most importantly, rm -f completely overrides -i. If you have an alias rm='rm -i', typing rm -f cancels the interactive prompt.

This is the “no-questions-asked” flag. It is the ultimate tool for “forceful” deletion in automated scripts.

The “Unholy Combination”: rm -rf

You now know the three parts.

  • rm: Remove
  • -r: Recursively
  • -f: Forcibly

rm -rf DIRECTORY_NAME This is the single most dangerous command in all of Linux. It means: “I want to delete DIRECTORY_NAME. Do it recursively, deleting all 10,000 files and 500 subfolders inside it. And do it forcibly. Do not prompt me. Do not stop for anything. I don’t care about permissions. I don’t care if files don’t exist. Just make it gone.”

This command is a scalpel. It is a “foot-gun.” It is a “chainsaw with no guard.” It is also extremely useful. 99% of the time, when you want to delete a project, you will use rm -rf my-project-folder/. It’s fast, it’s clean, it’s efficient.

But you must respect it. You must check your spelling. rm -rf my-project/ (This is what you wanted). rm -rf /my-project (This is NOT the same. The leading / means you’re starting from the root directory.) rm -rf my -project/ (The space… you are now trying to delete a folder named my and a folder named -project/, which is not what you wanted.)

The Ultimate Warning: sudo rm -rf /

Let’s break down the command that has destroyed more careers than any other. sudo rm -rf /

  • sudo: “Run this as the Super User (root).” (With God-like permissions).
  • rm: Remove
  • -r: Recursively
  • -f: Forcibly
  • /: …starting from the root directory of the entire filesystem.

This command literally tells the computer: “As the administrator, start at the trunk of the filesystem (/) and forcibly delete every single file and folder on this computer.” It will wipe /bin (all your commands). It will wipe /etc (all your settings). It will wipe /home (all your data). It will continue to run, vaporizing your entire OS, until the kernel itself (running in memory) crashes because it can’t find its own files on disk.

This will brick your system. (Most modern Linux systems have a “fail-safe” called --no-preserve-root that you now have to add, but the principle remains).

You have been warned. You now know the difference between the “safe” destroyer (rmdir) and the “chainsaw” (rm -rf). Use your power wisely.

A “Safer” rm -rf: When deleting a project, get in the habit of doing this: rm -rf ./my-project The ./ in front of the name is a “relative path” that means “a folder named my-project inside the directory I am currently standing in (.).” This small habit prevents you from ever accidentally running it on / if you mis-type something.

You Are Now an Architect

You’ve done it. This was the “high-stakes” day. You are no longer just a “navigator” who can “look but not touch.” You are an architect.

  • You can build entire structures from scratch (mkdir -p) and lay the “foundation” files (touch).
  • You can view files (cat), but you know the real purpose of cat is to build bigger files, and the real viewer is less.
  • You can manage your world, making perfect backups (cp -a), moving files (mv), and renaming them (mv again).
  • And you can destroy, with the “safe” (rmdir) and “dangerous” (rm -rf) tools, and you understand the critical difference and the respect this power demands.

You now have the entire basic toolkit for file manipulation. The “core loop” of a Linux professional isn’t just pwd/ls/cd. It’s pwd/ls/cd… then mkdir/cp/mv/rm.

With this, you can manage any file, anywhere.

But there’s one piece missing. We saw that ls -l shows drwxr-xr-x. We know d means “directory,” but what does rwx really mean? And how do you change it?

In our next guide (Day 6), we’ll master that final piece: Permissions. You’ll learn how to use chmod and chown to take full control of who can read, write, and execute your files.

Buy me A Coffee!

Support The CyberSec Guru’s Mission

🔐 Fuel the cybersecurity crusade by buying me a coffee! Your contribution powers free tutorials, hands-on labs, and security resources.

Why your support matters:
  • Writeup Access: Get complete writeup access within 12 hours
  • Zero paywalls: Keep the main content 100% free for learners worldwide

Perks for one-time supporters:
☕️ $5: Shoutout in Buy Me a Coffee
🛡️ $8: Fast-track Access to Live Webinars
💻 $10: Vote on future tutorial topics + exclusive AMA access

“Your coffee keeps the servers running and the knowledge flowing in our fight against cybercrime.”☕ Support My Work

Buy Me a Coffee Button

If you like this post, then please share it:

Linux 101

Discover more from The CyberSec Guru

Subscribe to get the latest posts sent to your email!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from The CyberSec Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading