Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of files in a directory with an OSX terminal command

Tags:

linux

shell

macos

I'm looking for a specific directory file count that returns a number. I would type it into the terminal and it can give me the specified directory's file count.

I've already tried echo find "'directory' | wc -l" but that didn't work, any ideas?

like image 429
Milos Avatar asked Oct 13 '17 13:10

Milos


People also ask

How do I count the number of files in a folder on a Mac?

You can see the file count by enabling Status Bar in Finder. To do this, click View -> Show Status Bar on the menu bar, or press Command + /. When you click "Show Status Bar", does a status bar appear at the bottom of Finder? If so, what does it display (if not the item count)?

How do we get the count of number of files in a directory?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

What command is used to count number of folders?

Using the ls Command. The ls command lists the directories and files contained in a directory. The ls command with the -lR options displays the list (in long format) of the sub-directories in the current directory recursively.


1 Answers

You seem to have the right idea. I'd use -type f to find only files:

$ find some_directory -type f | wc -l 

If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:

$ find some_directory -maxdepth 1 -type f | wc -l 
like image 70
Mureinik Avatar answered Sep 21 '22 12:09

Mureinik