LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2004, 09:16 PM   #1
jwprz70
LQ Newbie
 
Registered: Sep 2004
Location: Washington
Posts: 3

Rep: Reputation: 0
help!


How do I write Shell Script that counts the number of files in a directory then prints the number.

Last edited by jwprz70; 09-14-2004 at 09:19 PM.
 
Old 09-14-2004, 10:21 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 52
don't kow how to do a shell script but use this
c is lots faster
name it count_files.c
or whatever you want
compile with
gcc -o count_files count_files.c

then put the executable in usr/bin or wherever
you can also call this from a script of course
count_files will get you the regular files in current working directory
or you can put a path to a directory as an argument after the command

if you want more than regular files counted change it or write back
Code:
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int get_file_type (const char* path)
{
  struct stat st;
  lstat (path, &st);
  if (S_ISREG (st.st_mode))
    return 0;
  else return 1;
 }

int main (int argc, char* argv[])
{
  char* dir_path;
  DIR* dir;
  struct dirent* entry;
  char entry_path[PATH_MAX + 1];
  size_t path_len;
  int type;
  int count = 0;
  if (argc >= 2)
    dir_path = argv[1];
  else 
    dir_path = ".";
  strncpy (entry_path, dir_path, sizeof (entry_path));
  path_len = strlen (dir_path);
  if (entry_path[path_len - 1] != '/') {
    entry_path[path_len] = '/';
    entry_path[path_len + 1] = '\0';
    ++path_len;
  }
  dir = opendir (dir_path);
  while ((entry = readdir (dir)) != NULL) {
    strncpy (entry_path + path_len, entry->d_name,
	     sizeof (entry_path) - path_len);
    type = get_file_type (entry_path);
    if (type == 0) count++;
  }
  printf ("%i\n", count);
  closedir (dir);
  return 0;
}
 
Old 09-14-2004, 10:57 PM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 377Reputation: 377Reputation: 377Reputation: 377
Re: help!

Quote:
Originally posted by jwprz70
How do I write Shell Script that counts the number of files in a directory then prints the number.
here's one way you could do it:

Code:
#!/bin/sh
COUNT=`ls -al | wc -l`
SNIP="3"
NUMBER=$(($COUNT-$SNIP))
echo $NUMBER
this would count all files in the current directory, including the hidden ones...

basically it lists all files in the current directory, then counts the lines that were listed...

the SNIP is there cuz when you do a "ls -al" there's two lines that represent "./" and "../" and one line that represents the total size, so we subtract them...

just my two cents...


Last edited by win32sux; 09-14-2004 at 11:02 PM.
 
Old 09-14-2004, 11:01 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Use -A instead of -a and 1 instead of l
and forget about snip ;)


Cheers,
Tink
 
Old 09-14-2004, 11:07 PM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 377Reputation: 377Reputation: 377Reputation: 377
Quote:
Originally posted by Tinkster
Use -A instead of -a and 1 instead of l
and forget about snip


Cheers,
Tink
yup, that's much better... thanks Tinkster!!!

=)

Code:
ls -A1 | wc -l
 
Old 09-14-2004, 11:43 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
/me takes a bow:"Pleasure, mate!" ;)
 
  


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



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:05 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration