Linux move all files in subdirectories

How to move all files in current folder to subfolder?

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:

You do not need -R. Just mv * new Though, it will whine «cannot move new to new» or something similar. Perhaps mv -i * new to you prevent overwrites.

6 Answers 6

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:

shopt -s extglob dotglob mv !(new) new shopt -u dotglob 

(it is always better to unset dotglob to avoid bad surprises).

@adeer that is just saying not(something). In other words, copy everything but not something. Clear as mud?

I don’t get it, e.g. what If I try to move the content of the folder dev to my current location, do I have to execute mv !(dev) dev ?? I get -bash: !: event not found

@adeer very similar. setopt extendedglob to set the extended glob mode in zsh. ^ is the exclusion symbol, so mv ^new new would do the trick. then to see all the options set in zsh you can use setopt and to disable extended glob mode you can do unsetopt extendedglob

I found something like this but is a bit simpler to understand, and it might work well for you too:

ls | grep -v new | xargs mv -t new 

Adding an explanation to the above solution:

-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY 
-v, --invert-match Invert the sense of matching, to select non-matching lines. 
  • ls will list the files in current directory
  • grep -v new will return piped to that is not match new
  • xargs mv -t new will move the files piped to it from grep -v to the target directory

@ConstantFun It could be zsh but I would guess it’s your implementation of mv , (meaning your version of mv is different them what I was using) what os are you using? I believe when I wrote this answer I was on linux distro centos using bash .

Just testing this on osx and I get the same error mv: illegal option — t which is because osx’s mv does not have a -t option.

(I have not tried this, and don’t plan on it.) This link apple.stackexchange.com/questions/69223/… tells you how to install the gnucore which should help you get gnu core cli utils, osx ships with bsd cli tools which are different the the ones that come with linux.

@TheFool that is because Alpine linux uses busybox and the busybox implementation of mv does not have -t as an option. github.com/brgl/busybox/blob/master/coreutils/mv.c

Just use mv * subdir1 and ignore the warning.

You can just use mv * subdir1 . You will see a warning message relating to trying to move subdir1 into itself, like this:

mv: cannot move 'subdir1' to a subdirectory of itself, 'subdir1/subdir1' 

But it will move all of the other files and directories to subdir1 correctly.

$ ls $ mkdir dir1 dir2 dir3 $ touch file1.txt file2.txt file3.txt $ mkdir subdir1 $ ls #=> dir1 dir2 dir3 file1.txt file2.txt file3.txt subdir1 $ mv * subdir1 #=> mv: cannot move 'subdir1' to a subdirectory of itself, 'subdir1/subdir1' $ ls #=> subdir1 $ ls subdir1 #=> dir1 dir2 dir3 file1.txt file2.txt file3.txt 

If you really don’t like the warning (maybe this is in a bigger script where the warning would be confusing), mv * subdir1 2>/dev/null . This is not always desirable, though, since there might be other errors reported by mv (e.g. if you don’t have permissions).

Читайте также:  Kali linux пароль при входе

@NotSoShabby Interesting! Thanks for posting. I didn’t even think of that. Any idea how those can be included as well?

Simple idea. Assuming you are in /myuser, rename downloads to new, create a new downloads directory then move new into it.

mv downloads new # downloads is now called new mkdir downloads # create new directory downloads mv new downloads # 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:

find /myuser/downloads/ -type d -name 'new' -prune -type f | xargs mv -t /myuser/downloads/new 

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 .

I created a subdirectory named new and tried this command, I get mv: missing file operand and Try ‘mv —help’ for more information

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 command mkdir ../new , then move all the files in downloads to new , and finally move new into downloads . You can do this in one line, while in the /myuser/downloads/ path, with the command:

