- How to count the number of folders in linux?
- How to count the number of folders in linux?
- Count files and directories using shell script
- Counting number of directories in a specific directory
- Shell Script to Display Number of Files and Directories in a Directory
- Counting Files and Folders in a Directory
- Counting Files and Folders on Linux
- Counting Files and Folders on Windows
- Using File Explorer
- Using cmd.exe
- Using PowerShell
- References
How to count the number of folders in linux?
Solution 1: is also printing the directory itself: You can instead test the directory’s children and do not descend into them at all: You can also use : Solution 2: Get a count of only the directories in the current directory you will get out put like represents no. of directories. As an example: In order to only find the folders you could use something like in a fashion like this: Question: I’m learning bash scripting and have written a script to count the files and directories in the directory that is supplied as argument.
How to count the number of folders in linux?
How to count the number of directories on the desktop? I get only a list of folders, but I need only the number of folders and I absolutely don’t understand how to do it! 🙁
P.S. NOT files, ONLY folders
ls -d ./* should give you a list of directories and wc Count it
Use wc to count the lines of output after getting the list of folders would be one option. Assuming your operation outputs one line per folder. As an example: cat myfile.txt | wc -l
In order to only find the folders you could use something like find in a fashion like this: find . -type d
Shell — How to find number of Subdirectories under a, Use find to count all directories in a tree starting from current directory:. find . -mindepth 1 -type d | wc -l Note, that -mindepth is required to exclude current directory from the count.. You can also limit depth of search with -maxdepth option like this:. find . -mindepth 1 -maxdepth 1 -type d | wc -l More find options are …
Count files and directories using shell script
I’m learning bash scripting and have written a script to count the files and directories in the directory that is supplied as argument. I have it working one way which seems odd to me and am wondering if there is a simpler way of doing it.
I have commented out the code that will work, but left it in as a comparison. I am trying to get the for -loop working, instead using if statements inside it to detect if an item in the given location is a file or a directory.
Edit : I just found out that the commented code counts all files and directories in the subdirectories of the given location as well! Is there any way to prevent this and just count the files and directories of the given location?
#!/bin/bash LOCATION=$1 FILECOUNT=0 DIRCOUNT=0 if [ "$#" -lt "1" ] then echo "Usage: ./test2.sh " exit 0 fi #DIRS=$(find $LOCATION -type d) #FILES=$(find $LOCATION -type f) #for d in $DIRS #do # DIRCOUNT=$[$DIRCOUNT+1] #done #for f in $FILES #do # FILECOUNT=$[$FILECOUNT+1] #done for item in $LOCATION do if [ -f "$item" ] then FILECOUNT=$[$FILECOUNT+1] elif [ -d "$item" ] then DIRCOUNT=$[$DIRCOUNT+1] fi done echo "File count: " $FILECOUNT echo "Directory count: " $DIRCOUNT
For some reason the output of the for -loop, no matter where I point the location to, always returns:
File count: 0 , Directory count: 1
Use find as shown below. This solution will count filenames with spaces, newlines and dotfiles correctly.
FILECOUNT="$(find . -type f -maxdepth 1 -printf x | wc -c)" DIRCOUNT="$(find . -type d -maxdepth 1 -printf x | wc -c)"
Note that the DIRCOUNT includes the current directory ( . ). If you do not want this, subtract 1.
((DIRCOUNT--)) # to exclude the current directory
To just solve the problem you can use:
FILECOUNT=$(find $LOCATION -type f | wc -l) DIRCOUNT=$(find $LOCATION -type d | wc -l)
find will look for all files ( -type f ) or directories ( -type d ) recursively under $LOCATION ; wc -l will count the number of lines written to stdout in each case.
However if you want to learn, the bash script may be a better way. Some comments:
- If you want to look for files/directories in $LOCATION only (not recursively under their subdirectories etc), you can use for item in $LOCATION/* , where the * will expand to the list of files/directories in the $LOCATION directory. The missing * is why your original script returns 0/1 (becasue the $LOCATION directory itself is the only item counted).
- You may want to check first that $LOCATION is actually a directory with [ -d $LOCATION ] .
- For arithmetic expressions, use $(( . )) , for example FILECOUNT=$(( FILECOUNT + 1 )) .
- If you want to find all files/directories recursively, you could combine find with a loop.
find $LOCATION | while read item; do # use $item here. done
You’re not iterating over the list of files inside the given directory; add /* after $LOCATION . Your script should look like:
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 .
. am wondering if there is a simpler way of doing it.
Alternatively, reduce your script to
find /path/to/directory | wc -l
Bash — Shell Script to Display Number of Files and, $ ./t.sh Number of files: 6 Number of directories: 1 $ ./t.sh /tmp Number of files: 9 Number of directories: 3 You might want to check man test to tweak with regards to links to obtain your desired result.
Counting number of directories in a specific directory
How to count the number of folders in a specific directory. I am using the following command, but it always provides an extra one.
find /directory/ -maxdepth 1 -type d -print| wc -l
For example, if I have 3 folders, this command provides 4. If it contains 5 folders, the command provides 6. Why is that?
find is also printing the directory itself:
$ find .vim/ -maxdepth 1 -type d .vim/ .vim/indent .vim/colors .vim/doc .vim/after .vim/autoload .vim/compiler .vim/plugin .vim/syntax .vim/ftplugin .vim/bundle .vim/ftdetect
You can instead test the directory’s children and do not descend into them at all:
$ find .vim/* -maxdepth 0 -type d .vim/after .vim/autoload .vim/bundle .vim/colors .vim/compiler .vim/doc .vim/ftdetect .vim/ftplugin .vim/indent .vim/plugin .vim/syntax $ find .vim/* -maxdepth 0 -type d | wc -l 11 $ find .vim/ -maxdepth 1 -type d | wc -l 12
$ ls -l .vim | grep -c ^d 11 $ ls -l .vim total 52 drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc -rw-rw-r-- 1 anossovp anossovp 48 Aug 29 2012 filetype.vim drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin -rw-rw-r-- 1 anossovp anossovp 2505 Aug 29 2012 README.rst drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax $ ls -l .vim | grep ^d drwxrwxr-x 3 anossovp anossovp 4096 Aug 29 2012 after drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 autoload drwxrwxr-x 13 anossovp anossovp 4096 Aug 29 2012 bundle drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 colors drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 compiler drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 doc drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftdetect drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 ftplugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 indent drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 plugin drwxrwxr-x 2 anossovp anossovp 4096 Aug 29 2012 syntax
Get a count of only the directories in the current directory
you will get out put like 1 309 4594
2nd digit represents no. of directories.
find . -mindepth 1 -maxdepth 1 -type d | wc -l
For find -mindepth means total number recusive in directories
-maxdepth means total number recusive in directories
And for wc -l means count the lines of the input
If you only have directories in the folder and no files this does it:
Shell — Script to count number of files in each directory, I need to count the number of files on a large number of directories. Is there an easy way to do this with a shell script (using find, wc, sed, awk or similar)? Just to avoid having to write a proper script in python. The output would be something like this: $
Shell Script to Display Number of Files and Directories in a Directory
I’m trying to write a script that will tell you how many files and how many directories are in a given directory.
Here’s the script I’ve written, but the output is always «Number of files is .» and «Number of directories is .»
#!/bin/sh if [ -d "$@" ] then find "$@" -type f | ls -l "$@" | wc -l | echo "Number of files is $@" find "$@" -type d | ls -l "$@" | wc -l | echo "Number of directories is $@" fi
You seem to be having difficulties to understand how pipes work. You cannot «natively» use the «result» (stdout) of a pipe (the left-hand side) as a variable on the right-hand side of a pipe, you either need to consume and read it into a variable, e.g.
printf "line1\nline2\n" | while read line; do_stuff_with "$"; done
or you need to use command substitution (and optionally assign it to a variable), e.g.
files=$(find "$1" -maxdepth 1 -type f -printf . | wc -c)
- $@ expands to all positional parameters, in case of multiple arguments your [ -d «$@» ] will fail.
- The ls is completely superfluous
- find works recursively, but I guess you only want the first directory level to be checked so this needs the maxdepth parameter
- This will break on weird paths with newlines which can be worked around by telling find to print a character for each found directory/file and then count bytes instead of lines
In case you really don’t want this to be recursive it might be easier to just use globbing to obtain the desired result:
$ cat t.sh #!/bin/bash for file in "$"/*; do [ -d "$" ] && ((directories++)) [ -f "$" ] && ((files++)) done echo "Number of files: $" echo "Number of directories: $"
$ ./t.sh Number of files: 6 Number of directories: 1 $ ./t.sh /tmp Number of files: 9 Number of directories: 3
You might want to check man test to tweak with regards to links to obtain your desired result.
You seem to be confused on piping here.
You want the output of find . | wc -l to be expanded in the echo command.
So, your script, given what you want to accomplish should look something like this:
#!/bin/sh if [ -d "$@" ]; then echo "Number of files is $(find "$@" -type f | wc -l)" echo "Number of directories is $(find "$@" -type d | wc -l)" else echo "[ERROR] Please provide a directory." exit 1 fi
Count the Number of Directories in a Specific Directory, The find command finds directories and files on a filesystem and carries out actions on them. Let’s see how to get the count of the number of directories within a directory using the find command (recursive search): $ find . — type d | wc -l 6. The find command above finds directories in the current directory.
Counting Files and Folders in a Directory
With command line utilities or GUI file explorer, we can count files and folders in a directory on Linux or Windows.
Counting Files and Folders on Linux
To count folders in a directory recursively (excluding the directory itself), we use find and wc command:
find "$dirname" -mindepth 1 -type d | wc -l
If you do want to include the directory being counted itself, remove the -mindepth 1 arguments.
If you do not want to count recursively, add -maxdepth 1 .
Similarly, to count files in a directory recursively:
find "$dirname" -mindepth 1 -type f | wc -l
However, these commands will give false readings if file or folder name contains new line characters. To be safe, we should use a null character as the separator and count that:
# Count folders recursively find "$dirname" -mindepth 1 -type d -print0 | tr -cd '\0' | wc -c # Count files recursively find "$dirname" -mindepth 1 -type f -print0 | tr -cd '\0' | wc -c
Counting Files and Folders on Windows
Using File Explorer
The easiest way to count files and folders recursively on Windows is to use File Explorer, and check the folder properties.
Using cmd.exe
To count folders recursively, use:
dir /b /s /ad %dirname% | find /c /v ""
To count files recursively, use:
dir /b /s /a-d %dirname% | find /c /v ""
- /b option makes dir use bare format (no heading information or summary);
- /s option takes subdirectories into consideration (recursively);
- /ad option only displays directories (including hidden ones);
- /a-d option displays all files except directories (including hidden ones);
- find /c /v «» counts lines, i.e. number of files/folders in this case.
It’s safe to use dir to do the counting even if file/folder names contain new line characters, as new line characters are not output literally by dir .
Using PowerShell
To count folders recursively, use:
(Get-ChildItem -Recurse -Force -Directory "$dirname").Count
To count files recursively, use:
(Get-ChildItem -Recurse -Force -File "$dirname").Count
- -Force reveals hidden and system files/folders;
- -Directory restricts output to directories;
- -File restricts output to files.