If the pattern "mypattern" is found in server1/ab_123456_1 and if another pattern is found ab_123456_2 then i want the to du -k mypattern and sum and the result saved as shown.
find * -type d -name mypattern | xargs -r du -sk | while read k n
do
$(( sum += k ))
done echo $sum
executed the above command got error message:
./test.sh: line 3: 8: command not found
./test.sh: line 3: 12: command not found
./test.sh: line 3: 16: command not found
./test.sh: line 3: 24: command not found
./test.sh: line 3: 28: command not found
awk -F"[ _]" '$0~p {a[$3]+=$1} END {for (i in a) print a"k "i"/"p}' p="mypattern" server1> result.txt
awk: cmd. line:1: fatal: cannot open file `server1' for reading (Inappropriate ioctl for device)
cd server1
ls /etc/ -l > ab_1234567_1/mypattern/etc.txt
ls /etc/ -l > ab_1234567_2/mypattern/etc.txt
ls /etc/ -l > ab_1234569_3/mypattern/etc.txt
ls /etc/ -l > ab_1234569_2/mypattern/etc.txt
Thank you so much.
after execute the script, notice is different from your output.
also adding ab_1234567_9 directory, supposed they added to the sum of 36k which is 2/mypattern/mypattern +16 and will become 52 and so on but is not.
steps:
d_path=/root/server1
pattern=mypattern
find to $d_path with exact $pattern to all directory expression something like this 'ab_[0-9]\{6,\}\_[0-9]\{0,\}'
grep my $pattern to all directory found my expression. ei ab_1234567_9 or ab_12345673334_96
du -k to all $pattern that was found in expression and sum them all $pattern belong to same expression.
slice the expression something like ab_1234567_9, ab_1234567_19 and ab_1234567_20 and so on become 1234567 and the final output would be:
total expression/pattern
32k 1234567/mypattern
4k 1234568/mypattern
32k 1234569/mypattern
wish you could elaborate or explain then is nice.
ls
result.txt server1 server2 test.sh
pwd
/root
du -k | awk -F"[ _]+" '$0~p {a[$3]+=$1} END {for (i in a) print a"k "i"/"p}' p="mypattern" > result.txt
find * -type d -name mypattern | sed '
s/_[0-9]*\/mypattern$//
' | sort -u | while read p
do
sum=0
ls ${p}_[0-9]/mypattern | xargs -r du -sk | while read k n
do
(( sum += k ))
done
echo $p $sum
done
Find the mypattern dirs, remove any parent suffix and the child name, sort unique, and for each, start the sum at zero, find the size of all files in the family of directories and add to sum, and print out a summary line on this pattern.
find * -type d -name mypattern | sed ' s/.*_[0-9]*\/mypattern$// ' | sort -u | while read p do sum=0 ls ${p}_[0-9]/mypattern | xargs -r du -sk | while read k n do (( sum += k )) done echo $p $sum done