mkdir ../new && mv * ../new && mv ../new ../downloads 

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 as downloads , so you can just move everything in downloads to new , and then move new into downloads`.

However, if you already have the subfolder new created and don’t want to create another one, not to worry –– just change the mkdir command on the left-hand side of the first && in the command shown above to an mv command, pushing new up in the path; in other words, while you’re still in /myuser/downloads/ , you can change mkdir ../new to mv new .. . Then the subfolder new [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/ :

mv new .. && mv * ../new && mv ../new ../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 as grep . The commands written above are sufficient for your purposes, though.

Источник

How do I move files out of nested subdirectories into another folder in ubuntu? (Trying to strip off many subfolders)

How do I move files and not directories into another folder/parent folder? I have a folder structure that is extremely ugly, with some .mp3 files buried 6 levels deep in a sub-folder. I want to end up with all of the files (mostly .mp3 but not all) in one directory, with no subdirectories at all, using Ubuntu. Help?

Читайте также:  Linux какая папка занимает больше всего места

5 Answers 5

To do so, Open a terminal and execute this command:

It will move all the files and folders from Downloads folder to Videos folder.

To Move all files, but not folders:

But, If you are interested to move all files (but not folders) from Downloads folder to Videos folder, use this command

find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos 

To move only files from the Download folders, but not from sub-folders:

If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:

find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos 

here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2 , 3 also to test.

See the Ubuntu find manpage for a detailed explanation.

Источник

How to move all files including hidden files into parent directory via *

Its must be a popular question but I could not find an answer. How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

this question has a duplicate at SU, with an even more correct answer (not the accepted one though): cp -r /path/to/source/. /destination

9 Answers 9

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

You can use these two commands together:

mv /path/subfolder/* /path/ # your current approach mv /path/subfolder/.* /path/ # this one for hidden files 
mv /path/subfolder/* /path/subfolder/.* /path/ 

(example: echo ab expands to a.b ab )

Note this will show a couple of warnings:

mv: cannot move ‘/path/subfolder/.’ to /path/.’: Device or resource busy mv: cannot remove /path/subfolder/..’: Is a directory 

Just ignore them: this happens because /path/subfolder/* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

cp -r /path/subfolder/. /path/ # ^ # note the dot! 

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

The braces are just a short cut for mv /path/subfolder/* /path/subfolder/.* /path/ , not strictly necessary to combine the two commands into one.

I get the following error: mv: overwrite `/path/.’? y mv: cannot move `/path/subfolder/.’ to `/path/.’: Device or resource busy mv: overwrite `/path/..’? y mv: cannot move `/path/subfolder/..’ to `/path/..’: Device or resource busy

@Dejan Just ignore it. . denotes current directory and .. denotes up directory. You must have noticed that all other files are moved.

«Just ignore the warning» may not always be a good idea. Right now I’m having a problem with a script in which I need to stop execution if any step fails — since this solution always causes an error, it kills my script. I need a way to determine if the mv command failed or not.

Читайте также:  Run command in linux as another user

* works fine as a pattern to match hidden and unhidden files excluding . and .. but always returns itself as well even if there are some matching files. This seems to be a bug and can be worked around by setting nullglob ( shopt -s nullglob ). But if one does that one could set dotglob instead which seems favorable to me. Either should probably only be enabled temporarily.

I think this is the most elegant, as it also does not try to move .. :

mv /source/path/* /destination/path 

this would miss files like ..anything or . anything etc. — stackoverflow.com/a/31438355/2351568 contains the correct regex for this problem. || but anyway using shopt -s dotglob is still the better solution!

@DylanB don’t memorize it. remember that it matches whatever is in the curlybrackets, separated by commas. * would find all files starting with a or b such as «anatomy» and «bulldozer». The second match is just an empty match, equivalent to * , and the first match is equivalent to .[!.] , where the group [!.] means a group NOT starting with a . . This means .* but not ..* .

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

shopt -s dotglob # This would cause mv below to match hidden files mv /path/subfolder/* /path/ 

In order to turn off dotglob , you’d need to say:

Very helpful. Wanted to find out more but shopt is a builtin so man shopt doesn’t work and help shopt is very brief. But you can do bashman () < man bash | less -p "^ $1 "; >and then bashman shopt to read all about it straightforwardly. (Might have to hit n to jump down to the command if there are lines starting with shopt, as I found.)

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n <> /path \; 

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/ 2) mv /path/subfolder/* /path/ 3) mv /source/path/* /destination/path 

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

sudo rsync -vuar --delete-after --dry-run path/subfolder/ path/ 

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

Оцените статью
Adblock
detector