Find the answer to your Linux question:
Results 1 to 6 of 6
G'day everyone, I'm having trouble figuring out how to list the contents of a directory and exclude any subdirectories. Currently I have: Code: ls --width=1 ${FILEDIR}/ | grep -v '^Thumbs.db' ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Posts
    68

    ls exclude directories


    G'day everyone,

    I'm having trouble figuring out how to list the contents of a directory and exclude any subdirectories.

    Currently I have:

    Code:
     ls --width=1 ${FILEDIR}/ | grep -v '^Thumbs.db' $1 > ${FILES}
    The output is the correct format, any help someone can give me with this would be awesome.

    Cheers,

    Griffo

  2. #2
    Just Joined!
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Posts
    68
    I'm sorry, I meant to take the variables out when I posted to make it a little easier to read. When the script runs, it uses the values below.

    Code:
     ls --width=1 /mnt/ | grep -v '^Thumbs.db' $1 > files.txt

  3. #3
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    161
    Not sure ls can do that (I've never mmanaged to make it do it), but this should work

    Code:
    find /mnt -name "*"  ! -type d -maxdepth 1  -printf "%f\n"
    In a world without walls and fences, who needs Windows and Gates?

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Posts
    68
    Thanks for the reply, it works and it is the correct format to use in my script. But...when I pipe or direct the output to another program or file I receive an error on my display even though the file I'm creating is fine. Using the command you provided, I've added grep to strip Thumbs.db from the list.

    Code:
    find /mnt -name "*"  ! -type d -maxdepth 1  -printf "%f\n" | grep -v '^Thumbs.db' $1 > files.txt
    The error I get is:

    find: warning: you have specified the -maxdepth option after a non-option argument -name , but options are not positional (maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
    I'm not sure if the version of find is an issue here, I'm using Debian Lenny (but will soon be upgrading to Squeeze).

    It would be great if I could get rid of this error message, any ideas you have would be greatly appreciated.

    Cheers,

    Griffo

  5. #5
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    161
    try

    Code:
    find  /mnt -maxdepth  1 -name "*"  ! -type d  -printf "%f\n"
    I get the same error/warning message if the find returns no results, the above seems to fix/(get rid of) it.
    In a world without walls and fences, who needs Windows and Gates?

  6. #6
    Just Joined!
    Join Date
    Jan 2010
    Location
    Sydney, Australia
    Posts
    68
    Perfect, that's exactly what I was after. I piped it to grep and sent the output to a file and no error message.

    Thanks mate,

    Griffo

Posting Permissions

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