Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix find: list of files from stdin

Tags:

find

bash

unix

I'm working in Linux & bash (or Cygwin & bash).

I have a huge--huge--directory structure, and I have to find a few needles in the haystack.

Specifically, I'm looking for these files (20 or so):

foo.c
bar.h
...
quux.txt

I know that they are in a subdirectory somewhere under ..

I know I can find any one of them with find . -name foo.c -print. This command takes a few minutes to execute.

How can I print the names of these files with their full directory name? I don't want to execute 20 separate finds--it will take too long.

Can I give find the list of files from stdin? From a file? Is there a different command that does what I want?

Do I have to first assemble a command line for find with -o using a loop or something?

like image 628
JXG Avatar asked May 31 '11 10:05

JXG


People also ask

How do I grep a list of files?

You can insert the output of a grep directly into a command line with command substitution. For example, to edit only the subset of files containing R6, you would type: % vi `grep -l R6 file1 file2 ...`

Can a tar command Use a standard input for its file list?

tar cannot read in file data from stdin for creating an archive, since then tar will have no way of knowing what a file is - where one begins or ends, what its path and metadata is.

How do I search for a file with a specific content in Linux?

You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.


1 Answers

If your directory structure is huge but not changing frequently, it is good to run

cd /to/root/of/the/files
find . -type f -print > ../LIST_OF_FILES.txt #and sometimes handy the next one too
find . -type d -print > ../LIST_OF_DIRS.txt

after it you can really FAST find anything (with grep, sed, etc..) and update the file-lists only when the tree is changed. (it is a simplified replacement if you don't have locate)

So,

grep '/foo.c$' LIST_OF_FILES.txt #list all foo.c in the tree..

When want find a list of files, you can try the following:

fgrep -f wanted_file_list.txt < LIST_OF_FILES.txt

or directly with the find command

find . type f -print | fgrep -f wanted_file_list.txt

the -f for fgrep mean - read patterns from the file, so you can easily grepping input for multiple patterns...

like image 197
jm666 Avatar answered Sep 16 '22 18:09

jm666