Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash move * to subfolder fail: cannot move to a subdirectory of itself

Tags:

bash

I have a folder with several files that I want to move to a subfolder:

mkdir subfolder
mv ./* subfolder

But when I do this, I get:

mv: cannot move 'subfolder' to a subdirectory of itself

How can I simply avoid this?

like image 945
Augustin Riedinger Avatar asked Apr 06 '17 15:04

Augustin Riedinger


4 Answers

Using extglob you can do this:

shopt -s extglob

mv !(subfolder) subfolder

Glob expression !(subfolder) will match everything except subfolder.

like image 101
anubhava Avatar answered Nov 15 '22 05:11

anubhava


Was this on Windows Subsystem for Linux (WSL)?

If so, Windows recognizes the directory Jerry as equal to directory jerry, and WSL follows certain Windows rules for compatibility.

If you want to rename the directory Jerry to jerry, you could do the following:

mv Jerry placeholder_name
mv placeholder_name jerry
like image 25
Keith Lyons Avatar answered Nov 15 '22 07:11

Keith Lyons


There is an easy solution

mv * subfolder | mkdir subfolder

It will move all the files from the folder to the subfolder without any errors.

like image 31
Rauf Avatar answered Nov 15 '22 07:11

Rauf


Umm I had a similar problem. Took me a couple of tries! But I ended up with

mkdir ../subfolder
mv * ../subfolder
mv ../subfolder .
like image 24
HeyWatchThis Avatar answered Nov 15 '22 05:11

HeyWatchThis