Saturday, November 20, 2010

Unix count amount of files in ONE directory using bash?

atm my code looks like totalNum=`ls -l . | egrep -c '^-' ./submissions`



when taking off the ./submissions it lists the files correctly in the current directory.. when i add the ./submissions it just gives me 0. any ideas on how to replace the ./submissions with a directory change that WILL work?Unix count amount of files in ONE directory using bash?
egrep is trying to find a ';-'; at the beginning of ';./submissions'; as input string.



You want to have



ls -l ./submissions | egrep -c '^-'



if you want to count the files in the directory ./submissions, then you have to tell ls to look in that directory. You've kinda mis-understood the piping syntax or what egrep does.



The command I have given does the following:



lists entries in long format for the directory ./submissions.

The output from that ls command is piped into egrep which counts the instances of lines which start with a - character.



Your version lists the files in long format for the directory . and pipes that input into egrep which counts instances of - at the start of the string ';./submissions';. In this case, egrep ignores the pipe because egrep -c '^-' ./submissions is a ';full'; command (i.e. it has input specified already). The returned count is zero because ';./submissions'; doesn't start with a - character.



:%26gt;

No comments:

Post a Comment