Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Posts
    5

    Unanswered: 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

  2. #2
    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.

    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

  3. #3
    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

    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.

  4. #4
    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
    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

  5. #5
    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

  6. #6
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •