awk command to find the count of files ina directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to find the count of files ina directory
# 1  
Old 07-18-2007
awk command to find the count of files ina directory

hi Gurus,

can anyone provide a awk command to get teh count of number of file sin a specific directory.

appreciate any kind of information..

thanks
# 2  
Old 07-18-2007
This works but only if there actually are files in the directory... if there are only directories, or no files at all, it responds with an error and a '-1', because the 'ls -l' fails

Code:
ls -l *.* | wc | awk -F ' ' '{print $1-1}'

# 3  
Old 07-19-2007
Code:
ls -1  |awk 'END{print NR}'

# 4  
Old 07-19-2007
ls -l |grep '^-' | wc-l
# 5  
Old 07-19-2007
Quote:
Originally Posted by aajan
ls -l |grep '^-' | wc-l
OP had asked a solution with awk Smilie

moreover the above could be modified as just
Code:
ls -1 | wc -l

# 6  
Old 07-19-2007
By doing so, it wil count the directories also my dear pal
# 7  
Old 07-19-2007
Quote:
Originally Posted by sish78
hi Gurus,

can anyone provide a awk command to get teh count of number of file sin a specific directory.

appreciate any kind of information..

thanks
awk script for a specific directory (ie. a directory you specify) :
Code:
awk 'BEGIN {
	while ( ( "ls -l /yourdir" | getline line ) >0 ) {
		if ( line ~ /^-/ ){
			c++
		}
	}
	print "Total files: ", c
     }
'

or simply (for a specific directory):
Code:
ls -ltr | awk '/^-/{ c++;}END{print c}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Listing only the files under a directory from the result of find command

Hi, I have a main folder 'home'. Lets say there is a folder 'bin' under 'home'. I want to check the list of files under subdirectories present under the /bin directory created in the last 24 hours. I am using the following find command under home/bin directory: find . -mtime -1 -print ... (3 Replies)
Discussion started by: DJose
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] How to find particular files ina directory?

Hi, I am trying to write a script to find some files in a directory Example: if i have files like 2014-02-01_aaaa.txt 2014-02-01_bbbb.txt 2014-02-01_cccc.txt 2014-02-01_dddd.txt and some other files how can i just check to see if there four files exits or not i tried some thing like this... (5 Replies)
Discussion started by: vikatakavi
5 Replies

3. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

5. UNIX for Dummies Questions & Answers

command to find cksum of all files inside a directory?

I did this: ls -lrRt | grep ^* | cksum * but it is showing cksum of sub-directories. Thanks You Please use code tags when posting data and code samples, thank you. (3 Replies)
Discussion started by: ezee
3 Replies

6. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

7. Shell Programming and Scripting

print as well as count the files found by find command

I want the output of the find command to be printed and also the total files found by it. Can someone help in this. Obviously $ find . -type f | wc -l will not output the files found but only the count. I want both. There can be millions and trillions of files so dont want the output of find... (3 Replies)
Discussion started by: amicon007
3 Replies

8. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies

9. UNIX for Dummies Questions & Answers

How to find the count of files in a directory

Hi Gurus WHat would be the command to check whether there is a file in particular path or not.. for ex: my file name is ExRate_20071501.csv I can have many files with same pattern but diffrentiated by date.. i have a process where i have to check if files exist in tht folder i have to... (5 Replies)
Discussion started by: sish78
5 Replies

10. AIX

Help Using Grep command to Find the string in the files in the root directory

I want to serch for a string in all the files in the root directory. i want the search to be limited to only the files in the directory i.e the search to be done only in the files not in the sub directory. the approaches tried are 1)grep "pattern string" this command was serching the... (3 Replies)
Discussion started by: Subbu_Angeline
3 Replies
Login or Register to Ask a Question