LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   copying and simultaneously renaming multiple files in present working dir (https://www.linuxquestions.org/questions/linux-newbie-8/copying-and-simultaneously-renaming-multiple-files-in-present-working-dir-800766/)

vijay_babu1981 04-08-2010 06:22 AM

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

pixellany 04-08-2010 07:10 AM

While in the directory, do this:
Code:

for filename in *.txt; do newname=$(echo $filename|sed 's/\(^.\)/\11/'); cp $filename $newname; done

grail 04-08-2010 09:24 AM

awk could look like:

Code:

ls *.txt | awk '{name=$0;sub(/./, "&1",name);print | "cp "$0" "name}'

pixellany 04-08-2010 09:26 AM

Ah yes---and so much more readable than mine.......pick your favorite inscrutable code....;)

grail 04-08-2010 09:38 AM

Well I am definitely no ghostdog, but I am enjoying playing with awk :D

vijay_babu1981 04-09-2010 07:39 AM

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

grail 04-09-2010 08:18 AM

Well I created a directory with 100+ files in it and ran your copy of the code and all files were copied instantly??

vijay_babu1981 04-09-2010 08:37 AM

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

vijay_babu1981 04-09-2010 08:45 AM

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

vijay_babu1981 04-09-2010 09:07 AM

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

i92guboj 04-09-2010 09:33 AM

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

grail 04-09-2010 10:07 AM

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.


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