The way I'd approach it is to get uuids from file first, then use find
awk '{print $1}' listfile.txt | while read fileName;do find /etc -name "$fileName*" -printf "%p FOUND\n" 2> /dev/null;done
For readabilty,
awk '{print $1}' listfile.txt | \
while read fileName;do \
find /etc -name "$fileName*" -printf "%p FOUND\n" 2> /dev/null;
done
Example with a list of files in /etc/
, looking for passwd, group,fstab, and THISDOESNTEXIST filenames.
$ awk '{print $1}' listfile.txt | while read fileName;do find /etc -name "$fileName*" -printf "%p FOUND\n" 2> /dev/null; done
/etc/pam.d/passwd FOUND
/etc/cron.daily/passwd FOUND
/etc/passwd FOUND
/etc/group FOUND
/etc/iproute2/group FOUND
/etc/fstab FOUND
Since you've mentioned the directory is flat,you could use the -printf "%f\n"
option to just print filename itself
What this doesn't do is to list missing files. find
's small disadvantage is that it doesn't tell you if it doesn't find a file, only when it matches something. What one could do , however , is to check the output - if the output is empty , then we have a file missing
awk '{print $1}' listfile.txt | while read fileName;do RESULT="$(find /etc -name "$fileName*" -printf "%p\n" 2> /dev/null )"; [ -z "$RESULT" ] && echo "$fileName not found" || echo "$fileName found" ;done
More readable:
awk '{print $1}' listfile.txt | \
while read fileName;do \
RESULT="$(find /etc -name "$fileName*" -printf "%p\n" 2> /dev/null )"; \
[ -z "$RESULT" ] && echo "$fileName not found" || \
echo "$fileName found"
done
And here's how it performs as a small script:
skolodya@ubuntu:$ ./listfiles.sh
passwd found
group found
fstab found
THISDONTEXIST not found
skolodya@ubuntu:$ cat listfiles.sh
#!/bin/bash
awk '{print $1}' listfile.txt | \
while read fileName;do \
RESULT="$(find /etc -name "$fileName*" -printf "%p\n" 2> /dev/null )"; \
[ -z "$RESULT" ] && echo "$fileName not found" || \
echo "$fileName found"
done
One could use stat
as alternative, since it's a flat directory, but the code bellow won't work recursively for subdirectories if you ever decide to add those:
$ awk '{print $1}' listfile.txt | while read fileName;do stat /etc/"$fileName"* 1> /dev/null ;done
stat: cannot stat ‘/etc/THISDONTEXIST*’: No such file or directory
If we take the stat
idea and run with it, we could use the exit code of stat as indication for whether a file exists or not. Effectivelly, we want to do this:
$ awk '{print $1}' listfile.txt | while read fileName;do if stat /etc/"$fileName"* &> /dev/null;then echo "$fileName found"; else echo "$fileName NOT found"; fi ;done
Sample run:
skolodya@ubuntu:$ awk '{print $1}' listfile.txt | \
> while read FILE; do
> if stat /etc/"$FILE" &> /dev/null ;then
> echo "$FILE found"
> else echo "$FILE NOT found"
> fi
> done
passwd found
group found
fstab found
THISDONTEXIST NOT found