Find fails if filename contains bracketsFind: Reference to Current DirectoryHow do I use find when the filename contains spaces?recursive search for a pattern, then for each match print out the specific SEQUENCE: line number, file name, and no file contentsAppend and write out variable-named file from list of variablesRemove all brackets in filenameFind a directory which contains all the files in my listFind filename using list inside a fileFind words after specific symbol on linedesktop action with bash command and terminalHow to add values to an array which contains a variable in the array name in bash?

How do I define a right arrow with bar in LaTeX?

Why does John Bercow say “unlock” after reading out the results of a vote?

quarter to five p.m

What defines a dissertation?

Is it correct to write "is not focus on"?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

What would be the benefits of having both a state and local currencies?

Is there a problem with hiding "forgot password" until it's needed?

The plural of 'stomach"

Is it okay / does it make sense for another player to join a running game of Munchkin?

Modify casing of marked letters

What's a natural way to say that someone works somewhere (for a job)?

Trouble understanding overseas colleagues

How to verify if g is a generator for p?

Go Pregnant or Go Home

Efficiently merge handle parallel feature branches in SFDX

How will losing mobility of one hand affect my career as a programmer?

voltage of sounds of mp3files

How do I rename a LINUX host without needing to reboot for the rename to take effect?

Lay out the Carpet

Why Were Madagascar and New Zealand Discovered So Late?

Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?

How to avoid InDesign adding pages automatically?

Can I convert a rim brake wheel to a disc brake wheel?



Find fails if filename contains brackets


Find: Reference to Current DirectoryHow do I use find when the filename contains spaces?recursive search for a pattern, then for each match print out the specific SEQUENCE: line number, file name, and no file contentsAppend and write out variable-named file from list of variablesRemove all brackets in filenameFind a directory which contains all the files in my listFind filename using list inside a fileFind words after specific symbol on linedesktop action with bash command and terminalHow to add values to an array which contains a variable in the array name in bash?













2















I'm trying to use find inside a loop to create a variable that contains a file matching the filename + desired string



Example:



file1.en.srt
file1.mkv
file1.pt.srt


This is the relevant part of the code:



shopt -s nullglob
shopt -s nocaseglob

if [ -d "$1" ]; then
for file in "$1%//"*mkv; do
# Get filename to match against subs and audios
filename="$(basename "$file" .mkv)"

# Find matching subtitle file
engsubs="$(find . -name "$filename*en.srt*" | sed -e 's,^./,,')"

# Find matching audio file
engaudio="$(find . -iname "$filename*en.ac3" -o -iname "$filename*en.eac3" -o -iname "$filename*en.dts" | sed -e 's,^./,,')"
done
fi


It works if files don't contain brackets, but the find commands don't find anything for files whose names contain brackets. Why this is happening? I want to create a variable like $en that would contain file1.en.srt










share|improve this question



















  • 5





    Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

    – terdon
    yesterday











  • No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

    – Freedo
    yesterday







  • 1





    please try to create a minimal code example for us to reproduce without your whole script.

    – RoVo
    yesterday







  • 1





    Related: Why is looping over find's output bad practice?

    – Kusalananda
    yesterday











  • Also related: Why does my shell script choke on whitespace or other special characters?

    – Kusalananda
    yesterday















2















I'm trying to use find inside a loop to create a variable that contains a file matching the filename + desired string



Example:



file1.en.srt
file1.mkv
file1.pt.srt


This is the relevant part of the code:



shopt -s nullglob
shopt -s nocaseglob

if [ -d "$1" ]; then
for file in "$1%//"*mkv; do
# Get filename to match against subs and audios
filename="$(basename "$file" .mkv)"

# Find matching subtitle file
engsubs="$(find . -name "$filename*en.srt*" | sed -e 's,^./,,')"

# Find matching audio file
engaudio="$(find . -iname "$filename*en.ac3" -o -iname "$filename*en.eac3" -o -iname "$filename*en.dts" | sed -e 's,^./,,')"
done
fi


It works if files don't contain brackets, but the find commands don't find anything for files whose names contain brackets. Why this is happening? I want to create a variable like $en that would contain file1.en.srt










