Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux mv and rm Commands: Move, Rename and Delete Files

Let's start. We can make files and folders, and we can copy them. On a laptop the other everyday jobs are cut-paste, rename and delete. On a server, two commands do all three: mv moves and renames, rm deletes. We also learn to tell a file from a folder without guessing, how to hide a file with a dot, and what the mysterious . and .. are. Keep your server running and type every command with me.

In this class

  • The practice folders, and the real prompt after cd
  • An interview question: copy only what is inside a folder
  • mv = cut-paste: four ways to move a file into a folder
  • Renaming with mv, in the same folder and inside a subfolder
  • Moving folders, and why mv needs no -r (same disk vs across disks)
  • Wildcards with mv, and the silent overwrite (mv -i)
  • Deleting: rm, rm -r, rmdir and rm -i
  • Practice task: mv, cp and rm on one tree
  • Working smart: relative paths, braces and Tab
  • File or folder? Why names and colours don't decide
  • ls -l: the first character
  • The command summary
  • Hidden files, ls -a, and the entries . and ..
  • Before you leave: terminate or stop
  • Try-at-home tasks

1. The practice folders

Moving and deleting need something to move and delete. Build these first. Copying is in Linux cp Command with Examples (Copy Files and Folders); paths are in Linux Directory Commands: pwd, ls, cd, mkdir for Beginners.

How (start in your home folder). Run these lines one by one:

cd
mkdir ravi raj
cd ravi
touch 1.txt 2.logs 3.mp3
mkdir one two three
cd

Classroom line

Say we're in ec2-user's home, and I've made two folders there: one called ravi, one called raj.

