LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-20-2004, 05:37 AM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
finding number of files in a directory


how can i find the number of files in a directory and print the first line of each file
i am using C programming in linux
 
Old 08-20-2004, 05:49 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Look in linux there are man pages that give detailed discription of all command of linux and linux programming.

To get the number of files you have to read the directory. Go to console and write.

man scandir
This will show you how to read the directory.

Also check "man readdir". After reading directory you will get the list of files in a structure that you will pass to the function. Its **namelist and you can see the name of the file from namelist->d_name now you can open each file using open command and then get a line and print it.
 
Old 08-20-2004, 10:17 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 53
Code:
#!/bin/sh

echo "There are `ls | wc -l` files in the directory."

for i in * ; do
  head -n1 $i
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of files in directory bullium Linux - General 13 08-31-2011 08:50 AM
Maximum number of files in a directory neranjana General 14 05-13-2010 01:33 PM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 03:01 AM
Finding number of CPUs. r_213 Linux - Networking 2 10-07-2004 11:48 PM
finding the number of files in each directory ridertech Programming 1 06-02-2004 10:26 PM


All times are GMT -5. The time now is 04:31 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration