LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 01-04-2011, 10:46 AM   #1
ExitRitual
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
Unhappy Looking for help with Bash script to search folders, rename, and output to text file


Hello all! First post and all that.
I'm hoping some kind-soul can help me out with a bash script.

I'm trying to figure out how to get a script to:
  • scan folders and sub folders of specified directory for any file with the extension .jpg or .png
  • minimum folder depth of 3
  • if only 1 image, rename to Folder.<original extension>
  • if 0 or more than 1, output folder path to /<user name>/documents/follow-up.txt
  • if 1 image found & dimensions below 400x400 output to above txt file
  • if 1 image found & has non-square dimensions, output to above txt file

I've googled as best i can, found tutorials, but i admit; It's all very confusing for me. I don't handle lines of text all too well. Much more of an image person. Down-side to being a graphic designer, i suppose

What it's for:
I'm trying to finally finish cleaning up my music collection, and the album art (or lack thereof) is my final hurdle.

The minimum folder depth is to make it so that only album folders are scanned. My collection is organised as so: /Music/<basic genre>/<artist>/<album>, and i would be launching the script from /music.

Parts of what i can find and understand tell me that i can find the image types with:
find / -name ‘.jpg’ -o -name ‘.png’

And i can compare a list of folders with and without the search results with:
find ./* -mindepth 3 -type d > albums
diff albums has_art > needs_art
less needs_art


I realise half of the snippets of code i've found are probably laughably wrong, but hey. I'm trying

Any and all help appreciated.
 
Old 01-04-2011, 01:01 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Code:
#!/bin/bash
COUNTER=0
# scan folders and sub folders of specified directory 
[ $# -eq 0 -o $# -gt 1 -o -d "$1" ] || { echo "No directory name given, exiting."; exit 1; }
# for any file with the extension .jpg or .png
find "$1" -iname \*.jpg -o -iname \*.png | \
# minimum folder depth of 3 
# there is -maxdepth but no -mindepth
while read RESULT; do
# if 0 
# 'find' doesn't output anything if it doesn't match criteria
((COUNTER++))
# more than 1, output folder path to /<user name>/documents/follow-up.txt
[ $COUNTER -gt 1 ] && echo "output ${RESULT//\/*/} to ~/documents/follow-up.txt"
# if only 1 image, rename to Folder.<original extension>
[ $COUNTER -eq 1 ] && echo "rename ${RESULT//\/*/} to ${RESULT//\/*/}.EXTENSTION=${RESULT//*./}"
RESULTSIZE=($(file "${RESULT}" 2>/dev/null))
# Should use checkNumeric() { declare -i chkvar; str="$1"; [ $str -eq 0 ] 2>/dev/null && str=1; let "foo=${str}" 2>/dev/null || return 127; } here...
# if 1 image found & dimensions below 400x400 output to above txt file
[ ${RESULTSIZE[4]} -lt 400 -o ${RESULTSIZE[6]//,/} -lt 400 ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
# if 1 image found & has non-square dimensions, output to above txt file
[ ${RESULTSIZE[4]} -ne ${RESULTSIZE[6]//,/} ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
done; exit 0
Way crude but I spose it's a start for refining things.
 
1 members found this post helpful.
Old 01-04-2011, 01:03 PM   #3
ExitRitual
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Code:
#!/bin/bash
COUNTER=0
# scan folders and sub folders of specified directory 
[ $# -eq 0 -o $# -gt 1 -o -d "$1" ] || { echo "No directory name given, exiting."; exit 1; }
# for any file with the extension .jpg or .png
find "$1" -iname \*.jpg -o -iname \*.png | \
# minimum folder depth of 3 
# there is -maxdepth but no -mindepth
while read RESULT; do
# if 0 
# 'find' doesn't output anything if it doesn't match criteria
((COUNTER++))
# more than 1, output folder path to /<user name>/documents/follow-up.txt
[ $COUNTER -gt 1 ] && echo "output ${RESULT//\/*/} to ~/documents/follow-up.txt"
# if only 1 image, rename to Folder.<original extension>
[ $COUNTER -eq 1 ] && echo "rename ${RESULT//\/*/} to ${RESULT//\/*/}.EXTENSTION=${RESULT//*./}"
RESULTSIZE=($(file "${RESULT}" 2>/dev/null))
# Should use checkNumeric() { declare -i chkvar; str="$1"; [ $str -eq 0 ] 2>/dev/null && str=1; let "foo=${str}" 2>/dev/null || return 127; } here...
# if 1 image found & dimensions below 400x400 output to above txt file
[ ${RESULTSIZE[4]} -lt 400 -o ${RESULTSIZE[6]//,/} -lt 400 ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
# if 1 image found & has non-square dimensions, output to above txt file
[ ${RESULTSIZE[4]} -ne ${RESULTSIZE[6]//,/} ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
done; exit 0
Way crude but I spose it's a start for refining things.
Thank you! I shall now run off like an excited child and try this
 
Old 01-05-2011, 04:48 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
Quote:
Originally Posted by unSpawn View Post
Code:
# there is -maxdepth but no -mindepth
Why shouldn't there be a -mindepth? My find has this option too.

Last edited by Reuti; 01-05-2011 at 04:49 AM. Reason: Typo
 
Old 01-05-2011, 04:53 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
I don't think the idea is that -mindepth does not exist, but rather that it is not being used or needed

Edit: My bad ... appears I did not read all of the code first ... please ignore

Last edited by grail; 01-05-2011 at 04:55 AM.
 
Old 01-05-2011, 04:55 AM   #6
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hi,

As does mine. From the man page:
Code:
-mindepth levels
Do not apply any tests or actions at levels less  than  levels  (a
non-negative integer).  -mindepth 1 means process all files except
the command line arguments.
Kind regards,

Eric
 
Old 01-05-2011, 05:07 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Apparently there is. Obviously I never needed it else I would have remembered that. Thanks all for pointing out switch existence.
 
Old 01-05-2011, 06:02 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
@unSpawn - would you please explain what the following is supposed to do:
Code:
RESULTSIZE=($(file "${RESULT}" 2>/dev/null))
I have used 'file' on several pictures but have had no size information returned
I get something like the following:
Code:
Pictures/0398.jpg: JPEG image data, JFIF standard 1.02
Just curious if file is supposed to produce something else? Or maybe for only certain pictures?
 
Old 01-05-2011, 07:31 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Quote:
Originally Posted by grail View Post
RESULTSIZE=($(file "${RESULT}" 2>/dev/null))
This produces an array.


Quote:
Originally Posted by grail View Post
I have used 'file' on several pictures but have had no size information returned
I get something like the following:
Code:
Pictures/0398.jpg: JPEG image data, JFIF standard 1.02
Just curious if file is supposed to produce something else? Or maybe for only certain pictures?
Crap. I didn't test JPEG... Luckily there's ImageMagick's 'identify':
Code:
RESULTSIZE=($(identify "${RESULT}" 2>/dev/null)); RESULTSIZE=($(RESULTSIZE[2]//x/ ))
[ ${RESULTSIZE[0]} -lt 400 -o ${RESULTSIZE[1]} -lt 400 ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
[ ${RESULTSIZE[0]} -ne ${RESULTSIZE[1]} ] && echo "output ${RESULT} to ~/documents/follow-up.txt"
 
  


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 script automatic file rename if exists AutoC Programming 3 09-03-2009 04:46 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Bash script- capture cdparanoia text output code-breaker Linux - Software 7 08-03-2006 10:13 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 02:36 PM


All times are GMT -5. The time now is 12:53 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