Find number of directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find number of directories
# 1  
Old 06-06-2018
Find number of directories

Hello!

Yesterday I tried to make a shell script in order to find the number of directories in my home directory.

I tried with this:

Code:
c=0

for dic in $(find ~ -type d); do
    ((c++))
done

echo $c

The result is 4071.
If I enter "find ~ -type d | wc -l" code, the result is different (3928).

Can anyone help me?Smilie




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-07-2018 at 03:48 AM.. Reason: Added CODE tags.
# 2  
Old 06-07-2018
Welcome to the forum.


Looks like you have directory names with spaces in them. The for construct will split its input there and regard those fragments as extra items, increasing the c count artificially.

Last edited by RudiC; 06-07-2018 at 12:43 PM..
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 06-07-2018
You can set IFS to a newline character, then the for list splits only on newline
Code:
oIFS=$IFS
IFS="
"
for dic in $(find ~ -type d); do
    ((c++))
done
# restore the old behavior
IFS=$oIFS

Alternatively you can pipe to "wc -l" or "grep -c ."
Code:
c=$(find ~ -type d | wc -l)

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 06-07-2018
thanks

Thank you...
It is working well now.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Script to count number of files in directories

Hi All! I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me. I was thinking on : a=`/directory/subdirectory/ | wc -l` echo "/directory/subdirectory :$a" b=`/another_dir/subdir/ | wc -l` echo... (12 Replies)
Discussion started by: fretagi
12 Replies

3. UNIX for Dummies Questions & Answers

Number of files in all (sub-)directories

Hi, I need a list of the number of files in all (sub-)directories e.g.: /a/b/c 1364 /a/b 125 /a 362 etc. Should be nice to have the list sorted from high to low. Regards, Wim (6 Replies)
Discussion started by: deleriumdog
6 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. Shell Programming and Scripting

Need help with examine the number files in directories given as arguments

Hi , this is homework .. I have to finish it tomorrow, I did my best , but I found it so difficult .. Can You HELP Me ??! Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is... (1 Reply)
Discussion started by: abo-el-sos
1 Replies

6. Homework & Coursework Questions

ls number of directories in root help?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have been reading for several hours. Im not getting anywhere... well Im not getting there fast enough. so I... (4 Replies)
Discussion started by: rontopia
4 Replies

7. UNIX for Dummies Questions & Answers

How to get number of files and directories?

Hi All, can you please tell me how to get no.of files and directories in the given directory.:confused: (4 Replies)
Discussion started by: raju110384
4 Replies

8. Shell Programming and Scripting

How to find 777 permisson is there or not for Directories and sub-directories

Hi All, I am Oracle Apps Tech guy, I have a requirement to find 777 permission is there or not for all Folders and Sub-folders Under APPL_TOP (Folder/directory) with below conditions i) the directory names should start with xx..... (like xxau,xxcfi,xxcca...etc) and exclude the directory... (11 Replies)
Discussion started by: gagan4599
11 Replies

9. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

10. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies
Login or Register to Ask a Question