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 contentsRemove 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?Bash for loop on all files in folder except one
Was touching your nose a greeting in second millenium Mesopotamia?
MH370 blackbox - is it still possible to retrieve data from it?
Symbol for "not absolutely continuous" in Latex
How would a order of Monks that renounce their names communicate effectively?
Does a centaur PC also count as being mounted?
How to assign a Python list to a vim variable and escape its strings correctly
where clause to retrieve case record which are older than 12 months and 1 day in soql
Generate and graph the Recamán Sequence
Row to remove the dotted white border around focused button text
Are there any vegetarian astronauts?
How hard is it to sell a home which is currently mortgaged?
Which centaur is more 'official'?
Signing using digital signatures?
In native German words, is Q always followed by U, as in English?
Is there a short way to check uniqueness of values without using 'if' and multiple 'and's?
Bash echo $-1 prints hb1. Why?
How do I spend money in Sweden and Denmark?
What speedlites can work with the Canon EOS 4000D's non-standard hotshoe?
Children's short story about material that accelerates away from gravity
What's the point of DHS warning passengers about Manila airport?
Is なきにしもあらず~なきに a set phrase?
What does 2>&1 | tee mean?
Should I include salary information on my CV?
can’t run a function against EXEC
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 contentsRemove 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?Bash for loop on all files in folder except one
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
– pLumo
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
5924 silver badges14 bronze badges
5924 silver badges14 bronze badges
asked Mar 25 at 9:13
FreedoFreedo
4606 silver badges23 bronze badges
4606 silver badges23 bronze badges
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.
– pLumo
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.
– pLumo
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.
– pLumo
Mar 25 at 9:24
please try to create a minimal code example for us to reproduce without your whole script.
– pLumo
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 |
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
);
);
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
137k33 gold badges283 silver badges459 bronze badges
137k33 gold badges283 silver badges459 bronze badges
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.
– pLumo
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