-1

I was editing a shell script and I want to know how can use for to use all directories listed by a list or variables and then execute a command.

Example:

I have this directories:

/dirname1/app1
/dirname1/app2
/dirname2/app1
/dirname2/app2

The thing is each directory have 8 application directory, and I need to get the value of each one using du for usage of each one.

I have this example piece of code that I made, but I want to convert it more better than the actually have.

#!/bin/ksh

#files="/dev /etc"
inc=1

for f in /*;
do
vari=`du -ksh $f`
        [ -d $f ]

echo "The value of each one is: ------ : $((inc++)): $vari"
done;

echo "Execution Done."
exit 0

I hope to be cleared with this,

techraf
  • 5,941
  • This really isn't clear. Are you attempting to check if $f is a directory? If so your syntax is wrong; you don't have an if statement anywhere. Do you have app1 through app8 in each of dirname1 and dirname2, and you want to list the summarized usage for each appN directory? – Wildcard Apr 25 '16 at 20:12
  • and you want to list the summarized usage for each appN directory?

    Yes, I want the usage summary of each app from app1 to app8 in each directory. I put this piece of code just for example, Im trying to make the easy way to make it but, Im newbie learning shell scripting.

    – vicdeveloper Apr 25 '16 at 20:18

1 Answers1

3

My advice is keep things simple. Don't write a whole script when there is a ready-made tool that already does what you want.

du is the tool for reporting on disk usage, and find is the tool for finding files. Use them together.

find dirname* -mindepth 1 -maxdepth 1 -type d -exec du -hs {} \;

-maxdepth and -mindepth are GNU extensions; to handle this portably you need a slightly trickier command as described here:

The command in this case would be either

find dirname* -path '*/*' -prune -type d -exec du -hs {} \;

or, if all of your dirname* directories are within, say, topdirectory and there are no other directories in there, use:

find topdirectory -path '*/*/*' -prune -type d -exec du -hs {} \;

This will only report on the directories within the directories within topdirectory, which seems to be what you're asking for.


Update: I took another look at this and actually, you can do what you need entirely with shell globbing:

du -hs dirname*/app*/

Your directory are probably not actually named like this, but you could even run du -hs */* and it would work—it just might include some other directories (or files) you didn't want to list, depending on how clean (uncluttered) you keep those directories.

Wildcard
  • 36,499
  • Im using Solaris 10/11. An I got an error.

    find: bad option -maxdepth find: [-H | -L] path-list predicate-list

    Any idea how can I use this in this oss?

    – vicdeveloper Apr 25 '16 at 20:26
  • @vicdeveloper, sorry, my first command is no good for Solaris. I've updated my answer; try one of the other methods I suggested. – Wildcard Apr 25 '16 at 20:34
  • I got this error:

    find: bad option -path

    Any idea?

    – vicdeveloper Apr 25 '16 at 20:39
  • @vicdeveloper Wow, I haven't used Solaris and now I'm glad of that. :) Updated with the really simple answer. Hope that works for you? – Wildcard Apr 25 '16 at 20:47
  • Yes, I tried that one, but, the thing is that the execution time is very large, and it takes around 15 minutes for each app directory, many of then sizes around 1,2 gb and greater than it.

    It is possible try to optimize the result of time?

    – vicdeveloper Apr 25 '16 at 20:50
  • 1
    @vicdeveloper, that's a completely separate issue. The command does what you want; any command that actually checks the disk usage right now of several GB of files is going to take a bit of time. That's the nature of the task. – Wildcard Apr 25 '16 at 20:55
  • I wonder if you've really expressed what you're actually trying to solve. Perhaps checking total disk usage (using df) would be sufficient. How often is the usage expected to change? I don't have enough information to make an intelligent decision about what you should do—only you have that info. I did answer your specific question, though. ;) – Wildcard Apr 25 '16 at 20:57
  • Great. It Works. :D!. I passed many directories as parameters in the same command and it shows me the info that I want.

    Thanks.

    – vicdeveloper Apr 25 '16 at 20:58
  • 1
    that Update should be du -sh dirname*/app*/ (with a trailing /) if you want to run du only on sub-directories matching app* but not files matching app*. – cas Apr 26 '16 at 02:20
  • @cas, excellent point; updated. – Wildcard Apr 26 '16 at 02:22