LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-17-2003, 08:31 AM   #1
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Rep: Reputation: 15
modify bash script - recursive action


I have a script i use to convert and move files regularly uploaded to an image gallery.
Now those that send over the files (a mac shop) have decided that they will be sending them in compressed .sea files... this has a great compression ratio and will save mucho bandwidth and time - unfortunately my script assumes that the files in question are in the root of the directory and also that they are all image files.

I need to extend the script so that it unstuffs the .sea file and performs the conversion and subsequent functions to all files in the root and it's recursive directories. The .sea file will be in the /home/image/convert directory but will no doubt uncompress into it's own. I.e. a file called test.sea will extract to /home/image/convert/test

Also - i need to extend the search and replace for any unconventional characters in the file names. Right now it replaces spaces with a _ , i need to also replace any periods with a _

Any help or pointers is greatly appreciated

Cheers,

S.



Here is the original script -



#!/bin/bash
#####################################################################
IMAGEDIR='/home/image/convert/'
DATE=`date +%m-%d`
JPG='/usr/local/apache/htdocs/imagegallery/admin/mainimages/tmp/'
TIF='/usr/local/apache/htdocs/imagegallery/admin/mainimages/'
ERRORS='/tmp/errors'
LIST='/tmp/list'
RESULTS='/tmp/results'
CONVERTED='/tmp/converted'
IMAGES="*"
#####################################################################
# Convert files and log actions
#####################################################################
#
# Create three files - error, list and results.
#
# list = file listing of all files converted that day (size and name)
# errors = errors generated by conversion
# converted = output of file conversion
# results = results or errors of the conversion process
#
#####################################################################
cd ${IMAGEDIR}
# Rename files with spaces - replace the spaces with a _
# NEED TO ADD replace statement for .
#
find ${IMAGEDIR} -name '* *' -type f | sort | while read FILE
do
NEWFILE=`dirname "${FILE}"`/`basename "${FILE}" | sed 's/ /_/g;'`
mv "${FILE}" "${NEWFILE}"
done
#
# Populate list of files > generated each time and cleaned out at the end of the daily script
ls -lh ${IMAGEDIR} | awk ' { print $9 " " $5 } '>>$LIST
#
# THERE HAS TO BE A BETTER WAY TO DO THIS other than a blanket do..
#
for files in ${IMAGES}
do
(
echo converting ${files} >>$CONVERTED
#
# echo output from conversion to converted log file/convert the files/move the files and set permissions
#
convert ${files} -resize 800x600 -colorspace rgb ${files}.jpg >>$CONVERTED 2>&1
#
# mv files to web space and set permissions
#
mv ${files} ${TIF}${files}_${DATE}.tif
mv ${files}.jpg ${JPG}${files}_${DATE}.jpg
)
done
cd ${TIF}
chown nobody:nobody *.tif
chmod 664 *.tif
cd ${JPG}
chown nobody:nobody *.jpg
chmod 664 *.jpg
#
# Generate Result section
#
#
cat $CONVERTED |egrep "converting|error" >>$RESULTS
 
Old 09-17-2003, 09:01 AM   #2
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
find seems to be part of your solution. find <dir> -type f will list all the files in a directory recursively.

To replace periods I guess you can use sed s/'\.'/'_'/. Another way to do it - just exchange one character to another - is to use tr.

find <dir> -type f | tr '[:punct:][:space:]' '_'

will probably do approximately what you want :)

*testing*

Hm, it seems that the [:space:] set will also match newline, but that is not a big problem.



Last edited by Bebo; 09-17-2003 at 09:07 AM.
 
Old 09-17-2003, 09:17 AM   #3
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
thanks for the reply - but i can't seem to get the syntax of the tr command correct...

[root@image test]# ls
file.1 file.2 file.3
[root@image test]# find /home/image/convert/test -type f | tr '[:punct:]' '_'
_home_image_convert_test_file_1
_home_image_convert_test_file_2
_home_image_convert_test_file_3
[root@image test]# ls
file.1 file.2 file.3
[root@image test]# find . -type f | tr '[:punct:]' '_'
__file_1
__file_2
__file_3
[root@image test]# ls
file.1 file.2 file.3

Last edited by xscousr; 09-17-2003 at 09:26 AM.
 
Old 09-17-2003, 09:34 AM   #4
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Oops... The underscores at the beginning come from the directory part. Maybe you should only apply the tr on the basename?

Ottherwise... Do it like this:

Code:
find . -type f | tr '[:punct:]' '_' | sed s/'^_*'//
It'll be very ugly, but it works.

Last edited by Bebo; 09-17-2003 at 09:42 AM.
 
Old 09-17-2003, 09:57 AM   #5
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
Great - that works (as you can see) ... but (dumb question alert)..

why is the change not applied?

[root@image convert]# ls
5459 54591 5459_6574_03 5459_6574_04 5459_6574_05 Test.sea stu.1.2 test
[root@image convert]# ls
5459 54591 5459_6574_03 5459_6574_04 5459_6574_05 Test.sea stu.1.2 test
[root@image convert]# find . -type f | tr '[:punct:]' '_' | sed s/'^_*'//
test_stu_2_3
5459_6574_03
54591
5459_6574_04
5459_6574_05
5459
Test_sea
stu_1_2
[root@image convert]# ls
5459 54591 5459_6574_03 5459_6574_04 5459_6574_05 Test.sea stu.1.2 test
 
Old 09-17-2003, 10:11 AM   #6
xscousr
Member
 
Registered: Jul 2003
Location: Toronto
Distribution: Redhat
Posts: 89

Original Poster
Rep: Reputation: 15
i got it - Thanks Bebo
 
Old 09-17-2003, 01:52 PM   #7
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Yeah, you have to use mv or cp too, y'know

Ha, look at that, this silly remark is my hundredth post - wohoo!


Last edited by Bebo; 09-18-2003 at 07:55 AM.
 
  


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
Recursive Upload Script Kenji Miyamoto Linux - General 1 09-01-2005 11:18 PM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 07:03 PM
Bash Programming, Recursive/Iterative Calls on Folder Contents gtwilliams Linux - Newbie 1 07-06-2005 06:44 PM
Recursive search in bash scripting ! zulfilee Linux - Software 3 12-12-2004 10:40 PM
Issue with recursive script passing arguments gauge73 Linux - Newbie 0 01-06-2004 07:16 PM


All times are GMT -5. The time now is 09:08 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration