LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   creating copies of files in the same directory (https://www.linuxquestions.org/questions/linux-newbie-8/creating-copies-of-files-in-the-same-directory-4175496018/)

validator456 02-24-2014 06:55 AM

creating copies of files in the same directory
 
I want to create copies of files in the same directory. So something like:
image1027.jpg
image1028.jpg
image1029.jpg
to:
image1027.jpg
image1027_a.jpg
image1028.jpg
image1028_a.jpg
image1029.jpg
image1029_a.jpg

I think it can be done with regex but I don't have any experience with it.

rlx 02-24-2014 07:30 AM

Quote:

Originally Posted by validator456 (Post 5123680)
I want to create copies of files in the same directory. So something like:
image1027.jpg
image1028.jpg
image1029.jpg
to:
image1027.jpg
image1027_a.jpg
image1028.jpg
image1028_a.jpg
image1029.jpg
image1029_a.jpg

I think it can be done with regex but I don't have any experience with it.

>for i in 7 8 9; do cp -p image102$i.jpg image102$i_a.jpg; done
# or if you want to copy all jpeg's,
>for i in *.jpg; do b=$(basename $i .jpg); do cp -p $b.jpg $b_a.jpg; done
# or
>for i in *.jpg; do b=$(echo "$i" | sed -e 's|\.jpg||'); do cp -p $b.jpg $b_a.jpg; done
# or, if file names may have weird characters in such as 'spaces',
>\ls *.jpg | while read i; do b=$(echo "$i" | sed -e 's|\.jpg$||'); a="cp -p '$b.jpg' '${b}_a.jpg'"; echo "$a"; eval "$a"; done

Have fun!

validator456 02-24-2014 07:46 AM

The last line worked. Thank you, rlx.


All times are GMT -5. The time now is 02:38 AM.