LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 09-21-2010, 11:29 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
More Bash regex help please...


Grr...wish I was better at this...

I'm trying to convert all file extensions for files in many sub-directories from uppercase to lowercase. I have two problems, how to list the absolute path to the files recursively over many sub-directories for which I so far have this:-

Code:
find ~/Photos -print
...which would be fine, except it gives the directories on their own when it finds them rather than just the files with absolute paths. I couldn't find a switch for the "ls" command to do this, so I had to improvise with "find".

...and once I get grab each absolute file name, to just change the file extension rather than the entire file, which is what I have at the moment...

Code:
for filename in $(cat ${TEMP_FILE_LIST})
do
            RENAMED_FILE=`echo "$filename" | tr [:upper:] [:lower:]`
            mv ${PHOTOS_DIRECTORY}/$filename ${PHOTOS_DIRECTORY}/${RENAMED_FILE}
done
Any ideas please?
 
Old 09-21-2010, 11:47 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Code:
find ~/Photos -type f -print
to list only files (man find)

For the second part, you have the right idea. See this FAQ: http://www.faqs.org/faqs/unix-faq/fa...section-6.html
 
1 members found this post helpful.
Old 09-21-2010, 11:51 AM   #3
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 58
Maybe,

Code:
find ${PHOTOS_DIRECTORY}/ -type f | while read filename; do\
 RENAMED_FILE=$(echo "$filename" | tr [:upper:] [:lower:]);\
 mv ${PHOTOS_DIRECTORY}/$filename ${PHOTOS_DIRECTORY}/${RENAMED_FILE}; done
 
1 members found this post helpful.
Old 09-21-2010, 11:52 AM   #4
WoodsyDotOrg
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Rep: Reputation: 1
find ~/Photos -type f -print

Warning: Watch out for filenames with spaces

Last edited by WoodsyDotOrg; 09-21-2010 at 11:55 AM.
 
1 members found this post helpful.
Old 09-21-2010, 11:58 AM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Excellent - got it, thanks
 
Old 09-21-2010, 12:02 PM   #6
WoodsyDotOrg
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Rep: Reputation: 1
Even better!

find . -type f -print | sed 's/\(.*\/\)\(.*\)/mv "\1\2" "\1\L\2"/' |sh

Works ok with spaces
 
Old 09-21-2010, 12:13 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The best practice way to read the filenames into a bash variable is
Code:
while IFS= read -r -d '' file
do
   Do what you want with "$file"
done < <(find $dir -type f -print0)
This works for files containing all possible characters including backspace, newline, quotes ...

The -type f option tells find to give only names of ordinary files -- no directories, device files ...

To be fully robust the script should cope with files in the same directory with the same extension except for case, for example foo.WMV, foo.wmv ... and files with no extension and file with extensions already in lowercase.

Putting it all together (not tested):
Code:
while IFS= read -r -d '' fullpath
do
    dir="${fullpath%/*}"
    filename="${fullpath##*/}"
    filename_no_extension="${filename%%*.}"
    if [[ $filename_no_extension = $filename ]]; then
        echo "$fullpath has no extension; skipping"
        continue
    fi
    extension="${filename##.*}"
    extension_lowercase="$( echo -n "$extension" | /usr/bin/tr '[:upper:]' '[:lower:]' )"
    if [[ $extension_lowercase = $extension ]]; then
        echo "$fullpath extension already lower case; skipping"
        continue
    fi
    newfullpath="$dir/$filename_no_extension.$extension_lowercase"
    if [[ -f $newfullpath ]]; then
        echo "New name $newfullpath already exists; skipping"
        continue
    fi
    echo mv "$fullpath" "$newfullpath"  # Remove echo after testing
done < <(find ~/Photos -type f -print0)
If you would like explanation of how any of the above works please ask.
 
Old 09-21-2010, 08:18 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Also if you are using bash 4 you can use the following for case changing:

Code:
var=veryMixedUpVariable
echo ${var}            # veryMixedUpVariable
echo ${var^}           # VeryMixedUpVariable
#         *              First char --> uppercase.
echo ${var^^}          # VERYMIXEDUPVARIABLE
#         **             All chars  --> uppercase.
echo ${var,}           # veryMixedUpVariable
#         *              First char --> lowercase.
echo ${var,,}          # verymixedupvariable
#         **             All chars  --> lowercase.
 
  


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
bash regex - sort czezz Programming 3 02-05-2009 08:41 PM
bash : crop some text with regex dodoecchi Programming 2 01-30-2009 02:10 PM
Need help with bash + regex onesikgypo Programming 2 01-10-2009 11:08 AM
Regex in bash? BeholdMyGlory Linux - Newbie 4 01-10-2009 09:32 AM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 06:37 PM


All times are GMT -5. The time now is 04:56 AM.

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