Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 03-02-2006, 06:19 PM   #1
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Rep: Reputation: 15
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

Last edited by nickleus; 03-02-2006 at 06:56 PM.
Old 03-02-2006, 09:20 PM   #2
Registered: Jun 2004
Location: High Green
Distribution: Fedora Core 4
Posts: 180

Rep: Reputation: 30
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.

Old 03-03-2006, 01:15 AM   #3
Registered: Apr 2005
Location: Timaru, New Zealand
Distribution: Manjaro
Posts: 105

Rep: Reputation: 15
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
Old 03-09-2006, 12:33 PM   #4
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Original Poster
Rep: Reputation: 15
Hey wipe, thanks for the cool examples. I appreciate it =)
Old 03-09-2006, 02:25 PM   #5
Novell CoolSolutions Editor
Registered: Jan 2006
Distribution: SUSE 9.3 - 10.0
Posts: 30

Rep: Reputation: 16
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 09:29 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 05:46 AM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 04:47 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 06:25 PM > Forums > Linux Forums > Linux - Software

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration