Find the answer to your Linux question:
Results 1 to 10 of 10
I am using the find command and I am trying to to exclude any files that start with the . in front of them such as .bash_history...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    47

    How do you exclude files in the find command


    I am using the find command and I am trying to to exclude any files that start with the . in front of them such as .bash_history

  2. #2
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    I think there is not a way to explicitly exclude that files, but you can use the -regex or -iregex flag to match only the files that not begin with '.'

    Regards

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    -regex matches the entire path, so -name and -iname are probably better, since they only match the actual filename.

  4. $spacer_open
    $spacer_close
  5. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Here's one way:
    Code:
    #!/bin/sh
    
    # @(#) s3       Demonstrate removal of display of dot-names.
    
    FILES="t1 .t1 t2 .t2"
    rm -f $FILES
    touch $FILES
    echo " Files present:"
    ls -al
    
    echo
    echo " Files listed:"
    find . -type f -print |
    grep -v '/[.]
    Which produces:
    Code:
    % ./s3
     Files present:
    total 12
    drwxr-xr-x  2 drl drl 216 Jul 25 14:31 .
    drwxr-xr-x  4 drl drl 104 Jul 25 14:06 ..
    -rw-r--r--  1 drl drl   0 Jul 25 14:31 .t1
    -rw-r--r--  1 drl drl   0 Jul 25 14:31 .t2
    -rwxr--r--  1 drl drl  79 Jul 25 14:09 s1
    -rwxr--r--  1 drl drl 131 Jul 25 14:13 s2
    -rwxr--r--  1 drl drl 212 Jul 25 14:30 s3
    -rw-r--r--  1 drl drl   0 Jul 25 14:31 t1
    -rw-r--r--  1 drl drl   0 Jul 25 14:31 t2
    
     Files listed:
    ./s1
    ./s2
    ./s3
    ./t1
    ./t2
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    One thing to look out for is that drl's solution will also skip files in hidden directories. So if there's a non-hidden file in a hidden directory, it won't display.

    That may be what you want, but you should just beware of that.

  7. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Good catch, Cabhan!

    We'd need to find if chrchcol wanted to ignore everything hidden. I'll fool around with the RE. I't's probably necessary to make sure that the / is a final separator (ihmmm, ignoring perhaps the pathological case of a dot-name empty directory) ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Here's a revision and test ... cheers, drl
    Code:
    #!/bin/sh
    
    # @(#) s4       Demonstrate removal of display of dot-names.
    
    FILES="t1 .t1 t2 .t2"
    rm -rf $FILES .hidden
    touch $FILES
    mkdir .hidden
    touch .hidden/t1 .hidden/.t1
    echo " Files present:"
    ls -AR
    
    echo
    echo " Files listed:"
    find . -type f -print |
    grep -v '/[.][^/]*$'
    Results:
    Code:
    % ./s4
     Files present:
    .:
    .hidden  .t1  .t2  s4  t1  t2
    
    ./.hidden:
    .t1  t1
    
     Files listed:
    ./s4
    ./t1
    ./t2
    ./.hidden/t1
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #8
    Just Joined!
    Join Date
    Sep 2005
    Location
    New delhi
    Posts
    22
    Quote Originally Posted by chrchcol
    I am using the find command and I am trying to to exclude any files that start with the . in front of them such as .bash_history

    find /root/ -not -name ".*" -name *.conf


    Simple.

  10. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi, amitsharma_26.
    Quote Originally Posted by amitsharma_26
    Code:
    find /root/ -not -name ".*" -name *.conf -name *.conf
    Thanks for posting this solution. The first part of this, namely:
    Code:
    find /root/ -not -name ".*"
    does seem to work (adjusting "/root/" appropriately), and I like the simplicity, but the addition of
    Code:
    -name *.conf
    seemed to inhibit the display of everything. What is it supposed to do? ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  11. #10
    Just Joined!
    Join Date
    Sep 2005
    Location
    New delhi
    Posts
    22
    Dear drl,

    Initial query is itself a bit confusing in the same regards. Though we can relate a same scenario like..

    How about searchin for all the mp3 files with excluding all those who are starting by alphabet "A" ?

    Or Even how about searchin for all the mp3 files with excluding all those who are smaller than 3 MB of size ?

    With both of the two of above queries we can use the same method of excluding particular criteria.

    find /mp3 -not -iname "A*" -iname "*.mp3"
    & find /mp3 -not -size -3M -iname "*.mp3"

    Though the later one can also be written as find /mp3 -size +3M -iname "*.mp3" , but it was just to correlate the way of excluding some particular criteria suited files.

    Regards
    Amit sharma..

    p.s. My way of searchin *.conf did not really meant for that command to look really effective though later part of that command really didnt what i was concentrated.

Posting Permissions

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