LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2010, 06:22 AM   #1
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
copying and simultaneously renaming multiple files in present working dir


How can I copy multiple files, each with a slightly different name from the SOURCE in the same directory?

example:
'/home/junk' contains A.txt, B.txt

I want to copy /home/junk/A.txt to /home/junk/A1.txt and /home/junk/B.txt to /home/junk/B1.txt using a single command.


Thanks for help
Regards
 
Old 04-08-2010, 07:10 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
While in the directory, do this:
Code:
for filename in *.txt; do newname=$(echo $filename|sed 's/\(^.\)/\11/'); cp $filename $newname; done
 
Old 04-08-2010, 09:24 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
awk could look like:

Code:
ls *.txt | awk '{name=$0;sub(/./, "&1",name);print | "cp "$0" "name}'
 
Old 04-08-2010, 09:26 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Ah yes---and so much more readable than mine.......pick your favorite inscrutable code....
 
Old 04-08-2010, 09:38 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Well I am definitely no ghostdog, but I am enjoying playing with awk
 
Old 04-09-2010, 07:39 AM   #6
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Thanks Grail.

I ran the following command to copy files in bulk.

Code:
ls *.txt | awk '{name=$0;sub(/.txt/,"_le.txt",name);print | "cp "$0" temp/" name}'
But this command is only copying few files at at time. Out of 70 files in the directory only 6-7 (random number of) files are copied in the temp directory each time I run this command.

It seems to me that the speed with which this is executed is too high for the computer to handle. May be I need to insert a blank line between each line.

Regards
 
Old 04-09-2010, 08:18 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Well I created a directory with 100+ files in it and ran your copy of the code and all files were copied instantly??
 
Old 04-09-2010, 08:37 AM   #8
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Hi Grail,

Then I have no idea why I am seeing such weird behaviour on my computer. Do you have any idea why such a thing may happen? Every time I run the command I see different number of files being copied.

Can you tell me a way to insert a blank line after each of the cp command? Will "\n" work? I am trying it.

Thanks
 
Old 04-09-2010, 08:45 AM   #9
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Hi Grail

When I run the following command
Code:
ls *.txt | awk '{name=$0;sub(/.txt/,"_le.txt",name);print | "echo "name}'
Each time I run it I see different number of filenames being printed. Sometimes 1, sometimes 2 or 4 etc.

Regards
 
Old 04-09-2010, 09:07 AM   #10
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Right now I have a very non-elegant solution:
Code:
ls *.txt | awk '{name=$0;sub(/.txt/,"_le.txt",name);print "cp " $0" "name}'>>copy_files
then chmod +x copy_files, then ./copy_files

Can someone give me a better solution? Or tell me why am i seeing this weird behviour as mentioned above?

Regards
 
Old 04-09-2010, 09:33 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
Too complex for my taste. In bash this would suffice:

Code:
for file in *.txt; cp "$file" "${file/.txt/1.txt}"; done
If you are not in bash, then just use sed or awk, but please, please, please: always quote file names unless you are 100% sure that no special characters will appear on their names. That will always lead to problems. Correctly quoting the names will never hurt.

Code:
for file in *.txt; do new=$(echo "$file"|sed <whatever>); cp "$file" "$new"; done
Also, please, don't ever parse the ls output like that example someone posted above. It's another way to call for problems, though a broadly extended one, unfortunately.

http://mywiki.wooledge.org/ParsingLs
 
1 members found this post helpful.
Old 04-09-2010, 10:07 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Quote:
ls *.txt | awk '{name=$0;sub(/.txt/,"_le.txt",name);print "cp " $0" "name}'>>copy_files
I assume this generates all the correct copy statements in the copy_files file and if so I too would like to know
why you are getting such spurious results.

I have now run the same script on a directory with almost 1000 txt files
and whilst there was a small pause (no doubt due to the size of data) the result still came through correctly.
I also checked the finished data in a comparison in a s/sheet (just to get away from the console in case I missed something)
and everything was correct.
 
  


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
how to write multiple threads working simultaneously ? indian Programming 3 09-19-2011 11:41 PM
cp help, copying multiple doc files from multiple dir to one dir. How do i do that? w@m Linux - Newbie 5 02-03-2010 09:56 AM
Little utility for copying and renaming Mp3 files hansalfredche Linux - Software 3 12-24-2006 11:07 AM
AWK script: moving, copying and renaming files uprjamaja Programming 10 12-05-2006 01:06 PM
Konqueror - Renaming multiple files simultaneously RodWC Linux - General 9 11-29-2006 09:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration