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 |
While in the directory, do this:
Code:
for filename in *.txt; do newname=$(echo $filename|sed 's/\(^.\)/\11/'); cp $filename $newname; done |
awk could look like:
Code:
ls *.txt | awk '{name=$0;sub(/./, "&1",name);print | "cp "$0" "name}' |
Ah yes---and so much more readable than mine.......pick your favorite inscrutable code....;)
|
Well I am definitely no ghostdog, but I am enjoying playing with awk :D
|
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}' 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 |
Well I created a directory with 100+ files in it and ran your copy of the code and all files were copied instantly??
|
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 |
Hi Grail
When I run the following command Code:
ls *.txt | awk '{name=$0;sub(/.txt/,"_le.txt",name);print | "echo "name}' Regards |
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 Can someone give me a better solution? Or tell me why am i seeing this weird behviour as mentioned above? Regards |
Too complex for my taste. In bash this would suffice:
Code:
for file in *.txt; cp "$file" "${file/.txt/1.txt}"; done Code:
for file in *.txt; do new=$(echo "$file"|sed <whatever>); cp "$file" "$new"; done http://mywiki.wooledge.org/ParsingLs |
Quote:
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. |