LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-11-2010, 04:06 PM   #1
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Rep: Reputation: 0
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?

Last edited by Eddie Adams; 08-11-2010 at 04:07 PM.
 
Old 08-11-2010, 04:18 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
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.
 
Old 08-11-2010, 04:18 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
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
 
Old 08-11-2010, 04:25 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
Quote:
Originally Posted by MensaWater View Post
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
 
Old 08-11-2010, 04:31 PM   #5
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Original Poster
Rep: Reputation: 0
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?
 
Old 08-11-2010, 04:38 PM   #6
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
Quote:
Originally Posted by Eddie Adams View Post
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
 
Old 08-12-2010, 02:04 AM   #7
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks! With sed I can handle it!
 
Old 08-12-2010, 09:31 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
[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
 
Old 08-12-2010, 09:56 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 08-12-2010, 10:31 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by MensaWater View Post
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
 
Old 08-12-2010, 10:32 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
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.
 
Old 08-12-2010, 12:08 PM   #12
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
Quote:
Originally Posted by ghostdog74 View Post
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).
 
Old 08-12-2010, 12:26 PM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by MensaWater View Post
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.
 
Old 08-12-2010, 03:56 PM   #14
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,681
Blog Entries: 14

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
Quote:
Originally Posted by konsolebox View Post
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.
 
  


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
batch renaming based on wildcards? Cultist Linux - General 2 07-20-2010 06:42 PM
help in batch renaming aapkae Linux - Newbie 5 02-13-2008 07:49 PM
batch renaming of files sycamorex Programming 9 08-13-2007 03:24 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 04:24 PM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 10:41 PM


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

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