Visit Our UNIX and Linux User Community


Read input txt while find copy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input txt while find copy
# 1  
Old 05-16-2012
Read input txt while find copy

I have a text file called file.txt which has a list of file as shown below that i need to find and copy to a particular location.

FILENAMES
Code:
skter.pdf
abcdf.sas
tereen.lst
abc12.txt

i am using following code and it never works however i try it.
Code:
cat file.txt | while read FILENAME;do find .  -name "$FILENAME" -type f -print -exec cp '{}' /testdir /; done;

Also point to consider while doing this is there is possibility of have 1 file in many directory in that case need to copy them to target folder like file1 file2

Can someone please give me a more better code.

Thank you

Last edited by Franklin52; 05-16-2012 at 06:57 AM.. Reason: Please use code tags
# 2  
Old 05-16-2012
Sooooo close!
Code:
cat file.txt | while read FILENAME;do find .  -name "$FILENAME" -type f -print -exec cp '{}' /testdir \; done;

---------- Post updated at 01:05 PM ---------- Previous update was at 01:01 PM ----------

As for your second problem (same file from different directories), here's one solution:
Code:
tar cf /testdir/archive.tar `cat file.txt | while read FILENAME;do find .  -name "$FILENAME" -type f -print;done `

archive.tar will then contain all the files you want. You can extract them (with relative path names) using
Code:
tar xf /testdir/archive.tar


Last edited by otheus; 05-17-2012 at 02:12 PM.. Reason: missing "done"
# 3  
Old 05-17-2012
Dear Otheus,
Thank you for your quik, solution but still it does not work and the code dose not seem complete when i execute them. it expects something more not quite sure what.
# 4  
Old 05-17-2012
Perhaps something like this:
Code:
find . $(sed 's/^/-name /; $!s/$/ -o /' infile) -type f .....

# 5  
Old 05-17-2012
I was missing "done" in my code. I Corrected the post. But Scrutinizer's post is also correct and more efficient. Both commands will fail, by the way, if there are too many files and the command line is too long. In that case you'd use xargs with my solution:
Code:
tar cf $BACKUP
cat file.txt | while read FILENAME;do find .  -name "$FILENAME" -type f -print;done |
{ read onefile; tar cf $BACKUP $onefile; xargs -r tar rf $BACKUP ; }

xargs tries to fill the command line with as many parameters as possible, invoking "tar rf $BACKUP " as a prefix to each command. If there are too many command-line parameters, it runs another invocation of the "tar rf $BACKUP" command with the rest of the parameters. The "r" option of tar adds to an existing tar file, so we first have to create it using the first file on the list. So we "read onefile" to get the first filename from the find operation. We then create the archive file using $onefile as the only argument. Now xargs can do its job properly.

Last edited by otheus; 05-17-2012 at 02:22 PM..

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

2. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

3. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

4. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

5. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

8. Shell Programming and Scripting

help copy *.txt to postgres

hi all, a have problem to load *.txt to postgres my database: id_list_ip (nextval) list_ip (varchar) txt file (list_ip.txt) hasilping_10.8.248.1 hasilping_119.110.112.226 hasilping_119.110.125.33 hasilping_125.22.1.25 hasilping_192.168.2.1 hasilping_202.73.96.70 script... (3 Replies)
Discussion started by: adi0926
3 Replies

9. Shell Programming and Scripting

sed to read x.txt and grep from y.txt

How would I write a command(s) to read from a file (list) that looks like this: 29847374384 and grep from a second file (list) that looks like this: 29847374384, jkdfkjdf,3833,ddd:confused: (1 Reply)
Discussion started by: smellylizzard
1 Replies

10. Shell Programming and Scripting

Read from fileList.txt, copy files from directory tree

Hi, hopefully this is a fairly simple Q&A. I have a clean file list of approximately 180 filenames with no directory or slashes in front of the filename nor any extension or dot ".". I would like to read from this list, find these files recursively down through directory trees, copy the files... (1 Reply)
Discussion started by: fxvisions
1 Replies

Featured Tech Videos