Compare files and extract matches on linux

Asked 2 years ago, Updated 2 years ago, 45 views

There is a text file (1) with the name of a particular file and a directory (2) that contains the actual (large) files.

I would like to extract only files from (2) that match the names of the files listed in (1).
How do I type the command on linux to do this?Please let me know.I'm sorry that Japanese is difficult to understand, thank you for your cooperation.

For example,
filenames.txt

file1    
file3
file5
file8
file10
file11

/usr/local/actualfilesdir/

file1file7
file2 file8
file3file9
file4 file10
file5file11
file6file12

filenames.txt to actualfilesdir/

file1    
file3
file5
file8
file10
file11

You want to extract only .

linux

2022-09-30 16:50

4 Answers

Essentially, we understand that what the questioner wants to do is to extract the commonality between the list of filenames written in the file (not including the directory portion) and the filenames of the files actually contained in a particular directory.

In implementing this, @nekketsuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuSpecifically,

  • File name does not contain line breaks

I believe that is necessary.

On the other hand, if this has been achieved, I think we can use grep-Fxf, which is an idiom for calculating the common part of a row in the unix system.specifically:

 (cd/usr/local/actualfilesdir/&ls-1) | grep-Fxf filename.txt

The above provides a list of common file names, so if necessary, we can do various things by turning the for statement.


2022-09-30 16:50

If I were myself,

  • The filename does not contain a new line.
  • The filename does not contain a double quotation.
  • filenames.txt has fewer files than actualfilesdir has.

If this is the case, the shell script continues to follow as follows:

# For example, if you want to echo the extracted filename,
cat filenames.txt | xargs-I{}sh-c' [-e"/usr/local/actualfilesdir/{}]&echo{}'

However, if the follow-up processing is complicated, write the program in a different programming language instead of a shell script.This is because I find it difficult to maintain complicated actions in the shell script.


2022-09-30 16:50

Whether it's efficient or not, separate explanation:

${cat filename.txt;ls/usr/local/actualfilesdir;}|sort|uniq-d

That 2 (other than No such file or directory of ls is hidden):

$(cd/usr/local/actualfilesdir&xargsls-12>/dev/null)<filenames.txt

Prerequisites:

  • The filename does not contain a new line.
  • filenames.txt does not contain duplicate names.


2022-09-30 16:50

Is this the image?

$pwd
/tmp/work

$ ll
total 28K
-rw-rw-r -- 1 genzouw wheel 29 1908:48 1.txt
-rw-rw-r -- 1 genzouw wheel 29 1908:482.txt
-rw-rw-r -- 1 genzouw wheel 29 1908:483.txt
-rw-rw-r -- 1 genzouw wheel 29 1908:484.txt
-rw-rw-r -- 1 genzouw wheel 29 1908:485.txt
-rw-rw-r -- 1 genzouw wheel 59 1908:5011.txt
-rw-rw-r -- 1 genzouw wheel 189 1908:48 list.txt

$ catlist.txt
1.txt
3.txt
5.txt

$ ls/tmp/work | grep-w-f list.txt
1.txt
3.txt
5.txt


2022-09-30 16:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.