Find fails if filename contains brackets The Next CEO of Stack OverflowFind: 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?
Why can't we say "I have been having a dog"?
Calculate the Mean mean of two numbers
Is a distribution that is normal, but highly skewed, considered Gaussian?
MT "will strike" & LXX "will watch carefully" (Gen 3:15)?
Is it possible to make a 9x9 table fit within the default margins?
Planeswalker Ability and Death Timing
Can I hook these wires up to find the connection to a dead outlet?
How to travel Japan while expressing milk?
Can Sri Krishna be called 'a person'?
What exactly is ineptocracy?
How badly should I try to prevent a user from XSSing themselves?
Read/write a pipe-delimited file line by line with some simple text manipulation
Calculating discount not working
What did the word "leisure" mean in late 18th Century usage?
Car headlights in a world without electricity
Loop in Mac OSX not working
Why did early computer designers eschew integers?
Why does the freezing point matter when picking cooler ice packs?
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
Prodigo = pro + ago?
Ising model simulation
Is it a bad idea to plug the other end of ESD strap to wall ground?
How can the PCs determine if an item is a phylactery?
Find fails if filename contains brackets
The Next CEO of Stack OverflowFind: 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?
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
|
show 1 more comment
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
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♦
Mar 25 at 9:18
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
Mar 25 at 9:22
1
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
1
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35
|
show 1 more comment
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
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
bash shell-script find filenames
edited Mar 25 at 13:31
wjandrea
504413
504413
asked Mar 25 at 9:13
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♦
Mar 25 at 9:18
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
Mar 25 at 9:22
1
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
1
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35
|
show 1 more comment
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♦
Mar 25 at 9:18
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
Mar 25 at 9:22
1
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
1
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35
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♦
Mar 25 at 9:18
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♦
Mar 25 at 9:18
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
Mar 25 at 9:22
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
Mar 25 at 9:22
1
1
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
1
1
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35
|
show 1 more comment
1 Answer
1
active
oldest
votes
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)")
Yeah I just noticed now that I didn't need that*
at the end ofsrt
or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containingspace
,()
,ç
or other special characters?
– Freedo
Mar 25 at 9:42
1
@Freedo no, the spaces and(
will also be escaped. Try runningprintf '%qn' "a ho[r]i(b)le file"
.
– terdon♦
Mar 25 at 9:51
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)")
Yeah I just noticed now that I didn't need that*
at the end ofsrt
or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containingspace
,()
,ç
or other special characters?
– Freedo
Mar 25 at 9:42
1
@Freedo no, the spaces and(
will also be escaped. Try runningprintf '%qn' "a ho[r]i(b)le file"
.
– terdon♦
Mar 25 at 9:51
add a comment |
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)")
Yeah I just noticed now that I didn't need that*
at the end ofsrt
or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containingspace
,()
,ç
or other special characters?
– Freedo
Mar 25 at 9:42
1
@Freedo no, the spaces and(
will also be escaped. Try runningprintf '%qn' "a ho[r]i(b)le file"
.
– terdon♦
Mar 25 at 9:51
add a comment |
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)")
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)")
answered Mar 25 at 9:38
terdon♦terdon
133k33267446
133k33267446
Yeah I just noticed now that I didn't need that*
at the end ofsrt
or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containingspace
,()
,ç
or other special characters?
– Freedo
Mar 25 at 9:42
1
@Freedo no, the spaces and(
will also be escaped. Try runningprintf '%qn' "a ho[r]i(b)le file"
.
– terdon♦
Mar 25 at 9:51
add a comment |
Yeah I just noticed now that I didn't need that*
at the end ofsrt
or other file extensions lol. And it worked...thanks so much <3. Do you think this could mess with other filenames containingspace
,()
,ç
or other special characters?
– Freedo
Mar 25 at 9:42
1
@Freedo no, the spaces and(
will also be escaped. Try runningprintf '%qn' "a ho[r]i(b)le file"
.
– terdon♦
Mar 25 at 9:51
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
Mar 25 at 9:42
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
Mar 25 at 9:42
1
1
@Freedo no, the spaces and
(
will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file"
.– terdon♦
Mar 25 at 9:51
@Freedo no, the spaces and
(
will also be escaped. Try running printf '%qn' "a ho[r]i(b)le file"
.– terdon♦
Mar 25 at 9:51
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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♦
Mar 25 at 9:18
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
Mar 25 at 9:22
1
please try to create a minimal code example for us to reproduce without your whole script.
– RoVo
Mar 25 at 9:24
1
Related: Why is looping over find's output bad practice?
– Kusalananda♦
Mar 25 at 9:32
Also related: Why does my shell script choke on whitespace or other special characters?
– Kusalananda♦
Mar 25 at 9:35