Unix Shell - Can a filename pattern specify both hidden and non-hidden files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > Can a filename pattern specify both hidden and non-hidden files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Can a filename pattern specify both hidden and non-hidden files
lovecreatesbea...@gmail.com

2007-01-21, 1:26 am

I use find command in the following script to count files with
specified filename patterns of suffixes only in current directory
excluding its sub-directories.

But with * (star) in the -name pattern, I can't count hidden files
whose names begin with . (dot). If I use .* instead of *, then I only
count the hidden files. How can I count both hidden and non-hidden
files? Thank you.

#tested on Linux 2.4.27-2-386
#!/bin/sh

threshold=100
p='/home/jhl';
d='/*';
c='*'

find "$p" -path "$p$d" -prune -name "$c" -type f |
awk 'END {print NR}' |
if read cnt; then
if [ $cnt -le $threshold ]; then
echo -e "[INFO] directory: "$p", files: "$c", count: "$cnt",
\c"
echo "within limitation: "$threshold""
fi
fi

Icarus Sparry

2007-01-21, 1:26 am

On Sat, 20 Jan 2007 22:23:31 -0800, lovecreatesbea...@gmail.com wrote:

> I use find command in the following script to count files with
> specified filename patterns of suffixes only in current directory
> excluding its sub-directories.
>
> But with * (star) in the -name pattern, I can't count hidden files
> whose names begin with . (dot). If I use .* instead of *, then I only
> count the hidden files. How can I count both hidden and non-hidden
> files? Thank you.
>
> #tested on Linux 2.4.27-2-386
> #!/bin/sh
>
> threshold=100
> p='/home/jhl';
> d='/*';
> c='*'
>
> find "$p" -path "$p$d" -prune -name "$c" -type f |
> awk 'END {print NR}' |
> if read cnt; then
> if [ $cnt -le $threshold ]; then
> echo -e "[INFO] directory: "$p", files: "$c", count: "$cnt",
> \c"
> echo "within limitation: "$threshold""
> fi
> fi


The most obvious way to answer your question is to use

\( -name "*" -o -name ".*" \)

instead of
-name "*"
Obviously in the special case where you want to match everything, then
don't specify any restrictions on names in the first place.
Ed Morton

2007-01-21, 1:17 pm

lovecreatesbea...@gmail.com wrote:
> I use find command in the following script to count files with
> specified filename patterns of suffixes only in current directory
> excluding its sub-directories.
>
> But with * (star) in the -name pattern, I can't count hidden files
> whose names begin with . (dot). If I use .* instead of *, then I only
> count the hidden files. How can I count both hidden and non-hidden
> files?


*.*

Ed.
lovecreatesbea...@gmail.com

2007-01-21, 1:17 pm


Icarus Sparry wrote:
> On Sat, 20 Jan 2007 22:23:31 -0800, lovecreatesbea...@gmail.com wrote:
>
>
> The most obvious way to answer your question is to use
>
> \( -name "*" -o -name ".*" \)
>
> instead of
> -name "*"
> Obviously in the special case where you want to match everything, then
> don't specify any restrictions on names in the first place.


Thank you.

It works. The two conditions can be combined using -o operator. I
wasn't aware of that before.

Janis Papanagnou

2007-01-21, 1:17 pm

Ed Morton wrote:
> lovecreatesbea...@gmail.com wrote:
>
[vbcol=seagreen]
> *.*


*.* will match only files with dots in their name.
Was there a space intended between * [non-hidden] and .* [hidden]?

Janis
Ed Morton

2007-01-21, 1:17 pm

Janis Papanagnou wrote:
> Ed Morton wrote:
>
>
>
>
>
> *.* will match only files with dots in their name.
> Was there a space intended between * [non-hidden] and .* [hidden]?


No, I just needed more coffee...

Thanks,

Ed.
Stephane CHAZELAS

2007-01-22, 7:21 am

2007-01-20, 22:23(-08), lovecreatesbea...@gmail.com:
> I use find command in the following script to count files with
> specified filename patterns of suffixes only in current directory
> excluding its sub-directories.
>
> But with * (star) in the -name pattern, I can't count hidden files
> whose names begin with . (dot).

[...]

Then report it as a bug in your find implementation.

If using GNU find, upgrade to a later release. That was fixed in
version 4.2.2 in 2004.

-name '*'
should match dot files (except that "." and ".." are not
considered when find descend into directories).

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com