LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-18-2009, 04:13 AM   #1
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Rep: Reputation: 15
[SOLVED] Changing one word in filename to uppercase


I have a large collection of files (pdf books).
I would like to mass rename them like this:

In:
Gilbert, Machiavelli and Guicciardini.pdf

Out:
GILBERT - Machiavelli and Guicciardini.pdf

Is there a way to do this automatically, perhaps with sed or tr?

Thanks in advance.

Last edited by musonio; 12-18-2009 at 08:39 AM.
 
Old 12-18-2009, 04:37 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Bash 4.0
Code:
for file in *.pdf
do
    front=${file%%,*}
    front=${front^^} #change case to upper
    back=${file#*, }
    newname=$front-$back
    mv "$file" "$newname"
done

Last edited by ghostdog74; 12-18-2009 at 05:02 AM.
 
Old 12-18-2009, 04:50 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
ghostdog74 beat me to it (is it correct? -> it is now, after editing ):

Here's mine:
Code:
/bin/bash

ls *.pdf | \
while read THISLINE
do
  echo "${THISLINE}"
  firstWord=${THISLINE%%,*}
  restLine=${THISLINE##*,}
  newName="$(echo $firstWord | tr [:lower:] [:upper:] ) - ${restLine}"
  echo $newName
#  mv "${THISLINE}" "${newName}"
done
Uncomment the mv line if this is what you want.

Last edited by druuna; 12-18-2009 at 05:15 AM.
 
Old 12-18-2009, 05:10 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Why not a simple
Code:
sed -nr 's/[^-]*/\U&/p'
 
Old 12-18-2009, 05:12 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@syg00: That's nice, but just one piece of the puzzle. How do you actually move the files (with special chars in it) using your command. Do give all the code please.
 
Old 12-18-2009, 05:14 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Yeah, sorry; that was only meant to be the "guts" of the loop.
 
Old 12-18-2009, 05:34 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
Why not a simple
Code:
sed -nr 's/[^-]*/\U&/p'
it changes every word to upper, at my side
Code:
$ ls *pdf | sed -nr 's/[^-]*/\U&/p'
GILBERT, MACHIAVELLI AND GUICCIARDINI.PDF
 
Old 12-18-2009, 05:37 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by druuna View Post
@syg00: That's nice, but just one piece of the puzzle. How do you actually move the files (with special chars in it) using your command. Do give all the code please.
something like this (according to my memory..)
Code:
ls *pdf | sed -nr 's/[^-]*/mv \"&\" \"\U&\" /p' | bash
 
Old 12-18-2009, 05:51 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
My bad - not concentrating; playing poker ... . Can be done in sed, but not as clean as I was hoping
Code:
sed -nr -e 's/[^,]*/\U&/' -e 's/, / - /p'
 
Old 12-18-2009, 07:59 AM   #10
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
MERDE!!! Here I was, thinking that when I came back nobody would have replied.
THANKS. THANKS. THANKS.

Just in case you are bored:
Among the pdf files, the format of half of them is like this:

Cooper (Justus Lipsius and the Revival of Stoicism in Late Sixteenth-Century Europe).pdf


I tried doing:
Code:
for file in *\).pdf
do
    front=${file%%(*}
    front=${front^^} #change case to upper
    back=${file#\(* }
    newname="$front $back"
		echo "FRONT=$front"
		echo "BACK=$back"
		echo "NEWNAME=$newname"
#     mv "$file" "$newname"
but I get:

COOPER Cooper (Justus Lipsius and the Revival of Stoicism in Late Sixteenth-Century Europe).pdf

The problem is in back
 
Old 12-18-2009, 08:26 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

In your original example there's always a , at the end of the first word, in the latest example there isn't (do post relevant examples ).

Slightly changing my suggestion will take care of the new example:
Code:
#!/bin/bash

ls *\).pdf | \
while read THISLINE
do
  echo "${THISLINE}"
  firstWord=${THISLINE%%(*}
  restLine=${THISLINE##*(}
  newName="$(echo $firstWord | tr [:lower:] [:upper:] ) - ${restLine}"
  echo $newName
#  mv "${THISLINE}" "${newName}"
done
(changes in bold).

Neither will do both (word, and word (). But the ls <regex> takes care of targeting the correct files.

Hope this helps.
 
Old 12-18-2009, 08:27 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
remove the slash then, try again and see
 
Old 12-18-2009, 08:38 AM   #13
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
THANKS once again.
That did it.

BTW: I had intentionally left this second part out so as not to bother you kind people. My idea was to get help on the basic procedure and adapt it to other cases. Unfortunately I couldn't when the () showed up.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
changing filename RudraB Programming 7 07-08-2009 07:48 AM
changing filename using Bash Script tmbigrigg Programming 10 11-10-2007 07:02 AM
Changing Filename on Samba Share from XP InEeDhElPlInUx Linux - Software 17 04-20-2004 08:29 AM
Changing to a directory with a space in the filename? guitargeek Linux - General 5 02-13-2003 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:55 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
Open Source Consulting | Domain Registration