Find the answer to your Linux question:
Results 1 to 6 of 6
Hey guys, so still learning how to code my own shell scripts, most of the ones i've created took a while just because i had to look everything up but ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    2

    shell script to scan all directories?


    Hey guys, so still learning how to code my own shell scripts, most of the ones i've created took a while just because i had to look everything up but i'm learning bit by bit.

    Anyways what i'm looking to do is to create a script i can install and setup under a cron job once a week that will scan all directories in the /home/ folder and output to a file the results.

    Here's what i need help with:

    Setting up some type of loop or something that will give me all the directories under the /home/ folder that I can then scan each one using

    find . | wc -l

    to show the inode count, then output that number to a file with the directory.

    Basically i'm trying to create something that i can run and will scan all the users directories under the /home/ folder and ouput the inode count to a file. This way i do not have to go through each users folder and run that command just to get the output.


    I know this sounds simple but i was hoping someone could point me in the right direction on where to get started with running that command on each directory under the /home/ directory.

    I know this syntax isnt correct but here's what i want to do:

    for $eachdirectory under /home
    $inodes = find $eachdirectory | wc -l
    echo $eachdirectory $inodes >> /home/inodecount
    next for


    haha i know that syntax is sloppy as hell and probably includes php and shell scripting, i was just trying to give an idea of what i'm looking to do.


    Can anybody help me with this or point me in the right direction? Thanks a million!

  2. #2
    Just Joined!
    Join Date
    Aug 2006
    Posts
    16
    You're looking for this, I imagine:
    <code>
    for dir in /home/*
    do
    nodes=`find $dir | wc -l`
    echo "$dir $nodes" >>/home/inodecount
    done
    </code>

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    7

    Using your same logic, but actual commands

    Your pseudo-code:

    <code>
    for $eachdirectory under /home
    $inodes = find $eachdirectory | wc -l
    echo $eachdirectory $inodes >> /home/inodecount
    next for
    </code>

    My version (I'm making a lot of assumptions here, such as ksh):

    <code>
    for eachdirectory in `ls /home` ; do
    inodes=`find /home/${eachdirectory} | wc -l`
    echo "${eachdirectory} ${inodes}"
    done > /tmp/inodecount
    </code>

    NOTES: I moved your output file out of /home to avoid confusion in your reporting. Also, I am assuming none of your directory entries in /home have spaces. Spaces will throw off the KSH for loop every time.

    It goes differently in PERL. The script would be longer but would not care about spaces in file/directory names.

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    7
    John's answer is a solid gold answer.

  5. #5
    Just Joined!
    Join Date
    Sep 2011
    Posts
    2
    Yup, worked perfect, thanks a ton man!!!

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    648
    Quote Originally Posted by John Rodkey View Post
    You're looking for this, I imagine:
    <code>
    for dir in /home/*
    do
    nodes=`find $dir | wc -l`
    echo "$dir $nodes" >>/home/inodecount
    done
    </code>
    Code:
    for dir in /home/*; do
         echo "$dir $(ind $dir | wc -l)"
    done >/home/inodecount

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •