[SOLVED] Find missing filenames from a list recursively
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have researched how to find missing filenames from a list recursively and I am close but not there completely.
I have files on one server that I need to verify exist on another server. I first made a list of the files with
Code:
ls > filelist
on one server. Copied that list to the other server for auditing.
I then executed script on server to be audited:
Code:
while IFS= read -r name; do
[ -n "$(find / -name "$name" -print | head -n 1)" ] || printf '%s\n' "$name"
done < filelist
This printed out a list of missing files to the screen which was partially correct. The issue is files with special characters and spaces in their names. They show as missing. If I manually change files such as
Code:
[this] file name
to
Code:
\[this\]\ file\ name
, then it works properly. I tried
Code:
' '
or
Code:
" "
but that didn't seem to work.
How do I print a list of filenames from the originating server that contain the backslashes in files for special characters and spaces? I found
Code:
ls -b
but that only shows
Code:
\
for the spaces not special characters.
Or is there a better way of doing this all together? Thanks
Thanks for the replies. The reason they are in multiple locations on the other server is they are categorized. I am just looking for a quick way to determine if they all made it to the archive server.
Writing something like "I tried wrapping with single and double quotes but find wouldn't locate them." would have been way clearer.
The reason that doesn't work is similar to the reason brackets aren't working - as per "man find", the -name argument accepts a pattern, where '*', '?', and '[]' are metacharacters. (And the quotes were being treated as literals.)
Since there appears to be no way to pass in a fixed string instead of a pattern, those characters need escaping, so your find command probably wants to be:
Note that the issue with the previous sed was actually the "$"name part -i.e. [[:punct:]] would work, but no point escaping more characters than necessary.
Writing something like "I tried wrapping with single and double quotes but find wouldn't locate them." would have been way clearer.
The reason that doesn't work is similar to the reason brackets aren't working - as per "man find", the -name argument accepts a pattern, where '*', '?', and '[]' are metacharacters. (And the quotes were being treated as literals.)
Since there appears to be no way to pass in a fixed string instead of a pattern, those characters need escaping, so your find command probably wants to be:
Note that the issue with the previous sed was actually the "$"name part - i.e. [[:punct:]] would work, but no point escaping more characters than necessary.
Thanks for the reply and patience. I apologize for my difficult to interpret post. I attempted to feed my list into the command you provided show below:
. Unfortunately no luck, it did not display any results at all. I know some of the file names in the list are not present on the server. I must be still missing something.
uses comm to get missing files between two files of 'like' strings. you should be able to use it direct one to the one server path to create the one file, then the other to another to create its list of files, then it spits it out in the end.
just mod it to get file names nd creates a list of the two seperate servers in the first two functions, then mod the other two to work. as you can see i did that in July so my memory of if is scratchy.
But i know this would work. just skim / read through these two links and hopefully you'll see what this script is basically doing to get missing files between two list of files.
The extra quotes around $name are not needed as $() allows for the nesting of itself and quotes. Also, I tested on my computer without issue, so unless you have other types of filenames
I do not see why it would not return what you needed.
Perhaps you could give an example of a filename that was not returned so we could check?
The extra quotes around $name are not needed as $() allows for the nesting of itself and quotes.
Quote:
Originally Posted by MadeInGermany
The quotes around $name are needed but the two quotes in sequence inhibit it.
Being more awake now, I would agree that $() removes the need to double the quotes, and a single set is usually required/recommended for safety.
However, it's working the same way for all three variations in my test i.e. $name and "$name" and ""$name"" are all giving the expected results.
It also works with """"""$name which is rather odd, so I went and checked the docs on <<< and they state:
Quote:
Originally Posted by Bash Reference
...undergoes tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion and word splitting are not performed...
So I guess that explains why they all behave the same.
Quote:
Originally Posted by MadeInGermany
The -print -prune is an attempt to make it faster.
My reading of the find manpage is that -print is not necessary, because it's automatically added, unless a conflicting param is used.
With regards to -prune, that raises the question of what directory behaviour should be - currently if there is a directory matching a line in the filelist then it is counted as being present, but perhaps it should explicitly be looking for files, in which case -type f should be added.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.