View Full Version : how to count files in bash ?
detlef
01-18-2004, 06:06 PM
Hi,
I would like to count the number of files in a folder using a small shell script. In AppleScript the code would look like this:
tell application "System Events"
set x to (number of files in folder "/Users/schmittdetlef/.Trash")
end tell
Can somebody please tell me how the equivalent command would look like in bash.
Thanks,
Detlef
gatorparrots
01-18-2004, 06:47 PM
ls -1 | wc -l
This uses wc to count of the number of lines (-l) in the output of ls -1. (It doesn't count dotfiles.)
If you want to count only actual files and disinclude symbolic links, use ls -l | grep -v ^l | wc -l (an "L", not a "1" this time, in order to obtain a "long" listing). grep discards any line beginning with "l" (indicating a link).
detlef
01-19-2004, 06:28 AM
thanks for your help. That part now I got done.
This line works for me:
ls ~/.Trash | wc -l
one more question. After that I should be able finish the script by myself.
How do I get the output of the command into a variable?
Detlef
Originally posted by gatorparrots
ls -1 | wc -l
This uses wc to count of the number of lines (-l) in the output of ls -1. (It doesn't count dotfiles.)
If you want to count only actual files and disinclude symbolic links, use ls -l | grep -v ^l | wc -l (an "L", not a "1" this time, in order to obtain a "long" listing). grep discards any line beginning with "l" (indicating a link).
SOMEVAR=`date +"%Y%m%d"`
ANOTHERVAR=`basename $0`
detlef
01-19-2004, 08:43 PM
naum,
I don't understand your answer. You have to be a little more simple with me. I am very new to unix and shell sripting.
In my previous questions I wanted to know how to count the files in my trash folder. The following command works for me.
ls ~/.Trash | wc -l
The second question I have is how to get the standart output of wc into a variable (e.g. $trashfiles).
Detlef
mervTormel
01-19-2004, 08:52 PM
one way:
trashfiles=$(ls ~/.Trash | wc -l)
echo $trashfiles
2
you probably could benefit from perusing the bash man pages, or consult some bash tutorial web pages.
detlef
01-19-2004, 10:06 PM
mervTormel,
thanks a lot.
You are right, I should take the time and teach myself the basics about unixs and shell scripting. Actually I do have a book about bash. I used it yesterday to figure out how to assign the wc command to a variable. If I look at your solution I got close but not close enough. Just looking here and there to solve a specific problem is not the way to go, I know. I have to learn the basics.
The man pages are also helpfull although sometimes not completely understandable for a newcomer.
Detlef
mervTormel
01-19-2004, 10:10 PM
re: man pages, yeah, they can be undecipherable at first. so, learn how to use your pager (more, less) to search for tasty nuggets. there's a hump, and if you keep diggin', you'll run smack into it, then over.
Originally posted by naum
SOMEVAR=`date +"%Y%m%d"`
ANOTHERVAR=`basename $0`
Backquote substitution - the output of any command enclosed in backquotes (`...`) gets placed into the shell variable - in my examples, $SOMEVAR would contain 20040119 (or whatever the current date is) and $ANOTHERVAR would be set to base file name (directory path stripeed off) of the executing script.
Here's an example that counts number of active processes that have "8000" in them and stores it to a variable:
PSTALLY=`ps -ef | grep 8000 | grep -v grep | wc -l`
if [ $PSTALLY -gt 0 ] ; then
echo I found $PSTALLY entries
fi
detlef
01-19-2004, 10:36 PM
naum,
ahhh, now I get it. Thanks.
Detlef
gatorparrots
01-21-2004, 03:28 AM
Command substitution
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:
$(command)
or like using backticks:
`command`
Bash performs the expansion by executing COMMAND and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
When the old-style backquoted form of substitution is used, backslash retains its literal meaning except when followed by "$", "`", or "\". The first backticks not preceded by a backslash terminates the command substitution. When using the "$(COMMAND)" form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted form, escape the inner backticks with backslashes.
If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.