Finding directories with expression | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Finding directories with expression

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2013
lxdorney lxdorney is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 25 October 2013, 5:51 AM EDT
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
Finding directories with expression

Hi All,

I need your help in finding pattern of directories.

need to search for all pattern have "mypatern" from base directory folder.

example
-------

server1 - base directory

100 server1/ab_123456_1/mypattern
100 server1/ab_123456_2/mypattern
200 server1/ab_123457_1/mypattern
400 server1/ab_123457_3/mypattern

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.

result.txt
---------
200k 123456/mypattern
600k 123457/mypattern

Appreciate your help in Advance.

Thanks
lxdorney
Sponsored Links
    #2  
Old 02-08-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts

Code:
find * -type d -name mypattern | xargs -r du -sk | while read k n
  do
    $(( sum += k ))
  done
echo $sum

Sponsored Links
    #3  
Old 02-09-2013
lxdorney lxdorney is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 25 October 2013, 5:51 AM EDT
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
very appreciate here thanks


Code:
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:

Code:
./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

but when execute this

Code:
find * -type d -name mypattern | xargs -r du -sk

output:

Code:
8       server/ab_1234568_2/mypattern
4       server/ab_1234567_2/mypattern
4       server/ab_1234569_2/mypattern
8       server/ab_1234567_1/mypattern
4       server/ab_1234569_3/mypattern

is there any chances that can group the same sequence of directory filename?
8 server/ab_1234567_1/mypattern
4 server/ab_1234567_2/mypattern

4 server/ab_1234569_2/mypattern
4 server/ab_1234569_3/mypattern

the output would be

Code:
8   ab_1234569/mypattern    
12 ab_1234567/mypattern


Last edited by Scrutinizer; 02-09-2013 at 02:43 AM.. Reason: Additional code tags
    #4  
Old 02-09-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 July 2014, 9:30 AM EDT
Posts: 1,038
Thanks: 62
Thanked 216 Times in 204 Posts

Code:
awk -F"[ _]" '$0~p {a[$3]+=$1} END {for (i in a) print a[i]"k "i"/"p}' p="mypattern" server1 > result.txt
cat result.txt
200k 123456/mypattern
600k 123457/mypattern

Sponsored Links
    #5  
Old 02-09-2013
lxdorney lxdorney is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 25 October 2013, 5:51 AM EDT
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts

Code:
awk -F"[ _]" '$0~p {a[$3]+=$1} END {for (i in a) print a[i]"k "i"/"p}' p="mypattern" server1> result.txt
awk: cmd. line:1: fatal: cannot open file `server1' for reading (Inappropriate ioctl for device)

my apologies, server1 is a directory not a file

my process goes like this:


Code:
mkdir -p server1/ab_1234568_2/mypattern
mkdir -p server1/ab_1234567_2/mypattern
mkdir -p server1/ab_1234569_2/mypattern
mkdir -p server1/ab_1234567_1/mypattern
mkdir -p server1/ab_1234569_3/mypattern


create some files

Code:
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


Code:
cd server1
du -k


4       ./ab_1234568_2/mypattern
8       ./ab_1234568_2
16      ./ab_1234567_2/mypattern
20      ./ab_1234567_2
16      ./ab_1234569_2/mypattern
20      ./ab_1234569_2
16      ./ab_1234567_1/mypattern
20      ./ab_1234567_1
16      ./ab_1234569_3/mypattern
20      ./ab_1234569_3
92      .

is there any chances can get the below result something like this?

32 1234567/mypattern
4 1234568/mypattern
36 1234569/mypattern

sum of all color red
sum of all color green
sum of all color blue

really appreciate your help

Last edited by lxdorney; 02-09-2013 at 07:35 AM..
Sponsored Links
    #6  
Old 02-09-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 25 July 2014, 9:30 AM EDT
Posts: 1,038
Thanks: 62
Thanked 216 Times in 204 Posts
I did assume that you have the information stored in file server1
You can use this

Code:
du -k | awk -F"[ _]+" '$0~p {a[$3]+=$1} END {for (i in a) print a[i]"k "i"/"p}' p="mypattern" > result.txt


Code:
cat result.txt
32k 1234567/mypattern
4k 1234568/mypattern
32k 1234569/mypattern

Also added a + since du seems to have many space as separator.
Sponsored Links
    #7  
Old 02-09-2013
lxdorney lxdorney is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 25 October 2013, 5:51 AM EDT
Posts: 14
Thanks: 4
Thanked 0 Times in 0 Posts
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
1. find to $d_path with exact $pattern to all directory expression something like this 'ab_[0-9]\{6,\}\_[0-9]\{0,\}'
2. grep my $pattern to all directory found my expression. ei ab_1234567_9 or ab_12345673334_96
3. du -k to all $pattern that was found in expression and sum them all $pattern belong to same expression.
4. 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.


Code:
ls
result.txt  server1  server2  test.sh


Code:
pwd
/root


Code:
du -k | awk -F"[ _]+" '$0~p {a[$3]+=$1} END {for (i in  a) print a[i]"k "i"/"p}' p="mypattern" > result.txt


Code:
cat  result.txt
36k 2/mypattern/mypattern
16k 3/mypattern/mypattern
16k 1/mypattern/mypattern


Code:
cp -r ab_1234567_2 ab_1234567_9


Code:
ls
ab_1234567_1  ab_1234567_2  ab_1234567_9  ab_1234568_2  ab_1234569_2   ab_1234569_3  result.txt


Code:
du -k | awk -F"[ _]+" '$0~p  {a[$3]+=$1} END {for (i in a) print a[i]"k "i"/"p}' p="mypattern" >  result.txt


Code:
cat result.txt
36k 2/mypattern/mypattern
16k 3/mypattern/mypattern
16k 1/mypattern/mypattern
16k 9/mypattern/mypattern


Last edited by lxdorney; 02-10-2013 at 12:19 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular expression for finding OCR mistakes. gencon UNIX for Advanced & Expert Users 2 05-17-2012 04:08 PM
using regular expression for directories in find command vinothbabu12 UNIX for Dummies Questions & Answers 3 09-14-2009 08:34 AM
Script for parsing directories one level and finding directories older than n days ejianu Shell Programming and Scripting 5 08-26-2009 11:24 AM
Create directories with regular expression mahendrt UNIX for Dummies Questions & Answers 13 09-03-2008 09:46 AM
Expression for Finding Multiple Directories..?? meskue Shell Programming and Scripting 3 07-10-2006 03:04 PM



All times are GMT -4. The time now is 12:25 PM.