Copy file after searching in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy file after searching in a directory
# 1  
Old 12-14-2010
Copy file after searching in a directory

Hi,

I am looking for an answer for following senario:

I have a text file (base.txt) which consist list of files to be searched like:
base.txt

abc.txt
def.txt
fgh.txt

Now i am going to search all the listed files in another directory after reading them one by one, once i found the file in the directory then i want to copy it to another location.


Please suggest how i can fullfill this scenario.

Thanks all for your valuable suggestion.
# 2  
Old 12-14-2010
Code:
 
cat base.txt | while read file
do
   cp <location1>/$file <location2>/$file 2>/dev/null
done

# 3  
Old 12-14-2010
Quote:
Originally Posted by anurag.singh
Code:
 
cat base.txt | while read file
do
   cp <location1>/$file <location2>/$file 2>/dev/null
done

why exactly do you need a cat?
# 4  
Old 12-14-2010
To read list of files to be searched/copied in base.txt. No?
# 5  
Old 12-14-2010
What vgersh99 is getting at:

Code:
while read file
do
   cp <location1>/$file <location2>/$file 2>/dev/null
done < base.txt

(no cat)
# 6  
Old 12-14-2010
I see.. That's right. cat should be avoided. Tks !!
# 7  
Old 12-14-2010
Quote:
Originally Posted by anurag.singh
I see.. cat should be avoided.
I wouldn't go that far! Personally, I think this whole UUOC thing is over-stated. It would be better described as an "unnecessary use of cat" Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching File in Directory and all Subdirectory and Delete

Hi All, My directory structure is like Directory1 SubDirectory1 SubDirectory2 SubDirectory3 I have main directories and subdirectories underneath. I want to write a shell script where I will be passing file name as a parameter, Now I want to find all the files in Directory1... (19 Replies)
Discussion started by: John William
19 Replies

2. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

3. UNIX for Dummies Questions & Answers

Searching the file in a directory

Hi Folks, I am using the putty as I need to check the logs, My query is that I know the location of my logs ..that is cd /var /logs/abc.log so I can reach to this place and open the logs in putty, But what About if I do not the location only thing I know the name of the abc.log , and I have... (1 Reply)
Discussion started by: KAREENA18
1 Replies

4. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

5. Programming

how to copy file to a directory

Hello, I've been spending a lot of hours trying to imitate cp copying a file to a directory. cp I just can't seem to write to a specified directory, it only creates a copy on the current directory. any hints/tips will help! Thanks! here's the code i've been trying to manipulate: ... (1 Reply)
Discussion started by: l flipboi l
1 Replies

6. UNIX for Dummies Questions & Answers

How to copy a file to a directory?

Hello all, I've been researching this problem for days, and have gotten no luck . =/ How do you copy a file to another directory without being in the same directory as the file? So, for example, say I wanted to copy the file 'my.txt' that is in the directory ' /export/hom0/user/asdf ' to the... (9 Replies)
Discussion started by: kvnqiu
9 Replies

7. Shell Programming and Scripting

Shell script for searching a record,copy to a file and then delete it

Hi, I have a requirement in hand: I have a file with millions of records say file 1.I have another file, say file 2 which has 2000 records in it. The requirement is to read file2 , and remove the read record from file 1 and move i to a seperate file, file 3. For eg: Read file 2, get the... (5 Replies)
Discussion started by: kumara2010
5 Replies

8. UNIX for Dummies Questions & Answers

Searching directory for file that contains some text.

If I go into a directory and type in .. more * | grep foo I get the lines of text that contain foo in all of the files in the directory out of all of the files that are there. How do I make it so I can find out what the names of the files are that contain that text "foo"? By doing what I... (4 Replies)
Discussion started by: LordJezo
4 Replies

9. Shell Programming and Scripting

searching each file in a directory for text

what command can i use to search the files in a directory for a text. the output would list the files containing the text. ive tried this but it is not exactly what im looking to do: find . -name "*.xml" -exec agrep searchstring {} \; (2 Replies)
Discussion started by: jim majors
2 Replies

10. UNIX for Dummies Questions & Answers

Searching a file withoug leaving home directory!!

hello there ! I am in my current working directory, and i want search a file in "dev" directory which is under root(/), but i donot want leave my current working directory. is it possible to find file like that ? let us say i am in /home/bin/user/malik/abid directory and i want find a file... (2 Replies)
Discussion started by: abidmalik
2 Replies
Login or Register to Ask a Question