LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash how mv */*.* /some/other/directory (https://www.linuxquestions.org/questions/programming-9/bash-how-mv-%2A-%2A-%2A-some-other-directory-895407/)

porphyry5 08-03-2011 08:01 PM

bash how mv */*.* /some/other/directory
 
I have a directory containing about 150 sub-directories, each of which contains only files of various types. I need to get all those files together in a single directory without any of the subdirectories. Can it be done with any simple approach like "mv */*.*", or do I need to get a list of all the individual filepaths and feed that to mv?

AlucardZero 08-03-2011 08:29 PM

find /src/dir -exec mv '{}' /some/other/dir +;

crts 08-03-2011 08:36 PM

Hi,

try this:
Code:

find /path/to/dir -type f -exec mv -n '{}' /path/to/target \;
The -n option prevents mv to overwrite existing files in the target directory. If you still have files in one of the source directories then you will need a slightly more complicated approach and a naming convention for the files that have the same name.

porphyry5 08-03-2011 10:51 PM

I thank both AlucardZero and crts for your help, but it seems that \; is the charm
Code:

g  find /home/g/x -exec mv '{}' /home/g/y +;
find: missing argument to `-exec'
g  find /home/g/x -type f -exec mv '{}' /home/g/y +;
find: missing argument to `-exec'
g  find /home/g/x -type f -exec mv '{}' /home/g/y \;
g

Many thanks.

MTK358 08-04-2011 06:32 AM

Quote:

Originally Posted by porphyry5 (Post 4433103)
*.*

That won't work for getting only files and not directories.

Linux has no concept of file extensions, both files and directory names can freely contain dots (or not have any at all).

AlucardZero 08-04-2011 08:55 AM

oop. crts is right in that you want "-type f". And my syntax was slightly wrong, it should be \;.

crts 08-04-2011 09:51 AM

Quote:

Originally Posted by AlucardZero (Post 4433607)
And my syntax was slightly wrong, it should be \;.

Hi AlucardZero,

your syntax is not wrong; at least the 'find' version (GNU 4.4.2) that I am using accepts it. Although the terminating ';' is not needed when you use '-exec' with a '+', it should not lead to an error if you use it anyway. At least not in bash. Bash will simply interpret it as a line terminator. So possible sources for the error messages can be a 'find' version that does not support '-exec ... +' or maybe executing 'find' in a non-bash terminal.


All times are GMT -5. The time now is 06:36 AM.