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 ...
- 09-14-2011 #1Just 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!
- 09-14-2011 #2Just Joined!
- Join Date
- Aug 2006
- Posts
- 12
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>
- 09-14-2011 #3Just Joined!
- Join Date
- Aug 2011
- Posts
- 6
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.
- 09-14-2011 #4Just Joined!
- Join Date
- Aug 2011
- Posts
- 6
John's answer is a solid gold answer.
- 09-14-2011 #5Just Joined!
- Join Date
- Sep 2011
- Posts
- 2
Yup, worked perfect, thanks a ton man!!!
- 09-17-2011 #6Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262


Reply With Quote
