LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-21-2009, 10:36 AM   #1
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Rep: Reputation: 15
Shell Script Question


I am new to shell scripting and am getting stuck into some documentation to help.

I am looking at writing a short script which will give me a directory sizes of a systems home directories and pipe the results into a text file.

I can issue the following command to get the readings I want for each users home directory

Code:
du -sh /home/user
What I'd like to do would be to create a variable/array which I can run the command against.

I have tried to create an array built on the result of

Code:
ls /home
to see if that would work but it hasn't done what I wanted. Would anybody be able to point me in the right direction so I can get cracking?

Many thanks,
 
Old 10-21-2009, 11:00 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Thumbs up

You can try this command


Code:
> /tmp/size
for i in `ls -lrth /home | grep ^d | awk '{print $9}' | grep -v lost+found`;do du -sh $i >> /tmp/size; done;

This will create a file /tmp/size, containing the needed info like.
Code:
   4K   eagle
  19M   vikas
 836M   browser
   1G   mi

I request the moderators to move this thread to programming section.

Last edited by vikas027; 10-21-2009 at 11:02 AM. Reason: Request to moderators.
 
Old 10-21-2009, 11:19 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by vikas027 View Post
I request the moderators to move this thread to programming section.
The "Report" button is a neat way to contact the moderators.
 
Old 10-22-2009, 03:23 AM   #4
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for your reply, really appreciated.

I have run the command you posted in a script but it creates an empty /tmp/size file.

I've tried breaking the command up to see where the problem might be.

If I run

Code:
ls -lrth /home | grep ^d | awk '{print $9}'
from the command line, I don't get any output just a few empty lines from where I ran the command to where the new command line is.

Would this be where the problem is?

Many Thanks for your help.
 
Old 10-22-2009, 03:44 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by keenboy View Post
Thanks for your reply, really appreciated.

I have run the command you posted in a script but it creates an empty /tmp/size file.

I've tried breaking the command up to see where the problem might be.

If I run

Code:
ls -lrth /home | grep ^d | awk '{print $9}'
from the command line, I don't get any output just a few empty lines from where I ran the command to where the new command line is.

Would this be where the problem is?

Many Thanks for your help.
If you are not getting any output, that means there are no directories (or home directories of users) in /home.

Could you please post output of
Code:
ls -lrth /home
and
useradd -D
 
Old 10-22-2009, 06:07 AM   #6
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Here is the output from those commands, thanks.

Code:
ls -lrth /home

drwxr-xr-x   4 demo2         users    4.0K 2006-06-12 12:25 demo2
drwxr-xr-x   4 demo          users    4.0K 2006-06-12 14:27 demo
drwxr-xr-x   3 stadmin       users    4.0K 2008-03-31 12:39 stadmin
drwxr-xr-x   3 lidias        users    4.0K 2008-11-07 16:26 lidias
drwxrwxr-x  22 alex          users    8.0K 2009-09-23 10:37 alex
Code:
useradd -D

GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=
SKEL=/etc/skel

Last edited by keenboy; 10-22-2009 at 06:08 AM. Reason: forgot the code tags!
 
Old 10-22-2009, 07:18 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
You can try this command


Code:
> /tmp/size
for i in `ls -lrth /home | grep ^d | awk '{print $9}' | grep -v lost+found`;do du -sh $i >> /tmp/size; done;
you are going a round about way to finding directories in the /home path. Also, if directory names have spaces, your awk command with $9 won't work. an easier way is to use find, just an example
Code:
$ find /home -type d -print0 | xargs -0 du -sh
 
Old 10-22-2009, 08:33 AM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question

Quote:
Originally Posted by ghostdog74 View Post
you are going a round about way to finding directories in the /home path. Also, if directory names have spaces, your awk command with $9 won't work. an easier way is to use find, just an example
Code:
$ find /home -type d -print0 | xargs -0 du -sh
Thanks, ghostdog. But I checked it on my system, this commands includes sub-directories too. This command is also printing space of all subdirectories

However, the OP just wants the size of the home directories of users.
 
Old 10-22-2009, 08:40 AM   #9
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Lightbulb

Oops, my mistake. I forgot to write /home in the script after du -sh (to make it path independent).

Now, try.
Code:
> /tmp/size
for i in `ls -lrth /home | grep ^d | awk '{print $9}' | grep -v lost+found`;do du -sh /home/$i >> /tmp/size; done;
There might not be the optimal way but this is as per my competence.
 
Old 10-22-2009, 09:07 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Thanks, ghostdog. But I checked it on my system, this commands includes sub-directories too. This command is also printing space of all subdirectories

However, the OP just wants the size of the home directories of users.
depth of recursive can be fixed with -prune or -maxdepth (GNU find). you have misunderstood what i mean by directory names with spaces
Code:
# ls -1
dir with space
t1
t2
test.txt
test with space.xml
test.xml

#  find . -type d -print0 | xargs -0 du -sh
40K     .
8.0K    ./t1
8.0K    ./t2
8.0K    ./dir with space
the command correctly shows directory with spaces and its size.

whereas your for loop:

Code:
$ for i in `ls -lrth  | grep ^d | awk '{print $9}'`; do echo "$i"; done
with
 
Old 10-22-2009, 10:08 AM   #11
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by ghostdog74 View Post
Code:
$ for i in `ls -lrth  | grep ^d | awk '{print $9}'`; do echo "$i"; done
with
Thanks, I got the message.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Question ytnexus Linux - Newbie 3 06-28-2009 09:22 PM
Shell Script Question. rvijay Linux - General 2 07-14-2005 06:41 PM
Shell Script Question swinchen Programming 1 08-20-2004 02:09 PM
shell script question Axion Programming 4 07-29-2004 09:51 PM
Shell script question rytrom Linux - Newbie 4 01-28-2004 07:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:51 PM.

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