share|improve this question



















  • 5





    Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

    – terdon
    yesterday











  • No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

    – Freedo
    yesterday







  • 1





    please try to create a minimal code example for us to reproduce without your whole script.

    – RoVo
    yesterday







  • 1





    Related: Why is looping over find's output bad practice?

    – Kusalananda
    yesterday











  • Also related: Why does my shell script choke on whitespace or other special characters?

    – Kusalananda
    yesterday













2












2








2








I'm trying to use find inside a loop to create a variable that contains a file matching the filename + desired string



Example:



file1.en.srt
file1.mkv
file1.pt.srt


This is the relevant part of the code:



shopt -s nullglob
shopt -s nocaseglob

if [ -d "$1" ]; then
for file in "$1%//"*mkv; do
# Get filename to match against subs and audios
filename="$(basename "$file" .mkv)"

# Find matching subtitle file
engsubs="$(find . -name "$filename*en.srt*" | sed -e 's,^./,,')"

# Find matching audio file
engaudio="$(find . -iname "$filename*en.ac3" -o -iname "$filename*en.eac3" -o -iname "$filename*en.dts" | sed -e 's,^./,,')"
done
fi


It works if files don't contain brackets, but the find commands don't find anything for files whose names contain brackets. Why this is happening? I want to create a variable like $en that would contain file1.en.srt










share|improve this question
















I'm trying to use find inside a loop to create a variable that contains a file matching the filename + desired string



Example:



file1.en.srt
file1.mkv
file1.pt.srt


This is the relevant part of the code:



shopt -s nullglob
shopt -s nocaseglob

if [ -d "$1" ]; then
for file in "$1%//"*mkv; do
# Get filename to match against subs and audios
filename="$(basename "$file" .mkv)"

# Find matching subtitle file
engsubs="$(find . -name "$filename*en.srt*" | sed -e 's,^./,,')"

# Find matching audio file
engaudio="$(find . -iname "$filename*en.ac3" -o -iname "$filename*en.eac3" -o -iname "$filename*en.dts" | sed -e 's,^./,,')"
done
fi


It works if files don't contain brackets, but the find commands don't find anything for files whose names contain brackets. Why this is happening? I want to create a variable like $en that would contain file1.en.srt







bash shell-script find filenames






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









wjandrea

504413




504413










asked yesterday









FreedoFreedo

450520




450520







  • 5





    Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

    – terdon
    yesterday











  • No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

    – Freedo
    yesterday







  • 1





    please try to create a minimal code example for us to reproduce without your whole script.

    – RoVo
    yesterday







  • 1





    Related: Why is looping over find's output bad practice?

    – Kusalananda
    yesterday











  • Also related: Why does my shell script choke on whitespace or other special characters?

    – Kusalananda
    yesterday












  • 5





    Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

    – terdon
    yesterday











  • No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

    – Freedo
    yesterday







  • 1





    please try to create a minimal code example for us to reproduce without your whole script.

    – RoVo
    yesterday







  • 1





    Related: Why is looping over find's output bad practice?

    – Kusalananda
    yesterday











  • Also related: Why does my shell script choke on whitespace or other special characters?

    – Kusalananda
    yesterday







5




5





Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

– terdon
yesterday





Please don't just tell us "it works" and "it doesn't work". How does it fail? What error message do you get? Which part of it fails? How do you call this script? What is $1?

– terdon
yesterday













No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

– Freedo
yesterday






No error message, it just won't find anything if the filenames contains brackets []... I call this script from command line... $1 would be the path

– Freedo
yesterday





1




1





please try to create a minimal code example for us to reproduce without your whole script.

– RoVo
yesterday






please try to create a minimal code example for us to reproduce without your whole script.

– RoVo
yesterday





1




1





Related: Why is looping over find's output bad practice?

– Kusalananda
yesterday





Related: Why is looping over find's output bad practice?

– Kusalananda
yesterday













Also related: Why does my shell script choke on whitespace or other special characters?

– Kusalananda
yesterday





Also related: Why does my shell script choke on whitespace or other special characters?

– Kusalananda
yesterday










1 Answer
1






active

oldest

votes


















8














The problem is that [ and ] are glob characters. For example, consider this file:



ba[r].mkv


