I am at the path:
/myuser/downloads/
And I create a sub folder:
/myuser/downloads/new
Now I want to move all files and folders/sub-folders in the downloads folder to the sub-folder.
how can I do this?
I tried:
mv -R *.* new/
But move doesn't take the -R switch it seems.
The command
should do the trick. If it doesn't work, run
shopt -s extglob
first.To also move hidden files/directories (that beginning with a dot), run also
shopt -s dotglob
first.So, to sum up:
(it is always better to unset
dotglob
to avoid bad surprises).I found something like this but is a bit simpler to understand, and it might work well for you too:
Adding an explanation to the above solution:
From man pages:
mv -t
grep -v
Explained by step:
ls
will list the files in current directorygrep -v new
will return piped to that is not match newxargs mv -t new
will move the files piped to it fromgrep -v
to the target directoryJust use
mv * subdir1
and ignore the warning.You can just use
mv * subdir1
. You will see a warning message relating to trying to movesubdir1
into itself, like this:But it will move all of the other files and directories to
subdir1
correctly.An example:
Simple idea. Assuming you are in /myuser, rename downloads to new, create a new downloads directory then move new into it.
If you want to move all the files from a folder to one of its subfolders you can use the following command:
It will find all the files and then move them to your subfolder.
@waltinator: added
-type d -name 'new' -prune
to prevent traversal of/myuser/downloads/new
.You can try this alternative process –– remain in the path
but, instead of first creating the
/myuser/downloads/new/
directory, instead create a folder in the/myuser/
directory, with the commandmkdir ../new
, then move all the files indownloads
tonew
, and finally movenew
intodownloads
. You can do this in one line, while in the/myuser/downloads/
path, with the command:In this case, you don't have to worry about any sort of "filtering" of files/folders, since
new
is on the same level of the path asdownloads
, so you can just move everything indownloads
tonew
, and then movenew
into downloads`.However, if you already have the subfolder
new
created and don't want to create another one, not to worry –– just change themkdir
command on the left-hand side of the first&&
in the command shown above to anmv
command, pushingnew
up in the path; in other words, while you're still in/myuser/downloads/
, you can changemkdir ../new
tomv new ..
. Then the subfoldernew
[in the path/myuser/downloads/new/
] gets pushed up to/myuser/new/
, at the same level as/myuser/downloads/
, and then you can run the rest of the command as it is shown above. All together, we have, starting from the path/myuser/downloads/
:and, since you wanted to "move all files and folders/sub-folders in the downloads folder to the sub-folder [
new
]", you're done! If you had wanted to move only files (or only folders or [insert more granular object movement]), then you'd have to use other commands that can "filter" objects, such asgrep
. The commands written above are sufficient for your purposes, though.