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:
- The file
variables.shdisappears from your current folder. - It now lives inside the
scriptsfolder. - 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.shexists andscriptsis a folder: Move inside. - If
variables.shexists andnew_name.shdoes 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!