ls grep concatenate shell commands , cp etc

I have a few general questions. I searched for an hour but could not find what I was looking for.

I want to list all files that end in .sql   and I want to be able to concatenate a string to the output like so

ls | grep *.sql  

output
big.sql
samll.sql
middle.sql  

I want the output to be like so

cp /dir1/big.sql  /dir2/big.sql
cp /dir1/big.sql  /dir2/big.sql
etc

so how do I concatenat the "cp  /dir1/" output from ls grep "/dir2/"output from ls grep


Second question  
How can I copy all files of type .sql  to another directory dir2

cp -R | grep *.sql    /dir2            " Any ideas?"

Thanks
dplinnaneAsked:
Who is Participating?
 
PapertripCommented:
what does the -E mean
man grep
-E, --extended-regexp
              Interpret PATTERN as an extended regular  expression  (ERE,  see
              below).  (-E is specified by POSIX.)

Open in new window

However, for this particular instance, you can also use '-e' or just omit it all together, I included it in my original answer just to make clear we were evaluating a regular expression.  There are probably cases where -E is required as opposed to -e, but I just use 'egrep' instead, which is the equivalent of -E, so I never ran into that issue.  fgrep and egrep are deprecated, however they still exist for legacy uses, and if you grew up using egrep you probably still do as opposed to grep -E...  anyways on to the rest of the question...

what does \.     and $ mean
"\." means to treat the "." in the string as a literal character, and not part of the regex.  "$" means end of line.

Regex Tutorial
0
 
käµfm³d 👽Commented:
What is the difference between #1 and #2? I realize you want to concatenate the string to the found files, but it seems like both will ultimately end up doing the same operation.

For #2, you should be able to simply do:

cp -R *.sql

Open in new window

0
 
käµfm³d 👽Commented:
Sorry, the command should be:

cp -R *.sql /dir2

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
omarfaridCommented:
try this

for file in `ls | grep *.sql`
do
        cp /dir1/$file  /dir2/$file
done
0
 
dplinnaneAuthor Commented:
for file in `ls | grep *.sql`
do
        cp /dir1/$file  /dir2/$file
done

How would I echo this to the screen

echo "cp /dir1/$file  /dir2/$file"    

or write to a file

echo "cp /dir1/$file  /dir2/$file"  > test.txt

Also the cp -R is just an example

can I not to an ls and concatenate a string to the end of the result

ie ls  | grep *.sql   concatenaet any string here.  Will try you above suggestions shortly
0
 
dplinnaneAuthor Commented:
$ for file in `ls | grep *.pll`
> do
> echo "cp /dir1/$file  /dir2/$file"
> done

how can I run this or do I have to put it all on the one line

for file in `ls | grep *.pll` do echo "cp /dir1/$file  /dir2/$file"  done
0
 
dplinnaneAuthor Commented:
got it

for file in `ls | grep pll`; do echo "cp /dir1/$file  /dir2/$file";  done

but why does *.pll not work on aix and does on linux?
0
 
käµfm³d 👽Commented:
but why does *.pll not work on aix and does on linux?
They are two different operating systems. Even in the same operating system, you could have two different versions of the same program installed, and each did things in its own way  = )
0
 
dplinnaneAuthor Commented:
so  in linux
*.pll  is the same as pll on unix

How would one list all files with an extension

ls | grep *.*      linux
ls | grep  .        unix

any luck on

ls | grep *.*   concatenate a string?
0
 
dplinnaneAuthor Commented:
just removed .pll.sh files by mistake

using
for file in `ls | grep pll`; do rm -r $file;  done

how can you specify end in .pll   ?  , should have played it save and used

for file in `ls | grep pll`; do echo "rm -r $file;"  done
0
 
PapertripCommented:
for file in `ls | grep -E '\.pll$'`; do echo "rm -r $file;"  done 

Open in new window

0
 
PapertripCommented:
Woops I edited your syntax which had a typo with the last semi-colon, here is the correct version:
for file in `ls | grep -E '\.pll$'`; do echo "rm -r $file";  done 

Open in new window


0
 
dplinnaneAuthor Commented:
grep -E '\.pll$'

what does the -E mean
what does \.     and $ mean

Trying to learn so I don't have to bother you guys (-:
0
 
TintinCommented:
No need for ls, grep or a for loop.

for file in `ls | grep pll`; do echo "cp /dir1/$file  /dir2/$file";  done

Open in new window


is better written as

for file in *.pll
do
   echo "cp /dir1/$file  /dir2/$file"
done

Open in new window


or if you don't want the echo, simply

cp /dir/*.pll /dir2

Open in new window

0
 
dplinnaneAuthor Commented:
for file in `ls`; do echo "cp -R $dir  ./dir2/";  done
 
This works great except directories with spaces do not work as expected.  For example directory name  sales and returns

echoes
cp -R /dir1/sales  /dir2/
cp -R/dir1/and  /dir2/
cp -R /dir1/returns  /dir2/

I should get
cp -R /dir1/sales and returns  /dir2/

How do I stop directory names with spaces from being split?

I'll close the question after this (-: thanks all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.