Searching a directory tree for a list of files
Searching a directory tree for a list of files
am 14.10.2007 20:04:47 von Carbon
Is there a better way to do this?
for dir in $(ls -F /work/data* | grep /$); do
for file in ${FILELIST}; do
echo ${dir}${file} >/tmp/.list-$$
done
done
cpio -ov --format=crc < /tmp/.list-$$ > /dev/st0
Basically I'm trying to generate a list of all directories in /work which
start with the letters data and then match a list of filenames which will
then be written to tape. I'm sure there are better ways to do this but I
haven't been able to figure one out.
Re: Searching a directory tree for a list of files
am 14.10.2007 22:35:33 von Carbon
On Sun, 14 Oct 2007 18:04:47 +0000, Carbon wrote:
> Is there a better way to do this?
>
> for dir in $(ls -F /work/data* | grep /$); do
> for file in ${FILELIST}; do
> echo ${dir}${file} >/tmp/.list-$$
> done
> done
> cpio -ov --format=crc < /tmp/.list-$$ > /dev/st0
>
> Basically I'm trying to generate a list of all directories in /work which
> start with the letters data and then match a list of filenames which will
> then be written to tape. I'm sure there are better ways to do this but I
> haven't been able to figure one out.
Thinko.
[ -f ${dir}${file} ] && echo ${dir}${file} >>/tmp/.list-$$
Re: Searching a directory tree for a list of files
am 14.10.2007 22:42:22 von cfajohnson
On 2007-10-14, Carbon wrote:
>
>
> Is there a better way to do this?
>
> for dir in $(ls -F /work/data* | grep /$); do
> for file in ${FILELIST}; do
> echo ${dir}${file} >/tmp/.list-$$
> done
> done
> cpio -ov --format=crc < /tmp/.list-$$ > /dev/st0
>
> Basically I'm trying to generate a list of all directories in /work which
> start with the letters data and then match a list of filenames which will
> then be written to tape. I'm sure there are better ways to do this but I
> haven't been able to figure one out.
If you don't need to check whether the files exist:
for dir in /work/data*/.
do
printf "${dir%.}%s\n" $FILELIST
done | cpio -ov --format=crc > /dev/st0
To check each file:
for dir in /work/data*/.
do
for file in $FILELIST
do
f=${dir%.}$file
[ -f "$f" ] && printf "%s\n" "$f"
done
done | cpio -ov --format=crc > /dev/st0
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: Searching a directory tree for a list of files
am 15.10.2007 01:08:48 von Carbon
On Sun, 14 Oct 2007 16:42:22 -0400, Chris F.A. Johnson wrote:
> To check each file:
>
> for dir in /work/data*/.
> do
> for file in $FILELIST
> do
> f=${dir%.}$file
> [ -f "$f" ] && printf "%s\n" "$f"
> done
> done | cpio -ov --format=crc > /dev/st0
Chris,
Thank you. I can't believe I didn't know you could just send the output
from for loops down a pipe like that. I also like the bit about
subtracting the trailing dot from the directory list. Very clean, and
quite a bit more efficient than my method.
Just one question if you don't mind. I normally use echo. The printf above
is a string followed by a newline, I believe. Isn't this the default
behavior of echo, or are there situations where echo doesn't do the
correct thing?
Re: Searching a directory tree for a list of files
am 16.10.2007 00:44:18 von cfajohnson
On 2007-10-14, Carbon wrote:
....
>> [ -f "$f" ] && printf "%s\n" "$f"
....
> Just one question if you don't mind. I normally use echo. The printf above
> is a string followed by a newline, I believe. Isn't this the default
> behavior of echo, or are there situations where echo doesn't do the
> correct thing?
Unless you know _exactly_ what you are printing, it is best to
avoid echo. Its behaviour is inconsistent across systems, and even
within a particular shell. For example, bash can be set (either at
compile time or run time) to interpret \c as a sequence to
suppress a newline or to print a 'c' (or \c if single quoted).
A POSIX-compliant echo will print -n, whereas most versions
interpret it as an option to suppress a newline.
These days, I only use echo when I am printing a literal string
and (not or) I am feeling lazy.
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: Searching a directory tree for a list of files
am 16.10.2007 09:17:12 von Stephane CHAZELAS
2007-10-15, 18:44(-04), Chris F.A. Johnson:
> On 2007-10-14, Carbon wrote:
> ...
>>> [ -f "$f" ] && printf "%s\n" "$f"
> ...
>> Just one question if you don't mind. I normally use echo. The printf above
>> is a string followed by a newline, I believe. Isn't this the default
>> behavior of echo, or are there situations where echo doesn't do the
>> correct thing?
>
> Unless you know _exactly_ what you are printing, it is best to
> avoid echo. Its behaviour is inconsistent across systems, and even
> within a particular shell. For example, bash can be set (either at
> compile time or run time) to interpret \c as a sequence to
> suppress a newline or to print a 'c' (or \c if single quoted).
>
> A POSIX-compliant echo will print -n, whereas most versions
> interpret it as an option to suppress a newline.
[...]
No, a Unix compliant echo will print "-n", but in POSIX, the
behavior is unspecified, so it may print anything like "-n",
nothing or an error message on stderr like
"echo -n is not standard and should not be used".
In a POSIX echo however, "echo -e" will print "-e". bash is not
conformant in that regard, but you'll see that it is an
intentional breach. zsh and some ksh on some systems are not
either.
--
Stéphane