Help with using grep command with copy command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with using grep command with copy command
# 1  
Old 02-10-2011
Help with using grep command with copy command

Hi, im taking an entry Unix class, and as part of my lab assignment I have to copy all files in the /home/david/lab3 directory that have the file extension .save to your lab3/temp directory. I'm having trouble getting the grep to do anything worth while

I've been trying to do:

cp /home/david/lab3 /home/user113/lab3 | grep '.save'

and havnt gotten it to work yet. Am I using the right context? I really am at a loss here. Also, when I try to run "grep '.save' in the /home/david/lab3 directory i dont get any output, and i have to ctrl+d to get back to a prompt. Any help would be appreciated, I have to finish this lab by 6:30pm EST.

edit: I'm using the Bourne shell.
# 2  
Old 02-10-2011
Code:
cd /home/david/lab3
for i in ls *.save
do
cp $i /home/user113/lab3
done

# 3  
Old 02-10-2011
this should work:

cp `ls /home/david/lab3/*.save` /home/david/lab3/temp
# 4  
Old 02-10-2011
Code:
I get an error:


[user113@vbitblnx1 lab3]$ ls
my.names  mynames.save  names  names.020807  names1  names.sav  names.save  pass
[user113@vbitblnx1 lab3]$ cp 'ls /home/david/lab3/*.save' /home/user113/temp
cp: cannot stat `ls /home/david/lab3/*.save': No such file or directory

---------- Post updated at 02:29 PM ---------- Previous update was at 02:23 PM ----------

Okay i think i got it
pwd
/home/david/lab3
cp *.save /home/user113/lab3 works for me
# 5  
Old 02-10-2011
grep matches regular expressions. The regexp for a string (filename), followed by a dot, followed by another string (extension) would be something like: grep '.*\.save$'

However you don't need to use grep at all; It can be done with a single command, using wildcards:

Code:
cp /your/path/*.save /destination


EDIT: Oh, well... my reply was too late. Smilie Anyway, glad you figured it out by yourself. Smilie
This User Gave Thanks to verdepollo For This Post:
# 6  
Old 02-10-2011
Quote:
Originally Posted by verdepollo
grep matches regular expressions. The regexp for a string (filename), followed by a dot, followed by another string (extension) would be something like: grep '.*\.save$'

However you don't need to use grep at all; It can be done with a single command, using wildcards:

Code:
cp /your/path/*.save /destination

yep, thats exactly what i did. Thanks a bunch guys.
# 7  
Old 02-10-2011
HTML Code:
cd /home/david/lab3
for i in ls *.save
do
cp $i /home/user113/lab3
done
^^^ above will also work if you modify the 1st line by adding 'tics':
for i in `ls *.save`

Cheers
Momin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy command

Hi , I am trying to take a backup of file before overwriting it with cp command, I am using the command cp -b. -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 a -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 b cp -b a b -rw-rw-r-- 1 autoengine murex 0 Jan 22 07:08 a -rw-rw-r-- 1... (1 Reply)
Discussion started by: Raj999
1 Replies

2. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

3. Shell Programming and Scripting

Copy command does not work

I am new to this forum. I have a script which randomly throws error.Following are steps followed in this script: Generate Term file Remove previous term and rpt files from utility directory. copy term file to utility directory call sql to generate rpt file using term file as input copy the... (4 Replies)
Discussion started by: ann15
4 Replies

4. Shell Programming and Scripting

Help with copy command

Hello, I have a directory in which I have files as follows CRDT.csv CRDT.csv.1 CRDT.csv.2 .... CRDT.csv.n I would like to copy it over to another directory as crdt_lon.csv crdt_lon.csv.1 crdt_lon.csv.2 .... crdt_lon.csv.n I am looking for a one line command but I am... (5 Replies)
Discussion started by: srattani
5 Replies

5. Shell Programming and Scripting

grep date with copy command

how can i copy only those files with creation date less then 24 hours. (1 Reply)
Discussion started by: Danish Shakil
1 Replies

6. Shell Programming and Scripting

can anyone help with shell script command about searching word with grep command?

i want to search in the current directory all the files that contain one word for example "hello" i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies

7. UNIX for Dummies Questions & Answers

Copy a command string from the command line

How can we copy a command string from a previous command line and paste it into the cursor position on the current command line? I know that ^c will not work as the shell will interpret as an interrupt signal. Thanks, (1 Reply)
Discussion started by: Pouchie1
1 Replies

8. Shell Programming and Scripting

Need help in Copy Command

i need to copy some files from a directory and move the copy files to some destination with the extension .dat Can any one help on this (5 Replies)
Discussion started by: ranga27
5 Replies

9. Linux

copy command

sir can any body tell me how i can copy files like copy c:\abc\pqr\mr.txt c:\windows\my documents\this.txt i need command in linux like this really i am a new in linux that is why simple questions alson can any body explain me how i get current directory tree or path in windows... (2 Replies)
Discussion started by: sadiquep
2 Replies

10. Shell Programming and Scripting

copy command

what command would i wrote to copy files from $folder1 to $folder2 ???? (1 Reply)
Discussion started by: perleo
1 Replies
Login or Register to Ask a Question