The officially official Devuan Forum!

You are not logged in.

#1 2022-10-14 22:27:46

Registered: 2022-01-09
Posts: 34  

[SOLVED] Locate cannot search for files with brackets?

This is driving me crazy. I am making a bash script that is given one or more files and will search with location for other locations that the same filename shows up. So I need to make this work given the whole filename. I intend to use this script as a custom Thunar action and it actually works fine for files that don't have square brackets in their name. But I have a file named:

5481[2].jpg (image).html

I want to use locate to search in what other locations it exists. Specifically

locate -b "(image).html"

will show the file, so the file is in my mlocate db. But trying to search with

locate -b "5481[2].jpg (image).html" 

returns zero results!

Even leaving the bash script aside for a while and simply opening a terminal and typing "locate" then the start of the filename and tab completing it, which escapes the filename with backslashes, still returns zero results while it works for any other file without square brackets.

What gives? What's so special about these square brackets that locate refuses to use them in a search expression? The same issue applies to all my files with square brackets in their filenames.

My only other option would be to find a way to turn a filename into a regex and then user the regex mode of locate. Is there a command that I can filter a filename through to turn it into a proper regex?

Thank you.


#2 2022-11-03 02:50:03

Registered: 2022-10-30
Posts: 228  

Re: [SOLVED] Locate cannot search for files with brackets?

very intersting indeed
I just tried it and mlocate too, same
why locate is not tremendously popular is beyond me
I think the original find command is maybe your only hope
I have patiently learned some find command, I do think it does take some application of concentration but I found it to be straightforward when I tried
I wish you the very best, it's a fine endeavour


#3 2022-11-03 05:37:49

Registered: 2022-10-26
Posts: 645  

Re: [SOLVED] Locate cannot search for files with brackets?

I am a user, not a programmer, for me it is "dancing with a tambourine". smile
"You can't use ] to mark the end of a range of characters. But if the range designator is preceded by /, then ] is the end of the range."
Example in the screenshot, after entering the first command with / nothing happens, but there is a file with square brackets next.
This property disappears after the computer is restarted.

The 1.txt file on the desktop was specially renamed to 1[1].txt and the example is exactly the same file name.

Last edited by aluma (2022-11-03 05:46:12)


#4 2022-11-03 05:54:19

From: Brisbane, Australia
Registered: 2019-07-18
Posts: 613  

Re: [SOLVED] Locate cannot search for files with brackets?

this may be off topic, bvut I did notice a message this morning with apt update, a

grep package went through and a notice was given, but I didn't read it all... But,

The note said something like "grep has changed the escape key.. backslash, do I want to continue or use the old/new version.

hth GW

pic from 1993, new guitar day.


#5 2022-11-03 12:56:07

Registered: 2022-01-09
Posts: 34  

Re: [SOLVED] Locate cannot search for files with brackets?

Thank you aluma! That's the solution to my problem. I'll need to find a way to do it programmaticaly with bash though since I'm just passing filenames to this bash script that internally calls locate with them.

andyp67 indeed find is much more predictable with the exception of the -exec argument which took me some time to get used to.


#6 2022-11-03 13:31:17

Registered: 2022-01-09
Posts: 34  

Re: [SOLVED] Locate cannot search for files with brackets?

I did it with Lua. I use this script in my custom Thunar action called "Locate elsewhere" and it works perfectly now

print("Other files with the same filename:")
for _,argument in ipairs(arg) do
        print('locate -b "'..argument..'"')
        os.execute('locate -b "'..argument..'"')

Last edited by Tritonio (2022-11-03 13:35:56)


Board footer