1851730 Members
3406 Online
104062 Solutions
New Discussion

Re: script problem

 
SOLVED
Go to solution
Hari Prasad S R
Frequent Advisor

script problem

Hi all,

I am doing some scripting, to copy some files source to destination.

I have about 25 files in current location, I want to copy all the files which starts from the letter a* ,out of which i don't want to copy a particular file which starts ae*

What I have written this

ls –al a* | grep –v ae | awk ‘{print $9}’

this is will give me the output, but i want to know how to copy all these files (i mean which comes as output of awk command)
11 REPLIES 11
John Waller
Esteemed Contributor

Re: script problem

I personally would take a cheating way:

for a in `ls al a* | grep v ae | awk {print $9} `
do
cp $a /destination
done

Note the use of ` ` around your command.


Muthukumar_5
Honored Contributor

Re: script problem

You can do as,

for file in `ls -al a* | grep -Ev 'ae|total' | awk '{ print $9 }'`
do
cp -i ${file}
done

Note: Negate grep for total string for using ls -l

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script problem

I am sorry no need to negate with total string. Simply as,

for file in `ls -al a* | awk '!/ae/{ print $9 }'`
do
cp -i ${file}
done

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor
Solution

Re: script problem

ls -al a[!e]* | awk '{ print "cp "$9" /tmp" }' | ksh

It will copy all files to /tmp location. Change destination location accordingly.

hth.
Easy to suggest when don't know about the problem!
Hari Prasad S R
Frequent Advisor

Re: script problem

Hi Muthukumar,

why we need ksh at the end, i am doing this bash

thanks
RAC_1
Honored Contributor

Re: script problem

ls -al1 a[a-df-z]*

Will also do it.
There is no substitute to HARDWORK
Orhan Biyiklioglu
Respected Contributor

Re: script problem

You should also use shell wildcards to filter the files you want instead of invoking three commands (ls,grep and awk)
Also this eliminates the need for a loop.


Just use

cp /sourcedir/a[!e]* /destdir/

for local or

scp /sourcedir/a[!e]* remote_host:/destdir/

for remote copying.

hth

Hari Prasad S R
Frequent Advisor

Re: script problem

thanks Orhan Biyiklioglu ,

it worked for me
Muthukumar_5
Honored Contributor

Re: script problem

You can change ksh to bash to do that job.

ls -al a[!e]* | awk '{ print "cp "$9" /tmp" }' | bash

It will just work as a sub process to execute those copy operations.

ls -al a[!e]* | awk '{ print "cp "$9" /tmp" }'

will print the shell command that has to be executed. | bash will execute it.

hth.
Easy to suggest when don't know about the problem!
Hari Prasad S R
Frequent Advisor

Re: script problem

thanks all for the reply
Muthukumar_5
Honored Contributor

Re: script problem

Hi Hari Prasad,

Hope you got the solution.

I have a serious comment as,

I have assigned points to 1 of 63 responses to my questions. from your profile.

You assigned only one point to all these responses. Plz assign points to all responses so that you will get good responses.

Every one is sharing their valuable technical time with you PROMPTLY.

hth.
Easy to suggest when don't know about the problem!