LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-02-2006, 05:19 PM   #1
nickleus
Member
 
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:
/folder1/1.jpg
/folder1/2.jpg

/folder2/1.jpg
/folder2/2.jpg


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:

/folder3/1-1.jpg
/folder3/2-2.jpg
/folder3/3-1.jpg
/folder3/4-2.jpg


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:
Code:
mv /folder*/*.jpg /folder3/$(date +%N)$RANDOM.jpg
but i get an error that says:
Quote:
mv: when moving multiple files, last argument must be a directory

Last edited by nickleus; 03-02-2006 at 05:56 PM.
 
Old 03-02-2006, 08:20 PM   #2
wipe
Member
 
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>.
Code:
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:
Code:
j=0
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:
Code:
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.

Simon
 
Old 03-03-2006, 12:15 AM   #3
phidor
Member
 
Registered: Apr 2005
Location: Timaru, New Zealand
Distribution: Garuda - wrestling with xmonad at chewing-gum & barb-wire level
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, 11:33 AM   #4
nickleus
Member
 
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, 01:25 PM   #5
scottmorris
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.
 
  


Reply


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 12:23 PM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 08:29 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 04:46 AM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 03:47 PM
Help I need help tarring multiple files in multiple directories VisionZ Linux - Newbie 28 03-25-2004 05:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:43 AM.

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