Maybe if you explained a bit more what we are looking at, and what you want (which doesnt seem to be what you mentionned in the title:
We could try to help you but here I must say I am confused...
e.g.
100k server1/ab_1234567_1/mydir
if this is a directory structure what is the meaning of the space after the initial 100k?
Or should you first tell us what your OS is and the shell you are using...
Just go step by step. First get the list of prefixes, sorted unique, then for each make a total for the prefix_/, then detect any prefix_/mydir/ and if present make a total for that.
ls server/ab_*_*/|sed '
s/^server.ab_\([0-9]*\)_[0-9]*.$/\1/
' | sort -u | while read pfix
do
sum=0
du -sk server/ab_${pfix}_* | while read k n
do
(( sum += k ))
done
echo ${sum}k $pfix
sum=0
du -sk server/ab_${pfix}_*/mydir | while read k n
do
(( sum += k ))
done
if [ $sum = 0 ]
then
continue
fi
echo ${sum}k $pfix/mydir
done
thank for the response and yes that what exactly I want, but executing the script I got error message:
and here is my process:
pwd
/root
ls server
ab_1234510_40 ab_1234511_1 ab_1234567_1 ab_1234567_2 ab_1234567_3 ab_1234569_1 ab_1234569_4 ab_1731458_9
nano test3.sh
then copy and paste your script
./test3.sh
du: cannot access `server/ab__*': No such file or directory
0k
du: cannot access `server/ab__*/mydir': No such file or directory
du: cannot access `server/ab_file.txt_*': No such file or directory
0k file.txt
du: cannot access `server/ab_file.txt_*/mydir': No such file or directory
du: cannot access `server/ab_mydir_*': No such file or directory
0k mydir
du: cannot access `server/ab_mydir_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234510_40/:_*': No such file or directory
0k server/ab_1234510_40/:
du: cannot access `server/ab_server/ab_1234510_40/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234511_1/:_*': No such file or directory
0k server/ab_1234511_1/:
du: cannot access `server/ab_server/ab_1234511_1/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234567_1/:_*': No such file or directory
0k server/ab_1234567_1/:
du: cannot access `server/ab_server/ab_1234567_1/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234567_2/:_*': No such file or directory
0k server/ab_1234567_2/:
du: cannot access `server/ab_server/ab_1234567_2/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234567_3/:_*': No such file or directory
0k server/ab_1234567_3/:
du: cannot access `server/ab_server/ab_1234567_3/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234569_1/:_*': No such file or directory
0k server/ab_1234569_1/:
du: cannot access `server/ab_server/ab_1234569_1/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1234569_4/:_*': No such file or directory
0k server/ab_1234569_4/:
du: cannot access `server/ab_server/ab_1234569_4/:_*/mydir': No such file or directory
du: cannot access `server/ab_server/ab_1731458_9/:_*': No such file or directory
0k server/ab_1731458_9/:
du: cannot access `server/ab_server/ab_1731458_9/:_*/mydir': No such file or directory
Well, sometimes it is cheaper to "2>/dev/null" than to test for potential directories or files.
You can test inside 'for x in zzz*;do ... done' to see if x is literally "zzz*" and continue. Note that the shell does all the work expanding glob expressions like zzz* without the cost of a fork()/exec() of 'ls' or 'find'.
The form 'ls zzz* 2>/dev/null | while read e' does nothing if zzz* does not expand, ls errors it out on stderr and it is never read from stdout pipe stdin into e, unlike the 'for' case.
The 'find' command does not complain or provide unexpanded glob expressions like 'zzz*', but it goes down the tree, whereas 'ls' just does the dirs or files (and unexpanded glob expressions like 'zzz*') it is given.
Well, not quite up to the forgiveness/permission level! More hidden requirements for the competence of those who use and maintain, avoiding anything that might cause them to overreact, like 'normal errors', so sometimes it is important to have none logged. It has more to do with the local culture. Shops vary enormously in that.
Trying to avoid error messages with pre-emptive file existence checks is indeed a matter of asking for permission instead of forgiveness. Further, it's a faulty approach because of the race between the check and the subsequent command.
Race situations are not easy to process error free, like if you mv input_file3976 input_file3976_$$ and it errors ENOENT, someone else renamed it first. If input_file3976_$$ exists, you got there first, and used directory file locking to get control of the file cleanly.
Luckily, most scripts run in a quiexcent situation.