2

I have a command in bashscript that will find the files from a wildcard folder name and count it from that wildcard folder we have a test folder that I want to exclude from count.

Here is my command

echo $(date "+%b %_d")
echo "$today"
sr_today=$(find /mnt/data/project_data/web_collab/mailbox/*/sr_pdf/*.pdf -type f -ls | grep "$today" | wc -l)

In the mailbox folder there are folders named
000000
111111
222222
333333
so on...

I want to exclude 000000 from that find command. Is that possible? Any tips?

Thanks.

3
  • 1
    Can you please describe in more detail what are you trying to do with that find command? Jul 21 '16 at 3:59
  • I'm curious, why do echo $(date "+%b %_d") instead of date "+%b %_d" Jul 21 '16 at 4:00
  • And why do find /mnt/data/project_data/web_collab/mailbox/*/sr_pdf/*.pdf -type f -ls instead of ls /mnt/data/project_data/web_collab/mailbox/*/sr_pdf/*.pdf ? Jul 21 '16 at 4:00
5

Using GNU find:

#!/bin/sh

dir='/mnt/data/project_data/web_collab/mailbox/'

sr_today=$(find "$dir" \
             ! -path '*/000000/*' \
             -newermt '12am today' \
             -ipath '*/sr_pdf/*.pdf' |
           wc -l)
  • GNU find's -newermt option understands the same date formats as GNU date -d and touch -d. See man find and search for -newerXY for details.

  • the final -ipath predicate is case-insensitive, so it will match *.PDF, *.pDf, etc. Unfortunately, it's also case-insensitive when matching the sr_pdf directory. If there are, e.g., SR_PDF subdirectories that you don't want to match, and you're sure that all .pdf file extensions are lowercase, change -ipath to -path.

    Or, for case-sensitive sr_pdf path match and case-insensitive *.pdf file match, replace that -ipath line with:

      -path '*/sr_pdf/*' \
      -iname '*.pdf' | 
1
  • Actually, I like this solution better than mine in some ways. However, -newermt is not portable (BSD doesn't have it.) Jul 21 '16 at 21:06
1

First, if you're going to be using a long path like that, it's best to give it its own variable name. Makes the code easier to read, makes sure you use the same name everywhere without typos, and makes it easier to change if you need.

MAILBOX=/mnt/data/project_data/web_collab/mailbox

Try this if there's less than a few hundred pdf files to contend with:

echo "${MAILBOX}"/*/sr_pdf/*.pdf | grep -v /00000/ | grep "$today" | wc -l

And if you want a more general solution, do this:

find "${MAILBOX}" -path '*/sr_pdf/*.pdf' | grep -v /00000/ | grep "$today" | wc -l
4
  • echo should change with find
    – Baba
    Jul 21 '16 at 4:06
  • How many files are we talking about? If it's less than a couple hundred or so, echo should work just fine. But I'll update my answer two. Jul 21 '16 at 4:09
  • @EdwardFalk Hundreds of folder and hundreds of files
    – Edmhar
    Jul 21 '16 at 4:13
  • But it will not go beyond 300
    – Edmhar
    Jul 21 '16 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.