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¦
) redirectsfind
command's standard output towc
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
-type f
- no option necessary
-type d
-perm /u+w,g+w
or some variation-perm /u+r,g+r
-perm /u+x,g+x
-size 0
-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
How to Get Notified for Ip Address Changes Automatically
Print Kernel's Page Table Entries
Option to Display Control Characters in Gedit
How to Catch Duplicate Entries in Text File in Linux
Using the Universal Chess Interface
Dma Cache Coherence Management
How to Split a Ca Certificate Bundle into Separate Files
Pause Programmatically Video Player Mpv
Running R Scripts or Commands with Interpretor in Unix for Unix-Layman
How to Read a Sector Using a Bio Request in Linux Kernel
Trying to Find All the Kernel Modules Needed for My MAChine Using Shell Script
Creating a Raw Printer Queue in Cups (Host) and Adding Them Through Cups (Client)
Adding a New System Call in Linux Kernel 3.3