Shell script to find the GB files in /tmp directory in remote server

Hi,
i need help on shell scripting.

Main intention of the script is
step 1: ssh to remote server
Step 2: cd /tmp in remote server
Step 3: in tmp i want to grep only files and directories which are in GB sizes

All the servers list file is - tmpsrv.txt

 vi tmpsrv.txt
 in001srv1
in001srv2
in001srv3
-
-
in001srv15

As of now i prepared the script as follows

 #!/bin/bash
while read line
do
 ssh -n $line "cd /tmp; du -sh * | awk '{ $1 ~ /[0-9]G }' "
done << tmpsrv.txt
 

/tmp sample data is

 1G     xxxx
2.2M   cdj
4.3G   jkth
5.2G   oiuf

Error :::
if i run my above code, i am getting the error like

  
 awk:  ~ [0-9]G/ 
awk:  ^ syntax error
awk:  ~ [0-9]G/ 
awk:        ^ syntax error
awk:  ~ [0-9]G/ 
awk:  ^ syntax error
  
 

but if i run the command alone du -sh * | awk '{ $1 ~ /[0-9]G } , i am getting the result as expected.

please guide me to correct the code.

Hello kumar85shiv,

Could you please change code awk '{ $1 ~ /[0-9]G }' " to awk '{if($1 ~ /[0-9]G/){print} }' " and let us know if this helps. Here I am considering that your ssh is working as expected.

Thanks,
R. Singh

Hi

I changed as you suggested, but its giving the same error.

 awk: { ~ [0-9]G/ }
awk:   ^ syntax error
awk: { ~ [0-9]G/ }
awk:         ^ syntax error
awk: { ~ [0-9]G/ }
awk:   ^ syntax error
awk: { ~ [0-9]G/ }
awk:         ^ syntax error
awk: { ~ [0-9]G/ }
awk:   ^ syntax error

---------- Post updated at 12:29 AM ---------- Previous update was at 12:27 AM ----------

please find the below error

 awk: { ~ /[0-9]G/ }
awk:   ^ syntax error
awk: { ~ /[0-9]G/ }
awk:   ^ syntax error
awk: { ~ /[0-9]G/ }
awk:   ^ syntax error
awk: { ~ /[0-9]G/ }
awk:   ^ syntax error

hi ,

can you try like awk '\$1 ~/[0-9]G/ { print $0 }'

venky

Hi Venky,

I changed as you suggested.

But, there is no out put from the script.

here with -x option

 bash -x kacey.sh 
 + read line
+ ssh -n in001srv1 'cd /tmp; du -sh * | awk '\''{ $1~ /[0-9]G/ }'\'' '
+ read line
+ ssh -n in001srv2 'cd /tmp; du -sh * | awk '\''{ $1~ /[0-9]G/ }'\'' '
+ read line
+ ssh -n in001srv3 'cd /tmp; du -sh * | awk '\''{ $1~ /[0-9]G/ }'\'' '
+ read line
+ ssh -n in001srv4 'cd /tmp; du -sh * | awk '\''{ $1~ /[0-9]G/ }'\'' '
+ read line

 

---------- Post updated at 12:49 AM ---------- Previous update was at 12:46 AM ----------

 awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                      ^ syntax error
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                        ^ unterminated regexp
awk: cmd. line:1: $1~ /[0-9]G/ { print ./kacey.sh }
awk: cmd. line:1:                                  ^ unexpected newline or end of string
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                      ^ syntax error
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                        ^ unterminated regexp
awk: cmd. line:1: $1~ /[0-9]G/ { print ./kacey.sh }
awk: cmd. line:1:                                  ^ unexpected newline or end of string
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                      ^ syntax error
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                        ^ unterminated regexp
awk: cmd. line:1: $1~ /[0-9]G/ { print ./kacey.sh }
awk: cmd. line:1:                                  ^ unexpected newline or end of string
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                      ^ syntax error
awk: $1~ /[0-9]G/ { print ./kacey.sh }
awk:                        ^ unterminated regexp
awk: cmd. line:1: $1~ /[0-9]G/ { print ./kacey.sh }
awk: cmd. line:1:                                  ^ unexpected newline or end of string

