LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 01-17-2012, 07:49 AM   #1
marioscs
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
Count all files without using find


Hi how can i count all files in / without using find only with grep.I tried using ls recursively but the output is confusing on how i will be able to pipe it through grep.Thanks a lot...
 
Old 01-17-2012, 08:23 AM   #2
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
Haven't tested its accuracy (it could also be utterly wrong)...

this lists only 'regular' files (no directories, character/block devices, links etc.)

Code:
ls -Ral / | grep -c ^-
this just excludes directories

Code:
ls -Ral / | grep -cv ^d
to exclude various specific stuff, for example links and directories:

Code:
ls -Ral / | grep -cv ^[dl]

Last edited by kabamaru; 01-17-2012 at 11:07 AM.
 
1 members found this post helpful.
Old 01-17-2012, 08:26 AM   #3
jay4linux
LQ Newbie
 
Registered: Jan 2012
Location: Spartanburg, SC
Distribution: Slackware, Kubuntu, Debian
Posts: 4

Rep: Reputation: Disabled
You could try using
Code:
ls -1AR /* | wc -l
That is one in arguements. You might have to adjust the arguements to ls a little, look at the man page for more information. The wc -l command counts the number of lines produced by the ls command. So if you list all files one per line and pipe it to wc, you should get the number of files.
 
1 members found this post helpful.
Old 01-17-2012, 11:04 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
This might be a useful solution:

Code:
tree / | tail -n 1
Then extract the desired numbers from the output.

The big question though is why you would want to or need to do this?
 
1 members found this post helpful.
Old 01-17-2012, 11:07 AM   #5
marioscs
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you all for the help
David i have an exercise and i guess because our teacher want us to learn use grep.
Anyway what do you think of this?
Code:
#!/bin/bash
echo USERS NAME $(whoami) #onoma xristi
echo Total number of files: $(ls -Ral / /dev/null 2>&1 | grep  ^- |wc -l)   
echo Total number of excecutable files: $(ls -Ral / /dev/null 2>&1 | grep  ^-rwx |wc -l) 
echo Total number of non excecutable files: $(ls -Ral / /dev/null 2>&1 | grep  ^-rw  |wc -l) 
echo Total number of Directories: $(ls -Ral / /dev/null 2>&1 | grep  ^d.*[^.]$  |wc -l)
 
Old 01-17-2012, 11:08 AM   #6
celthunder
Member
 
Registered: Feb 2009
Location: Newton, WI
Distribution: Arch
Posts: 84

Rep: Reputation: 8
du -a|wc -l

not sure if that'd work or not same concept as the tree and ls solutions though.
 
Old 01-17-2012, 12:44 PM   #7
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
If it is an exercise about grep, why not take advantage of grep's own options?

-c counts matching lines (and suppresses normal output), and combined with -v counts non-matching lines.

what do you need the /dev/null part for?


btw. Καλή επιτυχία φίλε

Last edited by kabamaru; 01-17-2012 at 12:48 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to find newest files and count them buee Programming 1 02-25-2010 07:43 PM
Count files in dir then move that many files in a script? chuckycharms Linux - Newbie 3 12-18-2009 12:31 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
To find out the logs count gsiva Programming 1 07-02-2009 07:40 AM
how to find PDF page count mfoley Programming 8 02-26-2009 03:55 PM

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

All times are GMT -5. The time now is 12:00 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