Results 1 to 6 of 6

Thread: Counting files of a specific extension recursively in a directory.

  1. #1
    Join Date
    Aug 2008
    Beans
    364
    Distro
    Kubuntu

    Counting files of a specific extension recursively in a directory.

    I'm fine tuning the PHP cache on my web server and that involves telling APC approximately how many source files there are to cache.

    Is there a way to recursively count files throughout all of my web root with the .PHP extension and possibly other extensions?
    And the thread title of the year award goes to...
    " Supositories not working - On the verge of losing it here - help!"

  2. #2
    Join Date
    Jul 2005
    Beans
    2,047

    Re: Counting files of a specific extension recursively in a directory.

    ~
    Last edited by ahallubuntu; June 24th, 2013 at 01:02 AM.

  3. #3
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Counting files of a specific extension recursively in a directory.

    The number of files may not correspond to the number of different requests that might be cached.

    For instance, all my sites use only one index.php file. The content displayed depends on additional parameters in the URI. The "welcome" page is represented as /index.php?go=welcome; the "contact us" page is /index.php?go=contact, and so forth. So my one index.php file can generate literally dozens of different pages.

    If you are sure that only one page is generated per file, then you can use

    Code:
    cd /var/www
    ls -lR *.php | wc -l
    to get a count of all files ending in .php in /var/www and its subdirectories.

    Otherwise you can use "wget -r http://www.example.com/" to generate a complete list of all URLs accessible from the home page and count those. The "-r" ("recursive") parameter tells wget to follow all the links it finds.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  4. #4
    Join Date
    Aug 2008
    Beans
    364
    Distro
    Kubuntu

    Re: Counting files of a specific extension recursively in a directory.

    Quote Originally Posted by SeijiSensei View Post
    The number of files may not correspond to the number of different requests that might be cached.

    For instance, all my sites use only one index.php file. The content displayed depends on additional parameters in the URI. The "welcome" page is represented as /index.php?go=welcome; the "contact us" page is /index.php?go=contact, and so forth. So my one index.php file can generate literally dozens of different pages.

    If you are sure that only one page is generated per file, then you can use

    Code:
    cd /var/www
    ls -lR *.php | wc -l
    to get a count of all files ending in .php in /var/www and its subdirectories.

    Otherwise you can use "wget -r http://www.example.com/" to generate a complete list of all URLs accessible from the home page and count those. The "-r" ("recursive") parameter tells wget to follow all the links it finds.
    I don't believe that matters for opcode caching. It's not caching static pages. It's caching PHP scripts. Unless I'm drastically misunderstanding opcode caching and APC.

    That would be pretty pointless for a forum app since literally every single post creates a new URL. It would be impossible to keep up with and my file hints setting would be up into the hundreds of thousands after not long.
    And the thread title of the year award goes to...
    " Supositories not working - On the verge of losing it here - help!"

  5. #5
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Counting files of a specific extension recursively in a directory.

    Since I have never used "opcode caching" in my life, I bow to your greater expertise in this matter.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Counting files of a specific extension recursively in a directory.

    find | wc -l is good in 99.99% cases but if there is even 1 file with newline in its name (which is allowed) the result will be wrong

    rock solid way to do this is to print a single char for every matching item and count how many chars are there
    Code:
    find -printf "x" | wc -c
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Tags for this Thread

Bookmarks

Posting Permissions

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