using linux cmds to match a list names against a bunch of files
I have a slight problem with matching file names.
I have one text file that contains some 160 names. I have a folder with 2000+ files. and some of them contains these 160 names. I am looking for a gr开发者_Python百科ep cmd that can take each name in the text file and try to match it to the contents of the folder.
I am trying to do this in perl, or just straight forward linux cmds, but neither has worked out very well for me because I am not familiar with either of them.
so for example: the text file contains
abc acc eee fff
and the folder will have abcXXX, accXXX, eeeXXX and fffXXX
I need to sort through the list and find out which one were missing.
thx Davy
If you search in the content of the files :
#!/bin/sh
for i in `cat files`
do
grep -R $i folder --color
done
and if you search in the filename of the files :
#!/bin/sh
for i in `cat files`
do
find . -name $i*
done
for file in $(< list);
do
[ ! -f ${file}xxx ] && echo "x: " ${file}xxx
done
list
is the file, containing the list of filenames "abc acc ...".
<
is redirection - so we read from the file 'list', the same as $(cat list)
. If the file isn't named 'list', just replace the name.
file
is declared in that statement and iteratively bound to all those entries in 'list'. Later it is used as ${file}.
[ ! -f ${file}xxx ]
is a test, whether a -f(ile) exists, for abc it searchs for a file abcxxx.
But ! negates the search, so if no such file exists, then echo ...
is called. "x: " is just a debug relict.
We can improve that part:
for file in $(< list);
do
[ -f ${file}xxx ] || echo ${file}xxx
done
instead of 'NOT x AND y' we can write 'x OR y' - the meaning is the same, just shorter: the file does exist or echo its name.
|| is short-circuit OR.
If you can arrange for the text file to have each name on a separate line then the following command will do what you need:
ls myfolder | grep -f mytextfile
One way to get each name on a separate line in the text file would be to edit a copy in vi
and issue the commands:
:%s/ /^V^M/g
:wq
(^V^M
means type "control-v" then "control-m")
精彩评论