0

Below is the current days file. Previous days file converted to .gz by system. I wanted to find the total count of last days specific .gz files. I tried the below command which gives me the error. Please suggest

bash-3.2$ ls -lrth|tail 

299K Mar 23  2017 N08170323091903766
333K Mar 23  2017 N08170323091903771
328K Mar 23  2017 N09170323091903776
367K Mar 23  2017 N09170323091903782
347K Mar 23  2017 N04170323092003784
368K Mar 23  2017 N08170323092003783

***bash-3.2$ ls -lrth N08170322*|wc -l***

bash: /usr/bin/ls: Arg list too long

          0

***bash-3.2$ zcat N08170322*.gz|wc -l***

bash: /usr/bin/zcat: Arg list too long

           0
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • The two answers here can be considered bad practice. The right way to do it would be `find . -name '*N08170322*' -printf . | wc -c` – hek2mgl Mar 23 '17 at 05:54

2 Answers2

0

This is happening because you have too many files in the directory.

You can easily get around the first issue:

ls | grep -c N08170322

or, to be even more precise:

ls | grep -c '^N08170322'

would give you the list of files. However, a better way to do this is:

find . -name "N08170322*" -exec ls {} + | wc -l

which will address the ls parsing issue mentioned in @hek2mgl's comment.

If you really want to count the lines of all the zipped files in one shot, you can do this:

find . -name "N08170322*" -exec zcat {} + | wc -l

See also:

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    Please read about [parsing the output of `ls`](http://mywiki.wooledge.org/ParsingLs). Furthermore I don't see the usefulness of the while loop in the second example. It should be `find . ... -exec zcat {} + | wc -l` – hek2mgl Mar 23 '17 at 06:02
  • I am aware of the pitfalls of parsing `ls` output, thanks to SO. This was a simple case of counting and hence used `ls`. – codeforester Mar 23 '17 at 06:05
  • 1
    Looks like you are *not* aware of the pitfalls. When there are newlines in the filenames the count would be wrong. If you think it is safe to do so in an interactive operation, ok, do it (I would not!). But it should not being used in an answer on SO. – hek2mgl Mar 23 '17 at 06:07
-1

Use this

find . -name "N08170322*" -exec ls {} \; |wc -l

As explained in the below answer, you are getting argument list too long as there are multiple files in the directory. To overcome it, you can club it with find and exec

Edit: Created use case to check if command works with/without ls

These are the 3 empty files I created.

$ find -name "file*" -exec ls {} \;
./file1
./file2
./file3

Running wc -l without ls, prints number of lines in each file.

$ find -name "file*" -exec wc -l {} \;
0 ./file1
0 ./file2
0 ./file3

Running it with ls gives me number count of number of files, which is what OP wants.

$ find -name "file*" -exec ls {} \; | wc -l
3
Utsav
  • 7,914
  • 2
  • 17
  • 38
  • There is no absolute "above" and "below" on Stack Overflow; answers are displayed in different order depending on the user's preferences and the metainformation in each post. – tripleee Mar 23 '17 at 05:08
  • Agreed......... – Utsav Mar 23 '17 at 05:20
  • I don't see the usefulness of `ls` here. – hek2mgl Mar 23 '17 at 05:55
  • @hek2mgl : Without `ls`, if I directly use `find . -name "VAL2*" -exec wc -l {} \; `, it will give the count of number of lines in each file. That is not what OP wants. He wants number of files on a particular day. Can you suggest any other way to achieve the same result without `ls`? – Utsav Mar 23 '17 at 08:45
  • `find` will print filenames by default – hek2mgl Mar 23 '17 at 09:00
  • Yes but how will you get the count of number of files without `ls` and `wc -1`? – Utsav Mar 23 '17 at 09:02
  • @hek2mgl = I added example of what I am trying to say. How can I get count as 3 in my sample without `ls`? – Utsav Mar 23 '17 at 09:12
  • `find . -name "VAL2*" | wc -l` ? – hek2mgl Mar 23 '17 at 09:24
  • Then where would we put `exec` ? OP gave error `Arg list too long` – Utsav Mar 23 '17 at 09:40