When running your script on that file, $filename will be: ba[r] and therefore your find command will be:



find . -name 'ba[r]*pt-BR.srt*'


Since [r] is a single letter character class, it means r. So your command is looking for a filename starting with ba, then an r, then any character(s), and pt-BR.srt and any characters again. You need to escape the brackets:



find . -name 'ba[r]*pt-BR.srt*'


The simplest way to do that is to use printf and %q. So just change this line:



filename="$(basename "$file" .mkv)"


To this:



filename=$(printf '%q' "$(basename "$file" .mkv)")





share|improve this answer























  • Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

    – Freedo
    yesterday






  • 1





    @Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

    – terdon
    yesterday










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508462%2ffind-fails-if-filename-contains-brackets%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














The problem is that [ and ] are glob characters. For example, consider this file:



ba[r].mkv


When running your script on that file, $filename will be: ba[r] and therefore your find command will be:



find . -name 'ba[r]*pt-BR.srt*'


Since [r] is a single letter character class, it means r. So your command is looking for a filename starting with ba, then an r, then any character(s), and pt-BR.srt and any characters again. You need to escape the brackets:



find . -name 'ba[r]*pt-BR.srt*'


The simplest way to do that is to use printf and %q. So just change this line:



filename="$(basename "$file" .mkv)"


To this:



filename=$(printf '%q' "$(basename "$file" .mkv)")





share|improve this answer























  • Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

    – Freedo
    yesterday






  • 1





    @Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

    – terdon
    yesterday















8














The problem is that [ and ] are glob characters. For example, consider this file:



ba[r].mkv


When running your script on that file, $filename will be: ba[r] and therefore your find command will be:



find . -name 'ba[r]*pt-BR.srt*'


Since [r] is a single letter character class, it means r. So your command is looking for a filename starting with ba, then an r, then any character(s), and pt-BR.srt and any characters again. You need to escape the brackets:



find . -name 'ba[r]*pt-BR.srt*'


The simplest way to do that is to use printf and %q. So just change this line:



filename="$(basename "$file" .mkv)"


To this:



filename=$(printf '%q' "$(basename "$file" .mkv)")





share|improve this answer























  • Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

    – Freedo
    yesterday






  • 1





    @Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

    – terdon
    yesterday













8












8








8







The problem is that [ and ] are glob characters. For example, consider this file:



ba[r].mkv


When running your script on that file, $filename will be: ba[r] and therefore your find command will be:



find . -name 'ba[r]*pt-BR.srt*'


Since [r] is a single letter character class, it means r. So your command is looking for a filename starting with ba, then an r, then any character(s), and pt-BR.srt and any characters again. You need to escape the brackets:



find . -name 'ba[r]*pt-BR.srt*'


The simplest way to do that is to use printf and %q. So just change this line:



filename="$(basename "$file" .mkv)"


To this:



filename=$(printf '%q' "$(basename "$file" .mkv)")





share|improve this answer













The problem is that [ and ] are glob characters. For example, consider this file:



ba[r].mkv


When running your script on that file, $filename will be: ba[r] and therefore your find command will be:



find . -name 'ba[r]*pt-BR.srt*'


Since [r] is a single letter character class, it means r. So your command is looking for a filename starting with ba, then an r, then any character(s), and pt-BR.srt and any characters again. You need to escape the brackets:



find . -name 'ba[r]*pt-BR.srt*'


The simplest way to do that is to use printf and %q. So just change this line:



filename="$(basename "$file" .mkv)"


To this:



filename=$(printf '%q' "$(basename "$file" .mkv)")






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









terdonterdon

133k32264444




133k32264444












  • Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

    – Freedo
    yesterday






  • 1





    @Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

    – terdon
    yesterday

















  • Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

    – Freedo
    yesterday






  • 1





    @Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

    – terdon
    yesterday
















Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

– Freedo
yesterday





Yeah I just noticed now that I didn't need that * at the end of srt or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containing space, (), ç or other special characters?

– Freedo
yesterday




1




1





@Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

– terdon
yesterday





@Freedo no, the spaces and ( will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file".

– terdon
yesterday

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508462%2ffind-fails-if-filename-contains-brackets%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

БиармияSxpst500bh2ntaf! 3h2r