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
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,
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.
If I were myself,
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.
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:
filenames.txt
does not contain duplicate names.
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
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.