Linux count directories in directory

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?

The additional 1 count is there due the the current directory ‘.’ , which is also taken into account by ‘find’.

17 Answers 17

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 

Источник

Count Number of Files and Directories in Directory on Linux

Count number of files in directory and subdirectory Linux. In this tutorial guide, you will learn how to count number of files in directory and subdirectory linux. And as well as how to count hidden files and directories in Linux system.

Читайте также:  Linux mint изменить пароль пользователя

Whenever you are probably monitoring disk space on your system all the time. And as well as, you may want to know How many files and directories are in the given directory, or in many different directories or subdirecotories.

So, this tutorial guide will help you on how you can easily count files and directories in a directory on Linux using ls , find and tree commands.

How to Count Files in Directory in Linux

Different ways to count number of files and directories in directory, subdirectory and hidden directory linux:

  • Count Files using wc
  • Count Files Recursively using find
  • Count Files using tree
  • Count Hidden Files

Count Files using wc

Now, you will learn the easiest way to count files in a directory on Linux using the “ls” command and pipe it with the “wc -l” command, As shown below:

Note that, The “wc” command is used on Linux in order to print the bytes, characters or newlines count.

Now, i will run the “ls” command on the “/html” directory and pipe it with the “wc” command, as shown below:

The output will be as shown below:

Count Files Recursively using find

Count files recursively on Linux using the “find” command and pipe it with the “wc” command. As shown below:

For example, if you want to recursively count files in the “/html” directory, you would write the following query:

If you are finding to some error messages with the above command, so you can use the following command on it to count files recursively in direcotry:

find /etc -type f 2> /dev/null | wc -l

Count Files using tree

If you want to count files and directories in a directory. So, you can use “tree” command and to specify the name of the directory to be inspected. As shown below:

This is very important thinga about the “tree” command, it is not installed on all hosts by default.

If you are having a “tree : command not found” or “tree : no such file or directory”, you will have to install it using sudo privileges on your linux system. By executing the following commands:

sudo apt-get install tree // for Ubunbu/Debian hosts sudo yum install tree // for CentOS/RHEL hosts

Count Hidden Files

If you want to count hidden files and directories in a directory. So, you can use “tree” command with -a parameter. As shown below:

Conclusion

Count files and direcotries in linux tutorial guide, you have learn how to count files and directories in directory using the ls , find and tree commands.

Читайте также:  Linux run two commands in parallel

Источник

Count number of directories

I need a script or command that prints a number of directories which name begins from «lib» in whole directory subtree. I was trying to do it using find, grep, and wc but can’t scan all directories. How to do it?

4 Answers 4

find . -type d -name lib\* -exec echo x \; | wc -l 

That does run one echo command in a new process for each lib* directory though, which is not efficient.

I doubt there’s any reason to use echo. find . -type d -name lib\* -printf «%i\n» | wc -l should be sufficient. Let find handle everything instead of calling extra process there.

LC_ALL=C find .//. -name 'lib*' -type d | grep -c // 

You can’t use find . | wc -l as that wouldn’t work properly if there are file paths with newline characters.

Without LC_ALL=C that could fail to count dir names that start with lib but where the rest of the name contains bytes that don’t form valid characters.

Wait, can you explain how does .//. work ? I get the idea that grep -c should be able to count lines starting with that pattern, but it seems like after ./ should only be filenames and yet there’s another slash dot there, and it still works. How ?

@SergiyKolodyazhnyy . is the current directory, which may also be spelled ./ . Slashes can be repeated, so .// is still the current directory, and .//. is the same as well ( foo/. is always the same as foo ). find will print its results relative to the root directory you specified, in this case like .//./libwhatever , .//./foo/bar/libsomething , etc.

Assuming all lib -directories have sane names with no newlines or other strange/exotic characters:

find / -type d -name "lib*" -print | wc -l 

This assumes that you, by the «whole directory subtree», mean «anywhere». Change / into . to only count in the current directory or below.

The find command would find all directories ( -type d ) whose name start with lib ( -name «lib*» ) and print these ( -print ).

The wc -l would count the number of lines in the output from find .

Источник

How to count subdirectories in a directory in UNIX?

I have to count the total number of directories that are in the given directory (note that these are subdirectories). I know how to count for files but am having trouble counting the directories for my script.

Just in the directory (not including the current working directory) I used the above format and then just subtracted 1

3 Answers 3

In a slightly robuster variation of @rahul’s comment:

find . -type d -mindepth 1 -printf '1' | wc -c 

will print ‘1’ for each directory in the current one that is not the current one ( -mindepth 1 ), and then we count the 1s. This will cope with directory names that contain newlines.

Читайте также:  Узнать владельца файла linux

If you put a / at the end of a wildcard pattern, the pattern will only match directories and symbolic links to directories. The following snippet therefore counts directories and symbolic links in the current directory.

set -- */ .*/ if ! [ -e "$1" ]; then shift; fi # handle the case when */ matches nothing echo $(($# - 2)) # -2 for . and .. 

In bash, you can simplify this to

shopt -s dotglob nullglob dirs=(*/) echo $

If you don’t want to include directories, use find . If none of the directories have a name that contains a newline, you can count the lines:

find . -name . -o -type d -print -prune | wc -l 

To be fully robust, count slashes instead.

find . -name . -o -type d -print -prune | tr -dc / | wc -c 

Источник

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 

Your whole script is broken if there happens to be spaces in file names. As you’re learming, my first advice is: Use More Quotes!

Another comment: I can tell that your sources for learning bash are outdated and don’t show good practices.

You could have a space in a filename. Try this: touch «hello Kitty» , you’ll have a file with a space in its name. Worse, try touch $’hello\nKitty’ , you’ll have a file the name of which will not be easy to handle with the methods involving find .

Источник

Оцените статью
Adblock
detector