Watch the prompt. It always tells you where you are. On Amazon Linux 2023 it looks like this (the ip-... part is your server's private hostname):

[ec2-user@ip-172-31-xx-xx ~]$ cd ravi
[ec2-user@ip-172-31-xx-xx ravi]$ cd
[ec2-user@ip-172-31-xx-xx ~]$
  1. ~ means you are in your home, /home/ec2-user.
  2. After cd ravi the prompt shows ravi, the name of the current folder.
  3. After cd alone, you are home again and ~ comes back.

The prompt changes with you: after cd ravi it shows the name of the folder you are in, here ravi, instead of ~. Check it after every cd, or run pwd.

2. Interview question: copy only what is inside a folder

"Copy the contents of ravi into raj, not the folder itself" is a favourite interview question. It tests whether you really understand cp -r.

Classroom line

And they ask this question in interviews.

Three tries (from your home folder):

  1. cp ravi raj → cp: -r not specified; omitting directory 'ravi'. A folder needs -r.
  2. cp -r ravi raj → works, but you get raj/ravi/...: the folder itself went inside raj.
  3. cp -r ravi/* raj → raj/1.txt, raj/2.logs, raj/3.mp3, raj/one... Only the contents. This is the answer they expect.

ravi/* means "every name inside ravi". The shell expands it before cp runs, so cp sees ravi/1.txt ravi/2.logs ravi/3.mp3 ravi/one ravi/three ravi/two raj.

The catch: hidden files. * doesn't match names that start with a dot (section 18). Make one and try again:

touch ravi/.notes
rm -r raj/*
cp -r ravi/* raj
ls -A raj

.notes is missing. To copy everything, hidden files included, copy the folder's own .:

cp -r ravi/. raj
Copy the folder, or only what is inside it? ravi contains: 1.txt 2.logs 3.mp3 one/ and a hidden file .notes $ cp -r ravi raj raj/ └── ravi/ ├── .notes 1.txt 2.logs └── 3.mp3 one/ the folder itself goes inside raj $ cp -r ravi/* raj raj/ ├── 1.txt 2.logs └── 3.mp3 one/ .notes is NOT copied contents only; * skips hidden files $ cp -r ravi/. raj raj/ ├── .notes 1.txt 2.logs └── 3.mp3 one/ all the contents, hidden files too $ cp ravi raj cp: -r not specified; omitting directory 'ravi' ravi/* = every visible name inside ravi. ravi/. = the folder ravi itself, seenfrom inside, so everything in it.

Figure 1. cp -r ravi raj puts the folder itself inside raj. cp -r ravi/* raj copies only the contents but skips the hidden file .notes. cp -r ravi/. raj copies all the contents, hidden files too.

cp -r ravi/* raj works for normal files, but ravi/* skips hidden files (names that start with a dot). cp -r ravi/. raj copies everything inside ravi, hidden files included.

3. mv: cut-paste for Linux

On Windows you cut a file and paste it somewhere else; the original is gone from the old place. Linux does the same with one command.

Classroom line

mv means cut-paste. The cut-paste we use in Windows is what we now do in Linux.

mv SOURCE DESTINATION, the same shape as cp. The difference: after mv, the source is gone from where it was.

Four ways to move my.txt into ravi. Make the file first with touch my.txt in your home. Each line moves it away, so run touch my.txt again before trying the next one:

  1. mv my.txt ravi → ravi/my.txt
  2. mv my.txt ravi/ → the same; Tab adds the slash for you.
  3. mv my.txt ravi/my.txt → the same, with the name written out.
  4. mv my.txt ravi/you.txt → moved and renamed: ravi/you.txt.

Check each time: ls (my.txt is gone from home) and ls ravi (it's there).

Handwritten board: a prompt with mv my.txt ravi, with source written under my.txt and destination under ravi; below it three lines mv my.txt ravi/, mv my.txt ravi/my.txt and mv my.txt ravi/you.txt.

Whiteboard: mv my.txt ravi: my.txt is the source, ravi the destination. Then the three other ways: mv my.txt ravi/, mv my.txt ravi/my.txt, and mv my.txt ravi/you.txt, which also renames the file.

4. Renaming with mv

Linux has no separate rename command for everyday use. Renaming is just moving a file to a new name in the same place.

Classroom line

mv is also the command for renaming.

  1. touch my.txt
  2. mv my.txt our.txt
  3. ls → our.txt (and no my.txt)

How mv decides:

  1. Destination is an existing folder → the item moves inside it.
  2. Destination doesn't exist → the item gets that name (rename).
  3. Destination is the same as the source → nothing to do.

Try it:

mv my.txt my.txt
mv: 'my.txt' and 'my.txt' are the same file
One command, four jobs: mv $ mv my.txt ravi before ~/ ├── my.txt └── ravi/ after ~/ └── ravi/ └── my.txt 1. Move: ravi is a folder, so my.txt goes inside it. Nothing stays behind. $ mv my.txt our.txt before ~/ └── my.txt after ~/ └── our.txt 2. Rename: our.txt does not exist, so my.txt just gets the new name. $ mv my.txt ravi/you.txt before ~/ ├── my.txt └── ravi/ └── one/ after ~/ └── ravi/ ├── one/ └── you.txt 3. Move and rename in one step. $ mv ravi one before ~/ ├── one/ └── ravi/ └── 1.txt after ~/ └── one/ └── ravi/ └── 1.txt 4. Move a folder: everything inside goes with it. No -r needed. Rule: if the destination is an existing folder, mv puts the item inside it.Otherwise the destination is the new name.

Figure 2. Animation: mv my.txt ravi moves the file into ravi; mv my.txt our.txt renames it; mv my.txt ravi/you.txt moves and renames; mv ravi one moves a whole folder, without -r.

5. Renaming inside a subfolder, without going there

You don't have to cd into a folder to rename something in it. Give both names as paths.

How (you stay in /home/ec2-user):

  1. mv ravi/1.txt ravi/2.txt
  2. ls ravi → 2.txt is there, 1.txt is gone.

Both paths point into the same folder, so it's a rename. If the second path pointed into another folder, it would be a move and rename.

Handwritten board: a prompt with mv my.txt our.txt; on the right a small tree ec2-user, ravi, 1.txt; below, a prompt with mv ravi/1.txt ravi/2.txt and a line mv ravi one.

Whiteboard: mv my.txt our.txt renames in the same folder. With the tree ec2-user, ravi, 1.txt on the right: mv ravi/1.txt ravi/2.txt renames a file inside ravi without leaving home, and mv ravi one moves the folder ravi into one.

6. Moving folders: no -r

With cp, a folder needs -r. With mv, it doesn't. Good to know before an interviewer asks "why?".

  1. mkdir one (in your home; ravi already exists)
  2. mv ravi one
  3. ls one → ravi, with everything that was inside it.

To bring it back: mv one/ravi . (. = here).

The option is dash r: a hyphen, -r. A slash is the path separator, as in ravi/1.txt.

7. Why mv needs no -r

Understanding this explains why mv of a huge folder takes no time at all, while cp of the same folder can take minutes.

Tell a rename from copying to a different disk

On one filesystem, the directory holds a name pointing to a file’s data. A rename updates those directory entries. This is why moving a large lab folder sideways within the same filesystem can feel instant. Moving it to a second mounted filesystem gives mv a different job. It has to copy the data to the destination, then remove the source after copying succeeds.

Before moving a large directory to an attached EBS data disk, compare df -h /path/to/source /path/to/destination using existing paths. If the paths are on different filesystems, check destination free space. For important files, use a verified copy and check the destination before separately removing the original. Inspect both sides if a move reports an error.

Classroom line

When we move, what really happens? Only the path changes. But when we copy, the file is actually stored again.

What happens on the same disk:

  1. A file's data sits in blocks on the disk.
  2. The folder only keeps an entry: the name, and where the data is.
  3. cp writes the data again into new blocks, file by file. For a folder it has to go inside every folder: that's what -r asks for.
  4. mv leaves the data alone and only changes the entry: the old name goes, the new path appears. A folder moves as a whole, in one step, so there is nothing to recurse into.

Across disks (for example from the root volume to a second EBS volume), an entry can't point to another disk. So mv copies the data to the other disk, then deletes the original. It takes as long as a copy, and it still needs no -r.

Why mv is instant and needs no -r (same disk) $ cp my.txt ravi folder entries: name → where the data is my.txt → blocks 1-3 ravi/my.txt → blocks 6-8 the data is written again, file by file; for a folder, -r tells cpto go inside $ mv my.txt ravi folder entries: name → where the data is my.txt → ravi/my.txt → blocks 1-3 no data is touched, only the name entry changes, so a foldermoves as a whole Across disks (for example to a second EBS volume) mv copiesthe data, then deletes the original. Still no -r.

Figure 3. Animation: cp my.txt ravi writes the data again into new blocks and adds a second entry. mv my.txt ravi on the same disk leaves the blocks alone and only changes the entry to ravi/my.txt. Across disks, mv copies, then deletes.

One move mv refuses, for the same reason as cp in the last chapter. Make one/two with mkdir -p one/two, then try:

mv one one/two
mv: cannot move 'one' to a subdirectory of itself, 'one/two/one'

On the same disk (filesystem), mv only renames the directory entry, so no data is copied. Across disks it copies and then deletes. Either way, no -r is needed.

8. Wildcards with mv

Same as with cp: describe a pattern instead of typing every name.

Classroom line

We can use wildcards while moving files or folders too.

  1. mv *.txt ravi → every name ending in .txt goes into ravi.
  2. mv 2.* ravi → 2.logs and anything else starting with 2..
  3. Preview first: echo *.txt shows what the pattern picks, without moving anything.

The * and ? rules are in the copy chapter.

9. mv overwrites without asking: mv -i

If the destination file already exists, mv replaces it. No question, no message, and the old file is gone.

See it happen:

echo old > b.txt
echo new > a.txt
mv a.txt b.txt
cat b.txt

Output: new. The old b.txt is lost, and a.txt no longer exists.

The safe way: mv -i asks first:

mv -i a.txt b.txt
mv: overwrite 'b.txt'?

Type y to replace, n to keep b.txt.

Ravindra Bagale's Tip

When you move files into a folder that already has files, use mv -i. One n can save a file you can't get back.

10. rm: deleting files

Old logs, test files, mistakes. Servers fill up, and a full disk stops everything.

Decide which names will disappear

In your disposable lab folder, run pwd and then printf '%s ' *.logs before acting on a pattern. That list shows the names the shell would pass to rm. If a name appears that you meant to keep, stop and narrow the pattern or choose one explicit file. A pattern can select more than one kind of file if the names happen to match.

For your first test, use rm -i -- ./old-test.logs after creating that throwaway file. -i asks before removing it. The -- separates options from filenames, useful if a file starts with a dash. Run ls -la after the prompt and confirm that only the intended name is gone.

  1. rm my.txt → one file.
  2. rm 1.txt my.mp3 3.logs → several files at once.
  3. rm *.txt → every .txt file here.
  4. rm file* → every name that starts with file.
Handwritten board: four lines, rm my.txt with a tick, rm 1.txt my.mp3 3.logs, rm *.txt, and rm file*.

Whiteboard: rm my.txt deletes one file, rm 1.txt my.mp3 3.logs deletes three, rm *.txt every name ending in .txt, and rm file* every name starting with file.

Things to know:

  1. There is no recycle bin. Deleted means gone.
  2. For ec2-user, rm doesn't ask before deleting.
  3. A name that doesn't exist: rm: cannot remove 'nothere': No such file or directory.
  4. A folder isn't deleted by plain rm.

Try it on a folder:

rm ravi
rm: cannot remove 'ravi': Is a directory

11. rm -r: deleting a folder and everything in it

rm alone refuses folders. When a whole folder of old builds or logs must go, you need rm -r, and you need to respect it: there is no Recycle Bin on a Linux server.

rm -r ravi deletes the folder ravi, every file in it, every folder in it, all the way down. -r = recursive, the same idea as cp -r.

  1. rm -r ravi
  2. ls → ravi is gone.

Ask before each delete:

rm -i my.txt
rm: remove regular empty file 'my.txt'?

For a folder, combine it with -r:

rm -ri ravi
rm: descend into directory 'ravi'?

rm -ri asks for every file and folder inside, one by one. Answer n at any point to keep that item.

rm -r on a single file costs nothing extra. The reason not to type -r out of habit is safety: with -r, one wrong name deletes a whole folder instead of failing with Is a directory. Use plain rm for files, rm -r only when you mean a folder, and rm -i when in doubt.

Ravindra Bagale's Tip

Before rm with a wildcard, run ls with the same pattern: ls *.txt, then rm *.txt. You see exactly what will go. And read any rm -r line twice before pressing Enter. More on this in 3.3 Working with files and directories.

12. rmdir: deleting only empty folders

rmdir is a safety net. It refuses to delete a folder that still has something inside.

Look for the hidden item before trying again

Make an empty hide-lab directory in your practice area and run touch hide-lab/.keep. A normal ls hide-lab looks blank. Now rmdir hide-lab reports that the directory is not empty. Check with ls -A hide-lab. That form lists .keep without the special entries . and ...

Read the error as information about the folder. In the test, you know exactly why the hidden file is there, so remove that one throwaway file with rm -i -- hide-lab/.keep and try rmdir again. On a real server, check the purpose of any hidden file before removing it.

  1. mkdir empty
  2. rmdir empty → gone, no message.
  3. rmdir my.txt → rmdir: failed to remove 'my.txt': Not a directory

Now a folder with files inside:

rmdir ravi
rmdir: failed to remove 'ravi': Directory not empty
Which delete command works on what rm a file deleted an empty folder Is a directory a folder with files Is a directory rmdir a file Not a directory an empty folder deleted a folder with files Directory not empty rm -r a file works, but risky habit an empty folder deleted a folder with files everything inside gone Red boxes show the real error text. There is no recycle bin: deleted meansgone. Add -i to be asked before each delete: rm -i, rm -ri.

Figure 4. rm deletes files but says Is a directory for folders. rmdir deletes only empty folders and says Not a directory or Directory not empty otherwise. rm -r deletes anything, including a folder with everything inside.

Which one to use:

  1. A file → rm
  2. An empty folder → rmdir
  3. A folder with things inside → rm -r (and think twice)

13. Practice task: mv, cp and rm on one tree

The task. Do everything from /home/ec2-user; don't cd anywhere.

  1. Make a folder projects, inside it web, and inside web four folders: frontend, backend, database, testing.
  2. Make two files, notes.txt and report.txt, inside web, next to the four folders.
  3. Move notes.txt into backend.
  4. Move report.txt into database.
  5. Copy report.txt from database into testing.
  6. Delete report.txt from database.
  7. End state: notes.txt only in backend, report.txt only in testing.

Classroom line

Do all the work while staying in ec2-user's home.

Classroom line

We have to use all the commands: cp, mv, rm.

Try it yourself before reading on.

Solution:

cd
mkdir -p projects/web/{frontend,backend,database,testing}
touch projects/web/{notes.txt,report.txt}
mv projects/web/notes.txt projects/web/backend
mv projects/web/report.txt projects/web/database
cp projects/web/database/report.txt projects/web/testing
rm projects/web/database/report.txt

Check with tree projects:

projects
└── web
    ├── backend
    │   └── notes.txt
    ├── database
    ├── frontend
    └── testing
        └── report.txt

6 directories, 2 files
Practice task: mv, cp and rm on one tree step 0 / 4 · start: both files sit in web, next to the four folders projects/web/ ├── frontend/ backend/ ├── database/ testing/ └── notes.txt report.txt step 1 / 4 $ mv projects/web/notes.txt projects/web/backend backend/notes.txt step 2 / 4 $ mv projects/web/report.txt projects/web/database database/report.txt step 3 / 4 $ cp projects/web/database/report.txtprojects/web/testing database/report.txt testing/report.txt step 4 / 4 $ rm projects/web/database/report.txt testing/report.txt End state: notes.txt only in backend, report.txt only in testing. Everycommand runs from /home/ec2-user.

Figure 5. Animation: notes.txt is moved into backend, report.txt into database, report.txt is copied to testing, and the copy in database is removed. End state: notes.txt in backend, report.txt in testing.

Why stay in your home? ec2-user may only write inside its own home (and a few places like /tmp). Outside it you get:

mkdir /projects
mkdir: cannot create directory ‘/projects’: Permission denied

14. Working smart: relative paths, braces and Tab

In an interview task, the person who finishes first, correctly, wins. Typing less also means fewer typos.

  1. Relative paths. You're already in /home/ec2-user. Write projects/web/backend, not /home/ec2-user/projects/web/backend.
  2. Braces. One line makes many names: mkdir -p projects/web/{frontend,backend,database,testing}.
  3. Tab. Type projects/w and press Tab; the shell completes web/. Fewer keys, no spelling mistakes.

Classroom line

You're already in ec2-user, so why are you typing the path from the very start?

Classroom line

When you need many files in one place: curly braces.

15. File or folder? Names and colours don't decide

Before you rm or rmdir, you must know which one you have. Guessing from the name doesn't work.

Names don't decide:

  1. touch report → a file called report, no extension needed.
  2. mkdir photos.txt → a folder, even though its name ends in .txt.

Colours don't decide either. In ls, folders often show in blue and files in white, but that's only a display setting. The colours come from the LS_COLORS variable, which a startup script fills in (with the dircolors command) when you log in. Change the setting, use another terminal, or pipe the output, and the colours change or disappear.

What does decide: ask Linux.

  1. ls -F → folders get a / at the end: photos.txt/ report
  2. ls -l → the first character (next section).

The colour rules come from LS_COLORS (set up by dircolors), usually loaded through startup files such as ~/.bashrc or ~/.bash_profile. Colour is a display choice, not the file type.

16. ls -l: the first character

ls -l (long listing) is the reliable way to see what something is, who owns it and when it changed.

In a folder with one empty file my.txt and one empty folder ravi:

[ec2-user@ip-172-31-xx-xx ~]$ ls -l
total 0
-rw-r--r--. 1 ec2-user ec2-user 0 Jan  5 10:15 my.txt
drwxr-xr-x. 2 ec2-user ec2-user 6 Jan  5 10:15 ravi

Read it column by column:

  1. First character: - = a regular file, d = a directory.
  2. rw-r--r--: permissions (read, write, execute). That's the next topic.
  3. .: an SELinux label is attached (Amazon Linux uses SELinux).
  4. 1 / 2: the number of links. A folder has at least 2: its name, and its own ..
  5. ec2-user ec2-user: the owner and the group.
  6. 0 / 6: the size in bytes. A folder's size is the size of its own list of names, not of what's inside. On Amazon Linux (XFS) a small folder shows a small number like 6; on other systems you'll often see 4096.
  7. Jan 5 10:15: last modified.
  8. The name.

Classroom line

The very first character tells us whether it's a file or a directory.

Handwritten board: ls -l at the top; two lines, one starting with a boxed dash and rw- r-- r--, the other with a boxed d and rwx r-x r-x; ec2-user ec2-user on each line; the names my.txt and ravi on the right.

Whiteboard: A simplified ls -l: the first character, boxed, is a dash for the file my.txt and d for the folder ravi, followed by the permissions and the owner and group ec2-user. The real output also shows links, size and time.

Reading ls -l: the first character says file orfolder $ ls -l total 0 -rw-r--r--. 1 ec2-user ec2-user 0 Jan 5 10:15 my.txt drwxr-xr-x. 2 ec2-user ec2-user 6 Jan 5 10:15 ravi - / d type rw-r--r-- permissions . SELinux 1 links ec2-user owner ec2-user group 0 size Jan 5 10:15 modified my.txt name - dash = a regular file touch, cat, nano and vi make these d d = a directory (folder) mkdir makes these Permissions (r w x) are the next topic. A folder size is the size of its ownlist, not of its contents.

Figure 6. ls -l for a file and a folder, every column labelled: type, permissions, SELinux dot, links, owner, group, size, modified time and name. A dash means a file, d means a directory.

To see a folder's own line instead of its contents: ls -ld ravi.

The ls -l output above shows the type, permissions, link count, owner, group, size, date and name. The dates are examples.

17. The command summary

Job A file A folder
Copy cp cp -r
Move or rename mv mv (no -r)
Delete rm rmdir (empty only), rm -r (with everything inside)

Add -i to cp, mv or rm to be asked before anything is overwritten or deleted.

18. Hidden files: a dot at the start of the name

Your home already has files you can't see with ls. Linux keeps its settings in them.

A name that starts with a dot is hidden from a normal ls. That's the whole rule.

How (from your home folder):

  1. touch notes.txt
  2. Hide it: mv notes.txt .notes.txt
  3. ls → it's not listed.
  4. ls -a → there it is, next to files that were hidden all along.

Your list will look something like this:

ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .notes.txt  .ssh  projects  raj  ravi

To unhide it, rename it back: mv .notes.txt notes.txt. Folders work the same way: mv ravi .ravi hides, mv .ravi ravi unhides.

ls -A is the same without . and ...

Hidden files: a name that starts with adot $ mv notes.txt .notes.txt$ lsprojects ravi report.txt$ ls -a. .. .bash_logout .bash_profile .bashrc.notes.txt .ssh projects ravi report.txt Hide and unhide = renamemv notes.txt .notes.txt (hide)mv .notes.txt notes.txt (unhide)mv ravi .ravi (folders too) Hidden is not secretanyone who types ls -a sees it.To protect a file, use permissions.The dot only keeps ls output tidy. Linux hides its own settings this way: .bashrc, .bash_profile,.ssh. ravi/* skips dot-names too.

Figure 7. mv notes.txt .notes.txt hides the file from ls; ls -a shows it next to .bashrc, .bash_profile and .ssh. Renaming back unhides it. Hidden is not secret: anyone can run ls -a.

Real examples of hidden files:

  1. .bashrc, .bash_profile: your shell's settings.
  2. .ssh/: holds authorized_keys, the list of public keys allowed to log in as you.
  3. Remember section 2: ravi/* skips these. ravi/. doesn't.

A dot only hides a name from a plain ls; anyone who types ls -a sees it. It is not security. To protect a file, use permissions (next topic).

19. The entries . and ..

They explain why cd .. and cp file . work at all, and why ls -a always shows two names you never created.

ls -a shows two names in every folder, even a brand-new empty one:

  1. . = this folder itself.
  2. .. = the parent folder, one level up.

They are real entries in the folder's list, pointing to the folder and to its parent. That's why they work everywhere:

  1. cd .. → up one level.
  2. cd . → stay where you are.
  3. cp one/a.txt . → copy here.
  4. mv ravi/2.txt .. → move one level up.
Every folder has two built-in entries: . and .. / └─ home └─ ec2-user ← .. └─ ravi ← . ● you are in ravi └─ one . = ravi .. = ec2-user $ cd ravi $ ls -a . .. 1.txt one even a brand-new empty folder shows . and .. $ cd . stays in ravi $ cp one/a.txt . copy here $ cd .. up to ec2-user . points to the folder itself, .. to its parent. Like a chain: each folderknows where it is and where its parent is.

Figure 8. You are in ravi: . points to ravi itself, .. points to its parent ec2-user. ls -a shows . and .. in every folder, even an empty one.

Think of a chain: every folder knows where it is (.) and where its parent is (..), so you can always walk back up to /.

20. Before you leave: terminate or stop

A running instance keeps using free-tier hours, and a terminated one can't be brought back. Choosing the wrong one costs either money or your work.

Before you leave, decide: stop or terminate.

  1. Stop = switch off. The disk (EBS) stays; you can start it again later.
  2. Terminate = delete the server. By default the root EBS volume is deleted too. It can't be undone.
  3. You don't need to stop before terminating. Terminate shuts it down by itself.

Stop vs terminate, and what each costs, is in What Is Amazon EC2 and How Does It Work.

You don't need to stop an instance before terminating it: terminate shuts the instance down by itself and deletes the root volume by default. Terminate only a server you no longer need; for practice servers you'll use again, stop is enough.

21. Hands-on lab: move, rename and delete safely

mv and rm change things for good: nothing is copied and nothing goes to a Recycle Bin. Practise them on throwaway files first, with the safe options, so you never learn them on real data.

  1. Set up throwaway files
    • Command: cd && mkdir -p lab/ravi && cd lab && touch a.txt b.txt 1.log 2.log
    • You should see: Nothing.
    • Check: ls → 1.log 2.log a.txt b.txt ravi
    • Why: A separate lab folder keeps intended targets away from real files. Confirm the current path and selected names before removing anything.
  2. Move a file into a folder
    • Command: mv a.txt ravi
    • You should see: Nothing.
    • Check: ls no longer shows a.txt; ls ravi → a.txt.
    • Why: mv is cut-paste: unlike cp, the original is gone from the old place.
  3. Rename a file
    • Command: mv b.txt notes.txt
    • You should see: Nothing.
    • Check: ls shows notes.txt, not b.txt.
    • Why: notes.txt isn't an existing folder, so mv treats it as the new name. Renaming is just moving within the same folder.
  4. Rename inside a subfolder without going there
    • Command: mv ravi/a.txt ravi/first.txt
    • You should see: Nothing.
    • Check: ls ravi → first.txt
    • Why: Both names are paths, so you stay where you are. Fewer cd commands, fewer chances to run something in the wrong folder.
  5. Move many files with a wildcard
    • Command: mv *.log ravi
    • You should see: Nothing.
    • Check: ls ravi → 1.log 2.log first.txt
    • Why: With several sources, the last name must be a folder. *.log describes the pattern instead of listing every log.
  6. Get warned before an overwrite
    • Command: touch old.txt && mv -i notes.txt old.txt
    • You should see: mv: overwrite 'old.txt'?
    • Check: Type n, Enter. ls still shows both notes.txt and old.txt.
    • Why: Plain mv would have replaced old.txt silently. -i makes it ask.
  7. Move a whole folder, no -r
    • Command: mkdir box && mv ravi box
    • You should see: Nothing.
    • Check: ls box/ravi → 1.log 2.log first.txt
    • Why: Moving only changes the path; the data doesn't move on the disk. That is why mv needs no -r and is instant even for huge folders.
  8. Delete a file, with a question first
    • Command: rm -i old.txt
    • You should see: rm: remove regular empty file 'old.txt'?
    • Check: Type y, Enter. ls no longer shows old.txt.
    • Why: rm has no undo. -i gives you one last look at the name before it is gone.
  9. Try rmdir on a full folder
    • Command: rmdir box
    • You should see: rmdir: failed to remove 'box': Directory not empty
    • Check: ls still shows box.
    • Why: rmdir deletes only empty folders. That refusal is the safety net: it can never take files with it.
  10. Delete the folder and everything in it
    • Command: rm -ri box
    • You should see: rm: descend into directory 'box'? and one question per item.
    • Check: Answer y to each. ls no longer shows box.
    • Why: -r goes all the way down. Adding -i while you learn shows exactly what rm -r is about to delete.
  11. Tell file from folder with ls -l
    • Command: touch report && mkdir photos.txt && ls -l
    • You should see: photos.txt line starts with d; report line starts with -.
    • Check: So photos.txt is a folder and report is a file, whatever the names suggest.
    • Why: Before rm or rmdir, know what you have. The first character of ls -l decides; names and extensions don't.
  12. Hide and show a file
    • Command: mv notes.txt .notes.txt && ls && ls -a
    • You should see: ls → photos.txt report. ls -a → . .. .notes.txt photos.txt report.
    • Check: .notes.txt appears only with -a, along with . and ...
    • Why: A leading dot hides a name from plain ls; that's how Linux keeps settings files out of the way. It isn't security: anyone with ls -a sees it.
  13. Clean up and stop
    • Command: cd && rm -r lab
    • You should see: Nothing.
    • Check: ls no longer shows lab. In the EC2 console, choose Stop instance (not Terminate).
    • Why: Stop keeps the disk for the next chapter; Terminate deletes the server and, by default, its root volume.
  14. Copy several sources into one folder
    • Command: cd && mkdir -p lab2 && cd lab2 && touch my.txt bot.txt && mkdir ravi && cp my.txt bot.txt ravi && ls ravi
    • You should see: bot.txt my.txt inside ravi. Both originals stay in lab2.
    • Check: cp accepts many sources and one destination folder as the last argument.
    • Why: Class showed multi-source copy so you can gather files in one place without repeating cp.
  15. A folder name can contain a dot
    • Command: mkdir notes.txt && touch real.txt && ls -l
    • You should see: a line starting with d for notes.txt (directory) and - for real.txt (file).
    • Check: Extension does not decide type — mkdir makes a folder even if the name ends in .txt.
    • Why: Students trust colours and extensions; ls -l first character is the truth.
  16. Hide, prove with ls -a, then unhide
    • Command: touch secret.txt && mv secret.txt .secret.txt && ls && ls -a && mv .secret.txt secret.txt && ls
    • You should see: plain ls hides .secret.txt; ls -a shows .secret.txt plus . and ..; after the second mv, plain ls shows secret.txt again.
    • Check: Leading dot only hides from default ls — it is not security.
    • Why: Real configs (.bashrc, .ssh) use this pattern; permissions protect secrets.

22. Try at home

Try at home

Task 1: contents only

  1. Make src with a.txt, b.txt and a hidden .env. Make an empty dst.
  2. Copy only the contents of src into dst with src/*. Check with ls -A dst. Is .env there?
  3. Empty dst (rm -r dst/*) and do it again with src/..

Task 2: mv

  1. touch my.txt, then rename it to our.txt.
  2. Move our.txt into src and rename it to final.txt in one command.
  3. Rename src/final.txt to src/done.txt without leaving home.
  4. mkdir box and move the whole src folder into box. Did you need -r?

Task 3: delete

  1. Try rmdir box. Read the error.
  2. Try rm box. Read the error.
  3. Delete it with rm -ri box; answer y to everything.

Task 4: look closely

  1. touch report and mkdir photos.txt. Run ls -l and ls -F. Which is the folder?
  2. Hide report with a dot, find it with ls -a, then unhide it.
  3. Redo the practice task (section 13) with your own folder names, from home, in as few commands as you can.

When you finish, stop the instance from the console, or terminate it if you won't use it again.

Common errors and fixes

Error / symptomFix
rmdir: Directory not emptyEmpty it first, or use rm -r if you really want everything gone.
mv: … are the same fileSource and destination paths resolve to the same name — pick a new name.
Silent overwrite with mvUse mv -i so it asks first.
Deleted the wrong folderLinux has no Recycle Bin by default — restore from backup. Next time rm -i and double-check pwd.
Thought .secret was privateHide ≠ secure. Use permissions (chmod) and never put secrets in world-readable files.

FAQ

Q. Why no -r on mv for folders?

On the same disk, mv mostly changes the directory entry (the path). It does not rewrite every file like cp -r.

Q. mv vs cp?

cp keeps the original. mv does not (after a successful move).

Q. Is rm -r on one file dangerous?

On a single file it usually works, but habitually typing -r is risky — one wrong name deletes a whole tree.

Q. Stop vs Terminate after practice?

Stop keeps the disk (you pay for EBS). Terminate usually deletes the root volume — use only when you are done forever.

Recap

In short

  1. The prompt shows your current folder: [ec2-user@ip-... ravi]$. ~ means home.
  2. Contents only: cp -r ravi/* raj. With hidden files: cp -r ravi/. raj.
  3. mv SOURCE DESTINATION = cut-paste. Existing folder → moves inside; new name → rename.
  4. mv my.txt ravi/you.txt moves and renames in one step; mv ravi/1.txt ravi/2.txt renames without leaving home.
  5. mv needs no -r: on the same disk it only changes the name entry; across disks it copies, then deletes.
  6. mv overwrites silently; mv -i asks. Wildcards work: mv *.txt ravi.
  7. rm files, rmdir empty folders, rm -r folders with everything. No recycle bin.
  8. rm -r on a file is a safety risk, not a speed problem. Use rm -i / rm -ri when unsure.
  9. Work from home with relative paths, braces and Tab.
  10. Names and colours don't tell file from folder; ls -l (first character - or d) and ls -F do.
  11. Hidden = name starts with a dot. ls -a shows them. Hidden is not secret.
  12. Every folder has . (itself) and .. (its parent).
  13. Terminate needs no stop first, and it deletes the root volume by default.

Samjla ka? Ghari practice task parat kara, ani rm vaparnyaadhi don vela vichar kara.


Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. The folder and file names (ravi, raj, projects, notes.txt, report.txt and the rest) are examples for learning. Command outputs and error messages were checked with GNU bash and coreutils, the same tools Amazon Linux 2023 uses; the hostname in the prompt and the dates in ls -l are illustrative.

Worked example: rename a file before you remove it

In a shop, a box goes to the returns shelf before anyone throws it away. Use the same two-stage method for a file you are unsure about. Try this only on the two disposable files below.

mkdir -p "$HOME/move-practice/hold"
cd "$HOME/move-practice"
printf '%s
' draft > report-draft.txt
printf '%s
' final > report-final.txt
mv -i -- report-draft.txt hold/
ls -la hold
cat report-final.txt

The live file is still readable and the draft is recoverable with mv -i -- hold/report-draft.txt .. Once you know the draft is unwanted, use rm -i -- hold/report-draft.txt and confirm the exact file. The -- tells the tool that later arguments are names, helpful if a name starts with a dash.

Interview question: is mv always an instantaneous rename?

Within the same file system it normally renames the directory entry. Across file systems, it can copy the data and then remove the source. For a large move, know where both paths live and verify the destination before assuming the source is safely gone.

Reference 1 · Reference 2

Practise this chapter

Interview questions and answers

You want to remove an empty temporary folder. Why start with rmdir?

rmdir refuses to remove a nonempty directory. If it reports that the folder is not empty, list hidden names with ls -A and understand what is inside. Use rm -i on a known throwaway file before repeating rmdir.

Why does a move between two filesystems need different checks than a rename?

The destination filesystem needs space for a copy before the original can be removed. For important data, copy it separately and verify the destination before deleting the source so you can investigate a failure without losing both names.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. You move a large file from a root filesystem to an attached data filesystem using mv. Why can this take longer than renaming it in the same directory?

Answer and explanation

B. The move may have to copy the bytes before removing the original

A rename within one filesystem can update directory entries. Moving to a different filesystem needs the file’s data to arrive on the other filesystem.

2. rmdir test reports Directory not empty, but ls test prints nothing. Which command can reveal the likely cause?

Answer and explanation

C. ls -A test

Plain ls skips names beginning with a dot. ls -A shows hidden items without showing the special . and .. entries.

3. You plan to remove old test logs by running rm *.logs. What is the useful first check in the practice folder?

Answer and explanation

A. Confirm pwd and print the names selected by the same unquoted pattern

The shell chooses matching names before running rm. Checking the current directory and previewing the same pattern lets you catch an unintended match before deleting anything.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Thane Housing Society is a real estate business in Thane. Mahesh Patwardhan looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy Rename a file

In Linux, renaming is the same as moving a file to a new name.

Rename the file report.txt to report-thane.txt.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Read a config file

Reading a file on the screen is the quickest way to check its settings.

Print the whole file /etc/thanehousing/app.conf on the screen.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy List all files with details

Hidden files (like .env) and file sizes or owners only show with the right options.

Show a long listing of every file, including hidden ones, in /var/www/thanehousing.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Create an empty file

Some apps need a file to exist before they can write to it.

Create an empty file named orders-inr.csv in the current folder.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Move a file into a folder

Keeping files in the right folder keeps the server tidy.

Move backup.sql into the folder /backup/thanehousing/.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Remove an empty folder

rmdir is the safe way: it only removes a folder when it is empty.

Remove the empty folder tmp-thanehousing.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Copy a folder with everything inside

A plain copy skips folders. You need the recursive option to copy a whole tree.

Copy the folder site to a new folder named site-backup (both in the current folder).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy List Availability Zones

Spreading servers across AZs keeps you up if one data centre fails.

List the Availability Zones in ap-south-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List your EC2 instances

Before changing anything, see what is running and in which state.

Show details of all EC2 instances of Thane Housing Society in ap-south-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Delete an old folder

Old build folders waste disk. rm needs the recursive option for folders, and force skips the questions.

Delete the folder old-build and everything inside it without being asked to confirm each file.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Permission number

Key files must be private.

What chmod number gives the owner read+write and nobody else anything?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Enable an nginx site on Ubuntu

On Ubuntu, a site is enabled by a symbolic link from sites-available to sites-enabled.

Create a symbolic link to /etc/nginx/sites-available/thanehousing inside /etc/nginx/sites-enabled/.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: mv on the same disk vs mv across disks. mv on the same disk: Moving a file inside one filesystem. mv across disks: Moving a file to another filesystem.
Comparison: mv on the same disk vs mv across disks · Download PNG
Revision card: mv: move and rename. Files end up in the wrong place or with the wrong name. mv is cut-paste. If the destination is a new name in the same folder, it renames. Folders need no -r with mv. Use -i to avoid silent overwrites. Rename and move:
Revision card: mv: move and rename · Download PNG
Revision card: Delete with rm, rm -r, rmdir. Linux has no recycle bin. A wrong rm is permanent. rm deletes files. rm -r deletes folders with content. rmdir deletes only empty folders. Use rm -i to confirm each file; run pwd first. Safe delete:
Revision card: Delete with rm, rm -r, rmdir · Download PNG
Revision card: Hidden files and ls -a. Important settings like .bashrc and .ssh do not show with ls. A name starting with a dot is hidden. . is this folder, .. is the parent. ls -a shows everything; ls -la shows details too. In home:
Revision card: Hidden files and ls -a · Download PNG
Revision card: File or folder? Read ls -l. Names and colours can lie: a folder can be named photo.jpg. The first character of ls -l tells the type: - file, d directory, l link. Check before cp -r or rm -r. Output:
Revision card: File or folder? Read ls -l · Download PNG
Interview card: Why does mv not need -r for folders when cp does?. On the same disk, mv only changes the name entry; nothing is copied. cp must copy every file inside, so it must be told to recurse. mv site /tmp/  vs  cp -r site /tmp/.
Interview card: Why does mv not need -r for folders when cp does? · Download PNG
Interview card: How do you copy only what is inside a folder, not the folder?. Use folder/* (or folder/. with -r) as the source. folder alone copies the folder itself. Command:
Interview card: How do you copy only what is inside a folder, not the folder? · Download PNG
Interview card: rm -r vs rmdir: when do you use each?. rmdir deletes only empty folders; rm -r deletes folders with everything inside. rmdir is a safe check: it fails if anything is left. rmdir old › "Directory not empty".
Interview card: rm -r vs rmdir: when do you use each? · Download PNG

More cards: Ravi Sir's Cards.