What happens if destination is a directory?

QuestionsQuestions8 SkillsProDec, 21 2025
095

Great question! That's a very important distinction in Linux.

If the destination is an existing directory, the mv command changes its behavior: instead of renaming the file, it moves the file into that directory while keeping its original name.

Example:

Imagine you have a file named variables.sh and a folder named scripts.

If you run:

mv variables.sh scripts

The result is:

  1. The file variables.sh disappears from your current folder.
  2. It now lives inside the scripts folder.
  3. Its full path becomes scripts/variables.sh.

Summary of mv:

Command If destination is... Result
mv file1 file2 A new name The file is renamed.
mv file1 folder/ A directory The file is moved into that folder.

How can you tell the difference?
The computer checks if the name you typed already exists as a folder.

  • If variables.sh exists and scripts is a folder: Move inside.
  • If variables.sh exists and new_name.sh does NOT exist: Rename.

In your current lab, since you are inside /home/labex/project, typing mv variable.sh variables.sh will rename it because variables.sh is just a filename, not a folder!

0 Comments

no data
Be the first to share your comment!