- find files command within script
- Posted by Stuart H on September 29th, 2003
Within a script that i have written i have a routine to rename whatever files
are within a directory and it's sub's.
I'm stripping the file names for spaces, periods and .tif extensions, replacing with a _
Currently it looks like this:
filedir='/tmp/stage'
find ${filedir} -type f | sort | while read FILE
do
(
NEWFILE=`dirname "${FILE}"`/`basename "${FILE}" | sed 's/ /_/g;'| sed 's/\.tif//g;'|sed 's/\.//g;'`
mv ${FILE} ${NEWFILE}
if [ $? -eq 0 ]; then
echo "Renaming of ${FILE} complete." >> ${log}
else
echo "Renaming failed."
echo "-> Critical failure, renaming of ${FILE} failure." >> ${log}
read
echo "-> Exiting."
exit 1
fi
)
done
This generates a few errors in my log when files that don't have these
characters are renamed..
I am looking for a way to list only the files that contain spaces, periods
and .tif to pass as NEWFILE
Any ideas?
Thanks in advance.
- Posted by Paul Lutus on September 29th, 2003
Stuart H wrote:
1. Which period? Will the one attached to ".tif" suffice? Then you have one
rule too many.
2. Did you mean "or" or "and"?
Here's the "or" case:
ls | egrep ' |\.tif$'
Here's the "and" case:
ls | egrep ' +.*\.tif$'
--
Paul Lutus
http://www.arachnoid.com
- Posted by Alan Connor on September 29th, 2003
On Mon, 29 Sep 2003 15:24:39 -0400, Stuart H <post@to_group.com> wrote:
find ${filedir} -type f -name '*.tif' -name '*.*.*' -name '* *'
Am not really sure of the second one, because it assumes that there is
also an extension with a period. But this should allow you to get the
job done. Put a ! in front of the -name and it will reject those names.
If you go beyond one apparent line, use a \ at the end to keep it one line
in fact.
--
Later, Alan C
You can find my email address at the website:
elrav1.html --> ACKNOWLDEGEMENTS/CONTACT (20k or less, plain text)
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
- Posted by John-Paul Stewart on September 29th, 2003
Alan Connor wrote:
[snip]
Do you want files matching all three conditions (i.e., each individual
file must contain at least one space, plus at least one period, plus it
must end in .tif)? Or do you want all files meeting one of the criteria
(i.e., any file with a space, any file with a period, any file ending in
..tif)?
The implied "and" in the above command line will find files in the first
case described above. If the OP is after the second case:
find ${filedir} -type f -and ( -name '*.tif' -or -name '*.*.*' -or -name
'* *' )
Note that the explicit use of -and is not strictly necessary; it is
only there to make obvious what would otherwise be done implicitly. The
parentheses around the -name conditions may not be strictly necessary
(AFAIK, -or is of lower precedence than -and), but again they clarify to
the human reader what is otherwise implied by command line. Note, too,
that they may need to be escaped on the command line as \( and \). I
haven't tested this (obviously).
- Posted by Alan Connor on September 29th, 2003
On Mon, 29 Sep 2003 18:57:25 -0400, John-Paul Stewart <jpstewart@sympatico.ca> wrote:
Couldn't get that to work at all, GNU find refused to accept the ( ) and
the "-and" didn't do it.
In fact, the only thing that would work was:
find ${filedir} -type f -name '*.tif' -or -type f -name '*.*.*' -or \
-type f -name '* *'
At least we've got it working :-)
--
Later, Alan C
You can find my email address at the website: contact.html
take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a
- Posted by Noi on September 30th, 2003
On Mon, 29 Sep 2003 15:24:39 -0400, Stuart H without thinking wrote:
The simple answer is to refine your the "Find" command to only select
files of interest ie, *.tif right. Or when parsing NEWFILE do a match for
space, .tif, . etc. and skip the file rename if there's no match.
Unfortunately for you I'm too tired to look up the syntax right now for
the find and sed you'd need. But I'm thinking
find ${filedir} -type f -name .tif | sort | while read FILE do
or
NEWFILE=`dirname "${FILE}"`/`basename "${FILE}" | grep .tif | sed 's/ /_/g;'|
- Posted by Eric Moors on September 30th, 2003
Just before the mv, add the following line of code:
[ ${FILE} = ${NEWFILE} ] && { echo "no renaming needed" ; continue }
or as first line in the loop
[ -z `echo "${NEWFILE}" | egrep '\ |\.'` ] && continue
the latter more accurately matches the thing you say you're
looking for. The first solution is more generic; no need to change
the regexp if you want to match something else.
Eric
- Posted by John-Paul Stewart on September 30th, 2003
Alan Connor wrote:
Weird. I had to escape them as \( and \) to prevent Bash from trying to
interpret them, but they worked fine in my quick test. GNU find version
4.1.20.
find ${filedir} -type f -name '*.tif' -or -name '*.*.*' -or -name '* *'
should work, too. (Again, from my quick tests, it does.) Since the
implicit "and" is of higher precedence than the -or conditions, it
evalutes the same as the command line you gave and the same as the one I
originally gave. In my first example the explicit -and plus the ( )
were just there for clarity. (I hate having to remember that conditions
are implicitly 'and'ed and so forth. It's so much clearer to state
that.)
Heck, I personally don't care whether it works exactly or not. My whole
point was to make the OP realize that it wasn't clear what he wanted and
that it may have to be done differently from your example, depending on
his circumstances. "Implementation is left as an exercise for the
reader" as they say.
- Posted by Stuart H on October 1st, 2003
On Mon, 29 Sep 2003 15:24:39 -0400, Stuart H wrote:
Thanks to all for the suggestions - and the lesson :-)
my apologies for not being specific enough, i should have used "or" instead
of "and" for the criteria, but you guessed that...
I ended up using
find -type f -and \( -name '*.tif' -or -name '*.*.*' -or -name '* *' \)
cheers
S.