PDA

View Full Version : Search for files only


RTAM
04-10-03, 17:18
Hi,
i have a directory say A which has a few files and may have directories say B,C as well. I want to list only the files from the A directory without the listing of directories B & C. Currently i am using "for the_file in `ls`".In this for loop i am doing i am taking each file and doing some processing. But with this even the unwanted directory name also comes in the listing.Can anyone help?

-Thnks
RTAM

stevetucknott
04-11-03, 04:18
Try using the find command.
ie find . -type f -exec ls -l {} \;
should list the files in the current directory in long format
find . -type f -print
just lists the file names. Check the man pages for other options.

Originally posted by RTAM
Hi,
i have a directory say A which has a few files and may have directories say B,C as well. I want to list only the files from the A directory without the listing of directories B & C. Currently i am using "for the_file in `ls`".In this for loop i am doing i am taking each file and doing some processing. But with this even the unwanted directory name also comes in the listing.Can anyone help?

-Thnks
RTAM

RTAM
04-11-03, 13:52
Hi,
Thanks for the prompt reply but both the find commands specified by you give a listing of files even of the subdirectory. That is the command gives files from main directory A as well as from the sub-dir B while i want to get files only from A and not look at the sub-directory at all.

Please suggest,
RTAM

Originally posted by stevetucknott
Try using the find command.
ie find . -type f -exec ls -l {} \;
should list the files in the current directory in long format
find . -type f -print
just lists the file names. Check the man pages for other options.

stevetucknott
04-12-03, 06:16
That's no problem - the find command (depending on the Unix/Linux variant) normally has an option to say at what level you want to perform the search. On my Linux box (Redhat 8) the parameter is
-maxdepth
so in my case the command to restrict the search to the current directory is:

find top_level_directory -maxdepth 0 -type f -print .........

Under SCO, the command is different (-level from recollection, but I'm old so don't quote me on that!) - man pages should tell you.
There are other good usable features such as -newer (finds files / directories newer that the specified file) -mount (only look in the specified file system) -size (files larger than)....... take a look.

Good luck,

Steve
Originally posted by RTAM
Hi,
Thanks for the prompt reply but both the find commands specified by you give a listing of files even of the subdirectory. That is the command gives files from main directory A as well as from the sub-dir B while i want to get files only from A and not look at the sub-directory at all.

Please suggest,
RTAM

jwall
05-08-03, 12:24
Hi RTAM

You may have problems finding an equivalent to '-maxdepth' in find, depending on your UNIX flavour.

Try:

ls -l | grep -v "^[d]" # long listing

or

ls -l | grep -v "^[d]" | awk '{print $9}' # filenames only

The commands run OK on HP-UX, AIX and Tru64

regards

Jack

juanep
06-24-03, 09:47
Hi RTAM

Try with


for f in *
do
test -d $f && continue
# we have a file to work
xxxxxxxxxx
done

in your directory

regards

/J