If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Search for files only

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-03, 16:18
RTAM RTAM is offline
Registered User
 
Join Date: Mar 2003
Posts: 5
Search for files only

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
Reply With Quote
  #2 (permalink)  
Old 04-11-03, 03:18
stevetucknott stevetucknott is offline
Registered User
 
Join Date: Jan 2003
Posts: 19
Re: Search for files only

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.

Quote:
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
Reply With Quote
  #3 (permalink)  
Old 04-11-03, 12:52
RTAM RTAM is offline
Registered User
 
Join Date: Mar 2003
Posts: 5
Re: Search for files only

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

Quote:
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.
Reply With Quote
  #4 (permalink)  
Old 04-12-03, 05:16
stevetucknott stevetucknott is offline
Registered User
 
Join Date: Jan 2003
Posts: 19
Re: Search for files only

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
Quote:
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
Reply With Quote
  #5 (permalink)  
Old 05-08-03, 11:24
jwall jwall is offline
Registered User
 
Join Date: May 2003
Posts: 2
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
Reply With Quote
  #6 (permalink)  
Old 06-24-03, 08:47
juanep juanep is offline
Registered User
 
Join Date: Jun 2003
Posts: 3
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On