Directory – Determine Number of Files Without Counting

directory

Related: What's the best way to count the number of files in a directory?

I have a system with a largish number of files in a directory

 $ ls -god xml
 drwxrwsrwx   7 7070720 Mar 12 11:51 xml

If I try to count specific groups of file using ls xml/*query | wc -l the system usually produces error message

 /bin/ls: arg list too long

I tried find xml -name '*query' | wc -l there was no response after 10 minutes at which time I terminated the command.

$ nohup time find xml -name '*query' -level 0 | wc -l &
[1]     11751

$ ps -f 2>rgb
     UID   PID  PPID  C    STIME     TTY        TIME CMD
     rgb 11751 10637  0 02:45:11  ttyp12    00:00:00 wc -l
     rgb 11752 11751  0 02:45:11  ttyp12    00:00:00 time find xml -name *query -level 0
     rgb 11753 11752 77 02:45:11  ttyp12    00:00:03 find xml -name *query -level 0
     rgb 11776 10637  1 02:45:17  ttyp12    00:00:00 ps -f
     rgb 10583 10581  0 02:30:13  ttyp12    00:00:00 -csh
     rgb 10637 10583  2 02:30:19  ttyp12    00:00:00 ksh

top -Urgb

last pid: 11864;  load averages:  1.21,  0.82,  0.66                   14:48:03
249 processes: 246 sleeping, 2 running, 1 onproc
CPU states:  0.0% idle, 24.5% user, 75.5% system,  0.0% wait,  0.0% sxbrk
Memory: 2048M phys, 1799M max, 1718M free, 1774M locked, 114M unlocked, K swap

  PID USERNAME PRI NICE   SIZE   RES  STATE   TIME  COMMAND
11837 rgb       26    0   804K   804K onpr    0:00  top
11753 rgb       56    4  5512K  5512K run     1:10  find
11751 rgb       51    4   588K   588K sleep   0:00  wc
10583 rgb       48    0  1204K  1204K sleep   0:00  -csh
11752 rgb       48    4   588K   588K sleep   0:00  time
10637 rgb       48    0  1288K  1288K sleep   0:00  ksh

last pid: 12330;  load averages:  1.82,  1.45,  1.05                   14:58:06
258 processes: 253 sleeping, 4 running, 1 onproc
CPU states:  0.0% idle, 20.7% user, 78.7% system,  0.6% wait,  0.0% sxbrk
Memory: 2048M phys, 1799M max, 1711M free, 1774M locked, 106M unlocked, K swap

  PID USERNAME PRI NICE   SIZE   RES  STATE   TIME  COMMAND
11837 rgb       26    0   804K   804K onpr    0:00  top
11753 rgb       -1    4  5512K  5512K run     5:10  find
11751 rgb       51    4   588K   588K sleep   0:00  wc
10583 rgb       48    0  1204K  1204K sleep   0:00  -csh
11752 rgb       48    4   588K   588K sleep   0:00  time
10637 rgb       48    0  1288K  1288K sleep   0:00  ksh

$ jobs
[1] +  Running                 nohup time find xml -name '*query' -level 0 | wc -l &

$ kill %1
[1] + Terminated               nohup time find xml -name '*query' -level 0 | wc -l &

Can I instead the number of files, within say 10%, from the 7070720 size of the directory given by ls -god xml?

Supplementary Q: To what extent does this depend on the filesystem (UFS, V7FS, HTFS etc etc)?


Update:

The command ls xml | wc -l did return a value in a few seconds. I should have tried this before posting the question. This provides the information I was asking for, so there's no point working out how many filename+inode entries fit in a 7070720 byte directory (answer: at least 260085).

Best Answer

ls wastes resources by sorting the output. If you have GNU ls, do this instead:

ls --quoting-style=escape -U xml | wc -l