Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How2 find files, in current dir, beginning with letters c or f 1

Status
Not open for further replies.

kornShellScripter

Programmer
Apr 27, 2007
24
GB
I am writing a script that:
- does an rsh to various unix boxes, and on each one...
- list the files in a given directory that begin with certain letters

I currently do it the following way, which seems like a cludge and I was hoping if any of you had a better suggestion?

I use
"find * -prune -type f \(-name 'c*' -o -name 'f*'\)"
which can become quite cumbersome.

Here is the full implementation, which again I'm sure could be done better:

Code:
FILE_LIST[${INDEX_COUNT}]=$(rsh ${WHICH_HOST[${INDEX_COUNT}]:?} \
    "if ! cd ${WHICH_DATA_DIRECTORY[${INDEX_COUNT}]:?}
     then
        echo foo.ksh states the Folder is\<br\>
        echo ${WHICH_DATA_DIRECTORY[${INDEX_COUNT}]:?}\<br\>
        echo which does not exist
     else
        find * -prune -type f \(-name 'c*' -o -name 'f*'\)
     fi
    " 2>/dev/null)
 
Hi

If your [tt]find[/tt] supports it :
Code:
find * -prune -type f -name '[cf]*'

[gray]# or ( but adds the path )[/gray]

find . -maxdepth 1 -type f -name '[cf]*'

[gray]# or ( does not add the path )[/gray]

find . -maxdepth 1 -type f -name '[cf]*' -printf '%f\n'
But is possible that your current way is the best for your system.

Feherke.
 
There is a problem with this method:

if there are no files found, FILE_LIST="\n", which is a pain. Sure, I can test for this and remove it later, but it would be neater if there is a way to cause FILE_LIST="" if find finds no files?
 
-name '[cf]*' is a perfect legal shell pattern and thus should be admitted by any version of find.
 
Hi PHV,

What I meant was:

Code:
$ FOO=$(find * -prune -type -f -name file_does_not_exist)
$ echo $FOO|od -c                             
0000000  \n
0000001
$

That new-line is proving to be a pain.
 
That new-lines comes from the echo command, doesn't it ?
 
try something like these:

Code:
[red]$[/red] echo "FOO=\"${FOO}\""
FOO=""

[red]$[/red] if [ "${FOO}" = '' ];then echo null string;else echo not null string;fi
null string

[red]$[/red] [ "${FOO}" = '' ] && echo null string || echo not null string
null string


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top