Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-04-2008, 01:00 PM   #1
Registered: Dec 2005
Posts: 76

Rep: Reputation: 15
scripting, number of subdirectories in a subdirectory

I have a number of questions:

1. How do I calculate the number of sub-directories within a particular directory, such that I can loop through all of those directories and perform some action.

2. With a file of data containing two columns, how would I pull out the last column, last row data into a variable?

Old 06-04-2008, 03:42 PM   #2
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
  1. You can do “find -type d” to get all directories.
  2. You can do this a few ways. Here it is in awk:
    awk '{a = $2};END{print a}'
Old 06-04-2008, 05:33 PM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,435

Rep: Reputation: 204Reputation: 204Reputation: 204
yes well, if you know the depth you can do,

for dir in */*/   # the trailing / means dir, try it with ls */
   (cd $dir; pwd) # in a subshell so the cd is forgotten
or as osor said
find . -type d |
while read dir; do
   (cd $dir; pwd)
count is simple: find . -type d -print | wc -l

Last edited by bigearsbilly; 06-04-2008 at 05:35 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell scripting: How can I increment a file by number dv502 Programming 11 08-27-2010 09:43 AM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 02:30 AM
where is my subdirectory blotch Linux - Newbie 22 06-18-2005 12:43 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
Bash Scripting (subdirectories) bendeco13 Linux - General 4 10-07-2004 01:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration