Skip to content
Advertisement

moving and renaming a folder in linux [closed]

I want to ask a question about moving and renaming folders in linux simultaneously.

Consider I am on my desktop and want to move a folder to another location.

folder1 exists at ~/folder1

If I want to move it to the desktop, I do the following command:

mv ~/folder1 ~/Desktop

However, if I want to move and rename the folder, for some reason this is the command:

mv ~/folder1 ~/Desktop/folder2

I’m confused here slightly.

If folder2 does not exist, folder1 is renamed as folder2, keeping all the contents.

However, if folder2 does exist, it simply slots folder1 into folder2 i.e.

~/Desktop/folder2/folder1

I thought that if folder2 does not exist, it simply creates folder2 and inserts folder1 into it as per the moving of a file with mv.

Why does the folder simply get renamed from folder2 to folder1?

Advertisement

Answer

You explained the mv workflow quite clearly here.

the command ‘mv’ first checks the destination and checks if it is available. Destination if not available – This source file is moved and renames the file. Destination if available and is a folder – This source file is moved into the destination folder Destination if available and is a file – This source file is moved into the destination folder and gets overwritten

Ref:https://en.wikipedia.org/wiki/Mv

Advertisement