executing command from subdirectories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting executing command from subdirectories
# 1  
Old 10-01-2009
executing command from subdirectories

Hello,

I've been trying to run 'ls -1R | wc -l' inside of sub directories to in order to determine how big each folder is.

Code:
find . -maxdepth 1 -type d | while read folder

 do
    cd "$folder" &&
        echo "$folder has $(ls -1R | wc -l) files" &&
    cd ..
 done

or

Code:
for folder in `ls -1S`; do 
    cd "$folder" &&
        NUMFILES=$(ls -1R | wc -l)
        FOLDERLIST='  $folder contains $NUMFILES files.'
    echo $FOLDERLIST >> ../filelist.txt && 
    cd ..
done

I know the second one works as long as I ignore the errors, but it does not work with directory names with spaces.
# 2  
Old 10-01-2009
Hi.

Try putting quotes (") around the `ls -1S` part.

Better is not to use this style, but

Code:
ls -1S | while read folder; do
...

With the style you chose, you're at the mercy of how long your arg list can be. With read, you are not.

Also with "read folder" the space problem is not an issue.

To get rid of the errors direct them to /dev/null
Code:
cd $folder 2> /dev/null

Or better perhaps
Code:
cd $folder 2> /dev/null || continue


Last edited by Scott; 10-01-2009 at 04:23 PM.. Reason: added bit about errors
# 3  
Old 10-01-2009
Maybe use this?

Code:
function my_function
{
    for file_or_directory in *;do
        [[ -d "${file_or_directory}" ]] && {
            cd "${file_or_directory}"
            count_files_in_any_way_since_there_are_many_ways
            cd "${OLDPWD}"
            my_function
        }
    done
    return 0
}


Last edited by pludi; 10-03-2009 at 04:12 PM.. Reason: code tags please
# 4  
Old 10-03-2009
Solved

Thanks adderek. Your solution fixed everything.

Here is the final code:
Code:
for file_or_directory in *;do
[[ -d "${file_or_directory}" ]] && {
    cd "${file_or_directory}"
    echo "${file_or_directory} has $(ls -1R | wc -l) files"
    cd "${OLDPWD}"
}
done

and outputs:
Code:
clients has 7099 files
current projects has 1230 files
orgainise has 1733 files
toserver has 60203 files
webiste stuff has 831 files

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX command to display Owner,Group,Root and Subdirectories list

Hi Team, Am a newbie to Unix. As I would like to see the Server Name,Owner Name ( not numeric form), Group Name ( not numeric ID), ROOT path. I would like to send this list as an attachment to my personal mail. Can any one please help me out to to resolve this . Here is the sample result... (6 Replies)
Discussion started by: vasuvv
6 Replies

2. UNIX for Dummies Questions & Answers

UNIX - command to count number of files in subdirectories

I have a folder named test/ and under that I have multiple directories and in each of the directory I have multiple log files. I want to know how many files exists under each sub directory. test |--quanrantine |--logfile1 |--logfile2 |--spooling |--logfile1 ... (4 Replies)
Discussion started by: ravikirankethe
4 Replies

3. Shell Programming and Scripting

Executing a command line in a directory and its subdirectories

I'm finally about to make the jump from PHP's mysql driver to the newer mysqli one using the mysqli converter tool. The tool is called by this command line: php /usr/share/nginx/html/rather/converter/cli.php -d -p ".php,.shtml,.inc" -u -b -v -w >> /tmp/convert.log e.g. at the web root:... (2 Replies)
Discussion started by: dheian
2 Replies

4. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

5. Shell Programming and Scripting

Error while executing sh command

Hi, I have 2 files temp1.sh and temp2.sh as follows: =========== temp1.sh =========== echo "session1" sh temp2.sh echo "exit session2 and enter session1" ================================= ============= temp2.sh ============= echo "session2" sh echo "exit session2"... (5 Replies)
Discussion started by: RP09
5 Replies

6. UNIX for Dummies Questions & Answers

Command for total number of files (and size) across subdirectories?

Hi all... I have a directory called dbrn. This directory contains an unknown number of subdirectories which in turn contain an unknown number of files. What I want to know is: How many files with extention .ABC can be found in /dbrn across all subdirecties, and what is the total size for... (9 Replies)
Discussion started by: Beun
9 Replies

7. UNIX for Dummies Questions & Answers

Doing a capture while another command is executing?

Basically what i'm trying to do is execute an update command and at the same time have the system do a TCPdump to file for that update traffic. So I would like to connect the two commands so that the tcpdump terminates automatically when the update finishes/fails/whatever. Right now I have... (0 Replies)
Discussion started by: MrEddy
0 Replies

8. UNIX for Dummies Questions & Answers

ls command to list recursively ONLY subdirectories

:confused: ls -dlRr I've tried different combinations of the ls command using the above-mentioned options but none of them are giving me the output I am looking for. Objective: To get a recursive listing of all subdirectories from a particular starting point. For example, if my starting... (5 Replies)
Discussion started by: HLee1981
5 Replies

9. Shell Programming and Scripting

Executing a command at startup

Hey all, How do I execute a file at startup automatically. From what I've read is that I need to put it into my .bashrc file. I'm not sure where to go from there. Can I just type commands into that and they'll run next time I restart my server? Right now I have added these lines: cd... (2 Replies)
Discussion started by: NathanWarden
2 Replies

10. UNIX for Dummies Questions & Answers

get command to ftp subdirectories at once

How do I use the "get" command in unix to ftp a directory with many sudirectories at one shot? Any idea anyone?? (3 Replies)
Discussion started by: lydiaEd
3 Replies
Login or Register to Ask a Question