Count Files and Directories Using Shell Script

Count files and directories using shell script

You're not iterating over the list of files inside the given directory; add /* after $LOCATION. Your script should look like:

...
for item in $LOCATION/*
do
...

As pointed by dogbane, just adding /* will count only files that does not begin with .; for doing so, you shall do the following:

...
for item in $LOCATION/* $LOCATION/.*
do
...

How to count number of files in each directory?

Assuming you have GNU find, let it find the directories and let bash do the rest:

find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done

Recursively counting files in a Linux directory

This should work:

find DIR_NAME -type f | wc -l

Explanation:

  • -type f to include only files.
  • | (and not ¦) redirects find command's standard output to wc command's standard input.
  • wc (short for word count) counts newlines, words and bytes on its input (docs).
  • -l to count just newlines.

Notes:

  • Replace DIR_NAME with . to execute the command in the current folder.
  • You can also remove the -type f to include directories (and symlinks) in the count.
  • It's possible this command will overcount if filenames can contain newline characters.

Explanation of why your example does not work:

In the command you showed, you do not use the "Pipe" (|) to kind-of connect two commands, but the broken bar (¦) which the shell does not recognize as a command or something similar. That's why you get that error message.

counting files in directory linux

All your questions can be solved by looking into man find

  1. -type f
  2. no option necessary
  3. -type d
  4. -perm /u+w,g+w or some variation
  5. -perm /u+r,g+r
  6. -perm /u+x,g+x
  7. -size 0
  8. -name '.*'

How to get the number of files in a folder as a variable?

The quotes are causing the error messages.

To get a count of files in the directory:

shopt -s nullglob
numfiles=(*)
numfiles=${#numfiles[@]}

which creates an array and then replaces it with the count of its elements. This will include files and directories, but not dotfiles or . or .. or other dotted directories.

Use nullglob so an empty directory gives a count of 0 instead of 1.

You can instead use find -type f or you can count the directories and subtract:

# continuing from above
numdirs=(*/)
numdirs=${#numdirs[@]}
(( numfiles -= numdirs ))

Also see "How can I find the latest (newest, earliest, oldest) file in a directory?"

You can have as many spaces as you want inside an execution block. They often aid in readability. The only downside is that they make the file a little larger and may slow initial parsing (only) slightly. There are a few places that must have spaces (e.g. around [, [[, ], ]] and = in comparisons) and a few that must not (e.g. around = in an assignment.

How can I list (and find the number of files) of directories with a bash script?

Here's a not particularly clever way of doing it (that does effectively what you're does but recursively AND doesn't solve that the file names don't mean they are JPG) -

( find . -type d -print | while read line; do echo "$line" $( ls -1 "$line"/*.jpg 2>/dev/null | wc -l); done ) | grep -v ' 0$'

Something quite similar to your request has been answered in details at unix & linux SO



Related Topics



Leave a reply



Submit