-1

Lets say I have a file by name searchfiles.txt with file names in it:

2345098.txt
2345099.txt
2345100.txt

and so on (100 file entries)

Now I want to list out or display all the file names that match with the filesystem files by passing above (searchfiles.txt) as input/argument.

How can I achieve this in Linux?

terdon
  • 242,166
satya
  • 3
  • 1
    Please clarify what you are looking for as "match with the filesystem files" is ambiguous. Same directory or not, Moreover, your first comment to a reply adds a new feature that you do not state in your question. – jlliagre Nov 08 '16 at 10:50
  • It is same dir and please include my comment to the reply – satya Nov 08 '16 at 10:55
  • 1
    That's the other way around. When you want to clarify your question, you edit it and modify/add what is needed. And by the way, feel free to accept the reply that best answer to your question, if any. – jlliagre Nov 08 '16 at 11:36

4 Answers4

0
while read -r file;do [ -f "$file" ] && echo "$file";done <searchfiles.txt
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
  • Thanks @IporSircer for your prompt response .What if (searchfiles.txt) contains partial file names like 2345098* where as complete file name is 2345098_gvhghghjgjh.txt – satya Nov 08 '16 at 10:46
  • @satya, at that point the only sensible thing to do is, ask "Where did this searchfiles.txt come from?" and "How can we solve the original problem in a more sensible fashion, rather than pretending that filenames can't contain newlines or special characters?" – Wildcard Nov 08 '16 at 11:00
  • Hi @IporSircer
    Thanks for your quick response. It solved my first(actual or original)problem.
    – satya Nov 08 '16 at 11:00
  • If so, @satya, then please use the checkmark to indicate that this Question is Answered. Thank you! – Jeff Schaller Nov 08 '16 at 12:02
0

What about the locate command?

#!/bin/bash
cat list | while read line; do
locate $line;
done

Also, you need to remember to updatedb before running the locate.

Details from man:

DESCRIPTION locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line

micjan
  • 73
0

If looking in the same directory or in case full paths are given in searchFiles.txt , you can try this

ls `cat searchfiles.txt` 2> /dev/null

It will ask ls to list all files that have the same name as the ones in the searchfiles.txt.

ls will only list those files that really exists, and issue messages for those which are missing. Redirecting error to /dev/null will ensure that error messages for missing files are not printed.

amisax
  • 3,025
  • Thanks @AmitKumar It solved my additinal question asked "What if (searchfiles.txt) contains partial file names like 2345098* where as complete file name is 2345098_gvhghghjgjh.txt" – satya Nov 08 '16 at 11:02
0

Assuming no special characters in the file names, you can use ls and comm:

ls | comm -1 -2 - searchfiles.txt

However, be aware this is inherently fragile—don't use this in a production script.

See:

Wildcard
  • 36,499
  • 1
    comm needs its inputs sorted ..while ls may do that out of the box, there's no guarantee in regards to searchfiles.txt – iruvar Nov 08 '16 at 17:44