Visit Our UNIX and Linux User Community


Finding directories with expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding directories with expression
# 1  
Old 02-08-2013
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
# 2  
Old 02-08-2013
Code:
find * -type d -name mypattern | xargs -r du -sk | while read k n
  do
    $(( sum += k ))
  done
echo $sum

# 3  
Old 02-09-2013
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 03:43 AM.. Reason: Additional code tags
# 4  
Old 02-09-2013
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

# 5  
Old 02-09-2013
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 08:35 AM..
# 6  
Old 02-09-2013
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.
# 7  
Old 02-09-2013
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 01:19 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #279
Difficulty: Easy
A microkernel is the near-minimum amount of software that can provide the mechanisms needed to implement an operating system (OS). These mechanisms include low-level address space management, thread management, and inter-process communication (IPC).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

2. UNIX for Advanced & Expert Users

Regular expression for finding OCR mistakes.

I have a large file of plain text, created using some OCR software. Some words have inevitably been got wrong. I've been trying to create grep or sed, etc., regular expressions to find them - but haven't quite managed to get it right. Here's what I'm trying to achieve: Output all lines which... (2 Replies)
Discussion started by: gencon
2 Replies

3. UNIX for Dummies Questions & Answers

Finding multiply directories

I have multiply directories scattered throughout my system that end in 2011. Example: one_2011 two_2011 three_2011 etc.... I'm trying to find all of these directories but coming up short. I tried find / -type d -name *2011 > example Any suggestions? I already searched in the... (13 Replies)
Discussion started by: shorty
13 Replies

4. UNIX for Dummies Questions & Answers

Finding size of all directories

Alright so I've tried a couple different things that at first glance, looked like they worked. find . -maxdepth 5 -type d -daystart -mtime 1 | xargs du -h Which seems to ignore the previous commands such as depth and modified time. find .. -maxdepth 2 -type d -daystart -ctime 1 | xargs... (8 Replies)
Discussion started by: Aussiemick
8 Replies

5. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies

6. Shell Programming and Scripting

Script for parsing directories one level and finding directories older than n days

Hello all, Here's the deal...I have one directory with many subdirs and files. What I want to find out is who is keeping old files and directories...say files and dirs that they didn't use since a number of n days, only one level under the initial dir. Output to a file. A script for... (5 Replies)
Discussion started by: ejianu
5 Replies

7. Shell Programming and Scripting

finding correct directories

I have directories like V00R01,V00R02,V01R01,V01R02 in a directory where V is version and R is a release. basically I need to set base directory and current directory. Under a version there can be any number of releases and there can be number of versions also. V00R01...V00R50..so on also,... (2 Replies)
Discussion started by: vjasai
2 Replies

8. UNIX for Dummies Questions & Answers

Create directories with regular expression

Hi guys, can any one tell me how to create directories using regular expression? Let's say that I need to create directories test01, test02, test03.... test10. Can it be done using any regular expression? thanks. (13 Replies)
Discussion started by: mahendrt
13 Replies

9. Shell Programming and Scripting

Expression for Finding Multiple Directories..??

I am writing a shell script to search for previous versions of an application...the application is called TAU and basically i want to search the users home directory and /Applications for any instances of a "TAU" folder.. If found i want to give the user the option to remove the old folders and if... (3 Replies)
Discussion started by: meskue
3 Replies

10. UNIX for Dummies Questions & Answers

finding directories in UNIX

I am accessing a UNIX server via FTP. I want to retieve a file in a directory. What is the UNIX command that I need to view and retrieve files from a directory? (1 Reply)
Discussion started by: yodaddy
1 Replies

Featured Tech Videos