LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Batch file renaming (https://www.linuxquestions.org/questions/linux-newbie-8/batch-file-renaming-825622/)

Eddie Adams 08-11-2010 03:06 PM

Batch file renaming
 
I have some files formatted like:
bleh-blehdableh-5605-56052.pdf.jpg

The final desired result should be:
5605-56052.jpg

Please do you know a oneliner that can do this?

Bueller?

MensaWater 08-11-2010 03:18 PM

Not a one liner but this should work if run in the directory where the files are:

Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F- '{print $3"-"$4}')
  mv $file $newfile
done

The asterisks allow for variable lengths. If your sure of the component lengths of each file are the same you could tighten it up with ? for each character position to insure you only get the ones with those lengths.

arizonagroovejet 08-11-2010 03:18 PM

Any script can be made a one liner if you replace all the new lines with ; characters ;)

The are many, many, ways to do it. This one assumes that your bleh-blehdableh is always in lowercase and is designed to be reasonably obvious about what's being done.

Code:

for i in *;do mv "${i}" $(echo "${i}" | sed 's/\.pdf\././;s/[a-z]*-[a-z]*-//');done

arizonagroovejet 08-11-2010 03:25 PM

Quote:

Originally Posted by MensaWater (Post 4063370)
Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F- '{print $3"-"$4}')
  mv $file $newfile
done


Looks much cleverer than mine except it doesn't remove the .pdf part. I'm not familiar enough with awk to amend it so it does.

It did inspire this though:

Code:

for i in *;do mv "${i}" $(echo "${i}" | cut -d '-' -f 3,4 | cut -d '.' -f 1,3);done

Eddie Adams 08-11-2010 03:31 PM

I tried your oneliner arizona but no luck:

To be more precize, the name is like:

word-word-word-word-digits-smallword-word-5000-50002.jpg

I was hoping for some regular expression like cutting the words before the double digits (5000-50002.jpg)

Any idea?

arizonagroovejet 08-11-2010 03:38 PM

Quote:

Originally Posted by Eddie Adams (Post 4063382)
I tried your oneliner arizona but no luck:

That's because I wrote it for the format that you provided in your original post not the one you've now revealed.

This is more flexible in that it replaces any lowercase letters or - characters at the start of the filename.

Code:

for i in *;do
  mv $i $(echo "${i}" |  sed 's/[a-z\-]*//')
done


Eddie Adams 08-12-2010 01:04 AM

Thanks! With sed I can handle it!

MensaWater 08-12-2010 08:31 AM

[QUOTE=arizonagroovejet;4063376]Looks much cleverer than mine except it doesn't remove the .pdf part. I'm not familiar enough with awk to amend it so it does.


I forgot about removing the pdf part. Just piping to an additional awk would do it:
Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F- '{print $3"-"$4}' |awk -F\. '{print $1"."$3}')
  mv $file $newfile
done

However you don't need to pipe it because you can specify multiple field separators in awk:
Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F "[-.]" '{print $3"-"$4"."$6}')
  mv $file $newfile
done


konsolebox 08-12-2010 08:56 AM

You might try this:

Code:

find -type -maxdepth 1 -iname '*.jpg' | sed 's@.*\([[:digit:]]\{4\}-[[:digit:]]\{5\}\).*@& \1.jpg@' | xargs echo mv
Please test it first. Remove echo if you think it's already the proper one.

The Perl version of the rename command may also make it simpler but it's not available in all systems.

ghostdog74 08-12-2010 09:31 AM

Quote:

Originally Posted by MensaWater (Post 4064128)
Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F- '{print $3"-"$4}' |awk -F\. '{print $1"."$3}')
  mv $file $newfile
done

However you don't need to pipe it because you can specify multiple field separators in awk:
Code:

for file in $(ls *-*-*-*.pdf.jpg)
do newfile=$(echo $file |awk -F "[-.]" '{print $3"-"$4"."$6}')
  mv $file $newfile
done



use shell expansion instead of ls.
Code:

for file in *-*-*-*.pdf.jpg
do

done


grail 08-12-2010 09:32 AM

Well I am not sure if you are running from within the directory or elsewhere, but I am going to assume the first:
Code:

for f in *.jpg
do
    rename -n 's/[^[:digit:]]+([^\.]+).*/$1.jpg/' "$f"
done

Remove the -n once you are happy with the output.

MensaWater 08-12-2010 11:08 AM

Quote:

Originally Posted by ghostdog74 (Post 4064197)
use shell expansion instead of ls.
Code:

for file in *-*-*-*.pdf.jpg
do

done


I see that advice all the time but often see issues with it in practice even though I've seen others argue that doing the ls will cause problems but I haven't seen any (other than needing to quote files with spaces or special characters).

konsolebox 08-12-2010 11:26 AM

Quote:

Originally Posted by MensaWater (Post 4064293)
I see that advice all the time but often see issues with it in practice

If there are some can you give an example? I hope you don't mind. I'm sort of curious and need to know. I'll think about it with my scripts if there really is.

MensaWater 08-12-2010 02:56 PM

Quote:

Originally Posted by konsolebox (Post 4064312)
If there are some can you give an example? I hope you don't mind. I'm sort of curious and need to know. I'll think about it with my scripts if there really is.

I don't recall them at the moment - just that I tried it when I've seen the advice and found it didn't work as expected. It's always possible it's due to "my" expectations as oppose to others' expectations. :)


All times are GMT -5. The time now is 07:44 PM.