-   Linux - Software (
-   -   copying multiple different files in different directories with identical file names (

nickleus 03-02-2006 06:19 PM

copying multiple different files in different directories with identical file names
Say that i have the following file structure:


and i want to copy all jpg files to /folder3. How do i do that when they have the same names, but they actually are different files/pictures? The file names can maybe look something like this, it doesn't matter:


where an incremental number (starting at "1", for example) and then a "-" is added at the beginning of the file name with each copy execution. I'm sure it is a combination of cp and some bash programming, but i'm not a bash wizard.

Or here's my attempt at using date and random:

mv /folder*/*.jpg /folder3/$(date +%N)$RANDOM.jpg
but i get an error that says:

mv: when moving multiple files, last argument must be a directory

wipe 03-02-2006 09:20 PM

I've used "dest-folder" here as the name of the destination directory. Be careful with these command lists, as they move files and might overwrite something without prompting.

This names the destination files dest-folder/<former directory name>-<filename>.

for i in folder*/*; do mv "$i" "dest-folder/$(dirname $i)-$(basename $i)"; done
These commands put an incremental number always in front of the file name:

for i in folder*; do let j++; for k in $i/*; do mv $k dest-folder/$j-$(basename $k); done; done

This uses a little trickery to put the icremental number only in the filenames that already exist:

mv -f --backup=t folder*/* dest-folder
for i in dest-folder/*~; do mv $i $(echo $i|sed -re 's/\/(.*).~(.*)~/\/\2-\1/'); done

The command sequences would be easier to understand if broken down into lines of code. The semicolon marks a place where you can press Return; the prompt changes until you give the last done command. The dos are usually also followed by a newline. If you want to write the commands into an executable file, write #!/bin/bash as the first line, then every command on a separate line. When the file is saved use chmod +x filename to change it into an executable. Then run it like this: ./filename.


phidor 03-03-2006 01:15 AM

Copying files
Ignoring the academic challenge of the CLI, why not use the GUI "Find Files" and then select the files you want and drop them into the folder you want? I realise I am working from a simplified scenario of /home data files but this apears to be what your are doing Nick?

- Phidor

nickleus 03-09-2006 12:33 PM

Hey wipe, thanks for the cool examples. I appreciate it =)

scottmorris 03-09-2006 02:25 PM

Another option
Something else that you might take a look at is Krename. That will allow you to move, copy, rename, etc. batches of files in different directories and all that sort of thing. It is a nice gui-based KDE app, if that sounds like something you're interested in. Anyway, if the CLI thing is more hassle than you're in the mood for, Krename may help do what you're looking for.

All times are GMT -5. The time now is 12:55 PM.