LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   use the ouput from a textfile as input for find command (https://www.linuxquestions.org/questions/linux-general-1/use-the-ouput-from-a-textfile-as-input-for-find-command-925078/)

tjcarol 01-22-2012 07:51 AM

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"`

ozanbaba 01-22-2012 08:22 AM

Code:

for i in $(cat list-of-files);
do
find /data/ -name $i
done

Not the perfect solution but it should work

tjcarol 01-22-2012 09:33 AM

Thanks Ozan,
That kinda worked but kept running in an endless loop.
Also it displayed a lot of irrelevant files.

ozanbaba 01-22-2012 09:37 AM

Well, maybe there's a cleaner way to go with xargs but xargs is all magic to me.

tjcarol 01-22-2012 09:46 AM

Yes I tried using xargs but like you say "It's all magic to me"

jschiwal 01-22-2012 11:38 AM

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


tjcarol 01-23-2012 01:23 PM

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.

tjcarol 01-23-2012 04:04 PM

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

jschiwal 01-23-2012 05:46 PM

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.


All times are GMT -5. The time now is 07:02 AM.