number of subfolders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting number of subfolders
# 1  
Old 01-22-2012
number of subfolders

Hello people i need your help, am still new at unix, How do you find the number of subfolders in a certain or current directory??
# 2  
Old 01-22-2012
This will list all of the directories in or below the directory /path

Code:
find /path -type d

You can pipe that to wc -l for a count of directories.

Code:
find /path -type d|wc -l

It will include /path in the output, so you might want/need to subtract one depending on what your expected result is.
# 3  
Old 01-22-2012
Code:
printf "%s\n" ./*/ | wc -l

# 4  
Old 01-22-2012
Thank you everyone,but how do i put it in a code so that it is executable??
# 5  
Old 01-22-2012
This would be one way.

Using vi, or your favorurite editor, edit a file (we'll call it ndirs.ksh). Add these lines to the file and write the file out:

Code:
#!/usr/bin/env ksh
# change ksh in the previous line to bash, or your preferred shell, if you'd 
# rather not use kshell. 

nd=$( find ${1:-$PWD} -type d | wc -l )     # count directories and save in var
printf "there are %d directories below %s\n" $nd ${1:-$PWD}    # print the var

exit

You now have a script: ndirs.ksh. At the command line, make the script executable with the command:

Code:
chmod 755 ndirs.ksh

Assuming you've been working in a scratch or development dirctory, copy the script to a directory in your PATH. Probably $HOME/bin:

Code:
cp ndirs.ksh ~/bin/

Now you can run the script from the command line:

Code:
ndirs.ksh
ndirs.ksh /tmp/foo

to get output like:

Code:
there are 1 directories below /tmp/foo

This is a very simple script without any error handling or other 'fluff' that would be needed for long term use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Help with listing only subfolders

Hi All, I have a AIX file system with a folder structure like this /1 /1/2 /1/2/3/5 /1/2/3/2 /2 /2/3/4 /2/5/6 I would like to list subdirectories /1/2/3/5 /1/2/3/2 /2/3/4 /2/5/6 Can you please help me with this issue. (6 Replies)
Discussion started by: kavinmjr
6 Replies

2. UNIX for Dummies Questions & Answers

Need help in checking for files in subfolders

Hi, I am trying to print a listing of files from the top level directory, check to see if any files have the same name as the top level directory name and if so, cd to that file and list the files under it. Don't know how to check for the file in the next level. What I have so far: ... (6 Replies)
Discussion started by: tes218
6 Replies

3. Shell Programming and Scripting

grep to search the subfolders

Hi everybody, I am trying to grep abcds in one folder and in all its subfolders! but it does not work! would you help please? thanks. (3 Replies)
Discussion started by: messi777
3 Replies

4. Shell Programming and Scripting

Search in folder and subfolders

How can this be done? I mean, I want to search for all *png *jpg *bmp files in my ~/Pictures/ folder....How can I list them? Thank you geeks :) :b: (2 Replies)
Discussion started by: hakermania
2 Replies

5. UNIX for Dummies Questions & Answers

Last Modified Date for subfolders

Sorry for the basic question, but I have a feeling that my developers are circumventing our change control process, and I want to be able to easily keep track of the last modified date of sub-folders of the production folder. Basically, we have this major folder PROD, and then each application... (1 Reply)
Discussion started by: saint01
1 Replies

6. UNIX for Dummies Questions & Answers

How to learn the number of files under a particular folder, containing subfolders

Hi ALL I would like know how many files there under a particular folder, which contains subfolders. Thanks (5 Replies)
Discussion started by: cy163
5 Replies

7. Shell Programming and Scripting

How to create folders/subfolders using shellscript

Hi, Can any one help me how to create folders using shellscript.My requirement is: FolderName: Main/Main1 :Main/Main2 :Main/Main3 underSubFolder : Main1/A :Main1/B :Main1/C underSubfolder: A/A1 ... (2 Replies)
Discussion started by: ram2s2001
2 Replies

8. UNIX for Dummies Questions & Answers

rename files in subfolders

Hello i have this script : foreach f ($1/*.cpp ) mv $f $f:r.c end that renames me files in dir , how can i change it so it will rename me also in subdirectorys? thanks (0 Replies)
Discussion started by: umen
0 Replies

9. UNIX for Dummies Questions & Answers

subfolders help please important

hi all. I just only wanna show all files from a whole 'root-table'....to understand: "ls *.exe" ...for instance in the directory it only shows the files from the folder, but not form the subfolders !? how to solve this problem ? if you are on your home /root/users/.... I want to see all files... (10 Replies)
Discussion started by: svennie
10 Replies

10. UNIX for Dummies Questions & Answers

TAR (Extract with subfolders)

Hi! I've uploaded a .tar file (created with Windows) containing my homepage to my webserver. Now I'd like to extract the file on the server. I can do that with SSH. BUT:: If i enter this command: tar -xf homepage.tar only the files in the root directory of the file get extracted.... (2 Replies)
Discussion started by: roberthawke
2 Replies
Login or Register to Ask a Question