Hi,

please try

ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print $0}'"

venky

Hi venky ,

I changed the script as like this

 #!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print $0}'"
done < tmpsrv.txt

 

but getting the error like below.

 ./kacey.sh}
awk:                     ^ syntax error
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                       ^ unterminated regexp
awk: cmd. line:1: $1 ~ /[0-9]G/{print ./kacey.sh}
awk: cmd. line:1:                                ^ unexpected newline or end of string
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                     ^ syntax error
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                       ^ unterminated regexp
awk: cmd. line:1: $1 ~ /[0-9]G/{print ./kacey.sh}
awk: cmd. line:1:                                ^ unexpected newline or end of string
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                     ^ syntax error
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                       ^ unterminated regexp
awk: cmd. line:1: $1 ~ /[0-9]G/{print ./kacey.sh}
awk: cmd. line:1:                                ^ unexpected newline or end of string
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                     ^ syntax error
awk: $1 ~ /[0-9]G/{print ./kacey.sh}
awk:                       ^ unterminated regexp
awk: cmd. line:1: $1 ~ /[0-9]G/{print ./kacey.sh}
awk: cmd. line:1:                                ^ unexpected newline or end of string

  
 

Hello kumar85shiv,

Could you please try my suggestion, I had updated it immediately.

Hello venky,

I don't think so that escaping $ will be helping here.

Thanks,
R. Singh

hi

i am also a newbie and its working on my AIX
may be guru's someone can give you better suggestion.

venky

Hi Ravinder,

As per your advice I changed the script as like

 #!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '{if($1 ~ /[0-9]G/){print} }' "
done < tmpsrv.txt

but still getting the error

 awk: {if( ~ /[0-9]G/){print} }
awk:      ^ syntax error
awk: {if( ~ /[0-9]G/){print} }
awk:      ^ syntax error
awk: {if( ~ /[0-9]G/){print} }
awk:      ^ syntax error
awk: {if( ~ /[0-9]G/){print} }
awk:      ^ syntax error

hi

Ravinder,

above code i posted works with me perfectly, as am not more exp user please correct me if am wrong

venky

Venky almost had it right...

#!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print $0}'"
done < tmpsrv.txt

will probably work if you also escape the $0 as in:

#!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print \$0}'"
done < tmpsrv.txt

(RavinderSingh13: Note that the single quotes don't escape the dollar signs in this awk script because the single quotes are inside double quotes.)

If that does work, it can be simplified to:

#!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /G/'"
done < tmpsrv.txt
1 Like

Hi Don Cragun,

yes i failed to look at that $0
Thank you for pointing the mistake sir

venky

Hi

As you suggested I changed the ssh line script as

 while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /G/'"
done < tmpsrv.txt

 

now not getting any out put.

 
bash -x kacey.sh 
+ read line
+ ssh -n in001srv1 'du -sh /tmp/* | awk '\''$1 ~ /[0-9]G/{print $0}'\'''
+ read line
+ ssh -n in001srv2 'du -sh /tmp/* | awk '\''$1 ~ /[0-9]G/{print $0}'\'''
+ read line
+ ssh -n in001srv3 'du -sh /tmp/* | awk '\''$1 ~ /[0-9]G/{print $0}'\'''
+ read line

 

Anyhow, could you please guide me to get the expected output without awk ...

hi ,

have you tried as suggested by Don

#!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print \$0}'"
done < tmpsrv.txt

venky

Are you absolutely sure the script "kacey.sh" that you trace above does contain the line ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /G/'" ?

What output do you get from:

#!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/*"
done < tmpsrv.txt

?

1 Like

Hi Finally I got it.

Thanks for your help.

 #!/bin/bash 
while read line
do
        ssh -n $line "du -sh /tmp/* | awk '\$1 ~ /[0-9]G/{print \$0}'"
done < tmpsrv.txt

worked for me .