LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-22-2012, 07:51 AM   #1
tjcarol
LQ Newbie
 
Registered: Apr 2004
Location: Toronto CA
Distribution: fedora
Posts: 19

Rep: Reputation: 0
use the ouput from a textfile as input for find command


is there any way to use the output from a text file as the input for the find command?
something like cat list-of-files.txt | `find /data/* -name "list-of-files.txt"`
 
Old 01-22-2012, 08:22 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 669

Rep: Reputation: 94
Code:
for i in $(cat list-of-files);
do
find /data/ -name $i
done
Not the perfect solution but it should work

Last edited by ozanbaba; 01-22-2012 at 08:24 AM.
 
Old 01-22-2012, 09:33 AM   #3
tjcarol
LQ Newbie
 
Registered: Apr 2004
Location: Toronto CA
Distribution: fedora
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks Ozan,
That kinda worked but kept running in an endless loop.
Also it displayed a lot of irrelevant files.
 
Old 01-22-2012, 09:37 AM   #4
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 669

Rep: Reputation: 94
Well, maybe there's a cleaner way to go with xargs but xargs is all magic to me.
 
Old 01-22-2012, 09:46 AM   #5
tjcarol
LQ Newbie
 
Registered: Apr 2004
Location: Toronto CA
Distribution: fedora
Posts: 19

Original Poster
Rep: Reputation: 0
Yes I tried using xargs but like you say "It's all magic to me"
 
Old 01-22-2012, 11:38 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Running a find command for each file in a list sounds very expensive. Consider running find to produce a file of all files, and then use grep to search the files.
Code:
find /data/ -type f >full_list
grep -f files.txt full_list
 
Old 01-23-2012, 01:23 PM   #7
tjcarol
LQ Newbie
 
Registered: Apr 2004
Location: Toronto CA
Distribution: fedora
Posts: 19

Original Poster
Rep: Reputation: 0
Can I rephrase my question? I'm not trying to read what's in the files. I'm trying to copy them. I want to do something like this.
cat list-of files-on-archive-server.txt | find files-on-archive-server.txt | cp files-on-archive-server to production-server.

The find and copy part would be like this

find . -iname "*filename*" -exec cp {} server/data/found_files \;

But I cannot figure out how to use the output from cat as the input for find.
Any help would be greatly appreciated.
 
Old 01-23-2012, 04:04 PM   #8
tjcarol
LQ Newbie
 
Registered: Apr 2004
Location: Toronto CA
Distribution: fedora
Posts: 19

Original Poster
Rep: Reputation: 0
Actually this worked, at least a test on my laptop did.

for i in $(cat find-list.txt);
do
find /home/tim/Documents/ -name "$i" -exec cp {} -a /home/tim/Desktop \;
done
 
Old 01-23-2012, 05:46 PM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
From your initial description, it sounded like you had a list of file names, but not the full pathnames. If the list also has their locations, you don't need to find them

One thing you might need to do is use the `tr' command to make it easier to handle white space and "evil" characters.
Code:
cat filelist.txt | tr '\n' '\0' |xargs -0 mv -t /target/directory/
This is similar to using find with the "-print0" option, piped to xargs with a correslonding "-0" option. The null character is used to separate arguments. The -t option for "mv" allows the target of the "mv" command in the front of the arguments, since xargs adds the source arguments at the end.

Look at the xargs options to limit how many arguments to handle at once. If your file list is too long you may get an out of memory error from bash.

On more thing to consider is if you may find a file whose name begins with a dash. If that is possible, end the command with two dashes.
 
  


Reply

Tags
cat, find


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to find a word in a textfile starting from the BOTTOM of the textfile ? markraem Linux - Software 3 02-08-2010 06:12 AM
how to redirect the output of find command to input of egrep babu198649 Linux - Newbie 2 12-27-2007 03:50 AM
/etc/mtab input/ouput error cecollinsjr Fedora 1 04-14-2006 02:36 PM
set the ouput of a command to a variable true_atlantis Linux - Newbie 2 03-23-2006 03:30 PM
C prog..except input ,write to textfile?? kato678 Programming 1 02-21-2002 01:09 AM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration