The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unix command/s to find files older than 2 hours in a directory Presanna Shell Programming and Scripting 6 11-20-2007 09:22 AM
How to delete Directory and inside files using Find command bmkreddy SUN Solaris 3 07-10-2007 02:35 PM
How to find the count of files in a directory sish78 UNIX for Dummies Questions & Answers 5 02-01-2007 03:58 PM
Help Using Grep command to Find the string in the files in the root directory Subbu_Angeline AIX 3 01-17-2007 01:45 PM
Find occurances of a directory and count stringman Shell Programming and Scripting 2 08-18-2005 03:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-18-2007
sish78 sish78 is offline
Registered User
  
 

Join Date: May 2006
Posts: 16
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 (permalink)  
Old 07-18-2007
jjinno jjinno is offline
Registered User
  
 

Join Date: Jul 2007
Location: Cloud 9
Posts: 72
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 (permalink)  
Old 07-19-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
ls -1  |awk 'END{print NR}'
  #4 (permalink)  
Old 07-19-2007
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
ls -l |grep '^-' | wc-l
  #5 (permalink)  
Old 07-19-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by aajan View Post
ls -l |grep '^-' | wc-l
OP had asked a solution with awk

moreover the above could be modified as just
Code:
ls -1 | wc -l
  #6 (permalink)  
Old 07-19-2007
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
By doing so, it wil count the directories also my dear pal
  #7 (permalink)  
Old 07-19-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,502
Quote:
Originally Posted by sish78 View Post
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}'
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0