What is the Linux command to display how many file names in the directory end in two digits?
ls with a command like this :
ls -l | wc –l *[0-9][0-9]
In Bash, the following works non-recursively. You can of course put it in a function:
That works even if you have crazy file names. If your file names are not crazy, then you can use that
Note that the flags I used to
Both of those will include the directory named
Note the use of
Inside one of my folders, I created 3 files that ends with double digits.
First, I issue ls to see all the files.
Now, I go one folder up to use the tree command.
After that, I issue the
As you can see the final line lists the total files that matches our criteria. The -P flag of
As derobert pointed out, if there are directories which ends with 2 numbers and which in turn contains multiple files that ends with 2 numbers the tree command would produce all the files and result in a more noisy output. We can prevent this by tweaking our original command to something like below.
Notice the flag
If you don't mind Perl:
Note that this (as all other solutions presented so far), assumes that you want to count files whose names end in two or more digits. The file
If that's not what you want, the following Perl will print a count of all files (as opposed to files and directories) whose name ends in exactly two digits:
Here is a portable way to do the
And the shell function:
To recursively and portably count files with
To search only the current directory do this instead: