Tags
agile agile testing Android Apple bash BCS BCS South West browser BT Home Hub 2.0 development DNS email event Firefox geolocation HTC Hero learning linux location aware browsing mobile non-technical privacy Problem & Solution professional review scripting server terminal testing tips Wireless NetworkingTwitter
- Using Bash to Change the Delimiter in a CSV File: Â The Problem I recently had a situation where I had a comma s... http://t.co/WaoORVD5 2012/01/23
- @James_Hellyer Not something I can Ebay unfortunately, shame, as I'm sure there'd be a huge demand for black market liposuction ;) 2011/11/30
- Anyone want a brown Ben Sherman leather jacket? 'cus I have a jacket like that on Ebay, you should all go bid on it ;) http://t.co/X0CSVRa4 2011/11/30
- Hello Twitter... its been a long while... 2011/11/15
- Job opportunity: Graduate Developers - Java, Tcl, J2EE, C at OpenBet Ltd - London, United Kingdom #jobs http://t.co/3ptpTOBE 2011/11/15
Counting All Files in a Linux Directory
To count how many files there are in a directory using the terminal on a Linux machine you can combine 2 commands:
We’ll use the find command to locate all the files (and exclude directories and other non-files) and then the wc command to count the files.
Find has many options, but we will only be using the type option:
find . -type fThe . indicates the directory to search in and can be replaced with any absolute or relative path. This command will also find files in subdirectories. To exclude subdirectories you can use:
find . ! -name . -prune -type fThe
! -name . -prunewill ignore any directories that are not the current one.The find command will give us a list of all the files we want to count, to count the number of entries in that list we must pass the output to wc to count it.
By default, wc will count words, newlines, and bytes. Since the output from find is a list of files, each separated by a newline, we can tell wc to only count newlines by using the -l argument:
wc -lCombining this into a single command, we get the final command to find count the number of files in a directory excluding sub-directories:
find . ! -name . -prune -type f | wc -l(Of course, if you want to include sub-directories, simply remove the
! -name . -prunearguments and use:find . -type f | wc -linstead)