Solved

ls grep concatenate shell commands , cp etc

Posted on 2011-09-29
15
1,305 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:dplinnane
  • 7
  • 3
  • 3
  • +2
15 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 36861128
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36861198
Sorry, the command should be:

cp -R *.sql /dir2

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 36861955
try this

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

Author Comment

by:dplinnane
ID: 36864990
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
 

Author Comment

by:dplinnane
ID: 36866065
$ 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
 

Author Comment

by:dplinnane
ID: 36867154
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36867463
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dplinnane
ID: 36868375
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
 

Author Comment

by:dplinnane
ID: 36875875
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
 
LVL 21

Expert Comment

by:Papertrip
ID: 36878466
for file in `ls | grep -E '\.pll$'`; do echo "rm -r $file;"  done 

Open in new window

0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36879240
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
 

Author Comment

by:dplinnane
ID: 36879990
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
 
LVL 21

Accepted Solution

by:
Papertrip earned 167 total points
ID: 36884851
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
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 167 total points
ID: 36894890
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
 

Author Comment

by:dplinnane
ID: 36913694
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now