Hi Gurus,
I have a requests to find if all the file in the filelist exist in certain directory.
example:
my filelist
abc
def
ddd
cde
afg
how can I find these 5 files exists at director /home/abc
Thanks in advance
Hi Gurus,
I have a requests to find if all the file in the filelist exist in certain directory.
example:
my filelist
abc
def
ddd
cde
afg
how can I find these 5 files exists at director /home/abc
Thanks in advance
Is this homework? If not, what have you tried so far?
No, it is not a homework.
I tried list the file then send to tmp file, then using diff to compare. but it doesn't work
ls > tmp
diff tmp filelist
You could use a while
loop to read the file names from the file: filelist
and check each one in the dir: /home/abc
while read file
do
[ -e "/home/abc/${file}" ] && echo "File: $file does exists" || echo "File: $file does not exists"
done < filelist
#!/bin/ksh
d='/home/abc'
fileList='./filelist'
while read file
do
if [ -f "${d}/${file}" ]; then
echo "${file} exists"
else
echo "${file} does not exist"
fi
done < "${fileList}"
$ ls -1 /home/abc |grep -wf filelist
ddd
Those grep options cannot cope with whitespace nor characters that are special in BRE grammar (most notably, the dot). Instead of -w, I would suggest -xF.
Minor nit: -1 is redundant.
Regards,
Alister
The most math-like tool for this is comm, where you supply two sorted lists and it tells you who is in a only, who in b only, and who in both, but you can option to just get 1 or 2 columns, e.g., comm -23 for just a only items. The output has leading tabs, one for b only and two for both, unless you eliminate columns.
comm -3 <( cd /home/abc ; export LC_ALL=C ; ls | sort ) <( export LC_ALL=C ; sort filelist )