Recursive file operation in a directory
Hi,
I have a directory of jpg images and I'd like to run through the recursively so that the output file name is the same as the input, with only the file type suffix changed. A bit like doing cuneiform 001.jpg -o 001.txt cuneiform 002.jpg -o 002.txt and so on (there are a lot of images) I can't remember how to do this recursively in a shell script, could someone help please? (Also, if I wanted to do this on certain files but miss out on others, how would I achieve this? For instance, omit processing all files that had "img" in the name, such as 002img.jpg, 010img.jpg and so on) Many thanks! Paul |
Hu,
most people would use find for that. Somthing like: Code:
find . -name '*.jpg' ! -name '*img*.jpg' Code:
find . -name '*.jpg' ! -name '*img*.jpg' | xargs -I{} sh -c 'echo "$1 -o ${1%.*}.txt"' -- {} Eg Code:
find . -name '*.jpg' ! -name '*img*.jpg' -print0 | xargs -0 -I{} sh -c 'echo "$1 -o ${1%.*}.txt"' -- {} HTH, Evo2. |
Hi,
Many thanks, I shall try, FWIW, the filenames don't have whitespace, they are just of the variety 002.jpg 003a.jpg 005img.jpg 010.jpg 124img.jpg and so on - all I'm trying to veto are the ones that have "img" in the filename. Incidentally, what does "-- {}" mean? |
Hi,
the "-- {}" allows the xarg variable to be put into the $1 shell variable. There are other ways to achieve the same thing with xargs, but like find, its a very powerful program and once I've found one way that works and can remember, I'm less likely to explore the other options. Cheers, Evo2. |
an alternative
Code:
#!/bin/bash takes two options example , process files in current dir ( including sub directories ) excluding files that contain the string "img" Code:
TheScripit.sh . img with comments Code:
#!/bin/bash more reading http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ http://www.tldp.org/LDP/abs/html/ http://mywiki.wooledge.org/BashGuide http://www.gnu.org/software/bash/manual/bashref.html The tldp stuff is great, however there are some nasty bad habits in it, The mywiki.wooledge does a very good job of 'fixing' those habits a note on the heredocs use here Code:
find "${Path}" -name "*.jpg" -print0 | while read -d '' FileName;do However the downside is that any variables you set inside the loop are 'lost' as the | ( pipe ) starts a subshell best example I can come up with here.. create a new dir, and 'touch' example files Code:
touch 00{1..5}{,img}.jpg Code:
#!/bin/bash Code:
#!/bin/bash |
Hi,
well, if we're doing loops and shell scripts... here's how I would actually do it myself. I use zsh, so I'd run the follow command: Code:
for f in **/*.jpg~*img.jpg ; do echo "$f" -o "${f%.*}.txt"; done The '**' in zsh will recurse into the sub directories and the '~' in the glob excludes the *img.jpg files. Cheers, Evo2. |
nice, I might have to try using zsh
|
I tried the one-line command of yours, evo2:
Code:
find . -name '*.jpg' ! -name '*img.jpg' | xargs -I{} sh -c 'cuneiform "$1 -o ${1%.*}.txt"' -- {} Magick: Unable to open file (./057.jpg -o ./057.txt) reported by magick/blob.c:2866 (OpenBlob) But it works it I explicitly type into a shell: cuneiform 057.jpg -o 057.txt |
try remove " :
find . -name '*.jpg' ! -name '*img.jpg' | xargs -I{} sh -c 'cuneiform $1 -o ${1%.*}.txt' -- {} |
Thanks, it worked like a champ! :)
|
glad to help you.
If you really want to say thanks just press YES. Also please mark the thread SOLVED if your problem has been solved. |
Great, thanks!
|
All times are GMT -5. The time now is 11:40 AM. |