Bash array with folder paths and wildcards The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?When is double-quoting necessary?List of top level folders with contents no younger than 30 daysBash script error with strings with paths that have spaces and wildcardsfind delete shows “No such file or directory”Exit bash when find gets to a folder with permission deniedHow to set up a pre-defined variable with multiple directory paths to use with the find commandregarding recursive deleting of a specific file typeHow to store a path built with wildcards and containing with spaces into a variableFind and delete folders within directory that are older than x daysCheck if files are deletedCleanup Script for macOS
Can the prologue be the backstory of your main character?
"... to apply for a visa" or "... and applied for a visa"?
Does Parliament need to approve the new Brexit delay to 31 October 2019?
How to split my screen on my Macbook Air?
Would an alien lifeform be able to achieve space travel if lacking in vision?
Cooking pasta in a water boiler
Working through the single responsibility principle (SRP) in Python when calls are expensive
Am I ethically obligated to go into work on an off day if the reason is sudden?
How are presidential pardons supposed to be used?
What information about me do stores get via my credit card?
How can I protect witches in combat who wear limited clothing?
Windows 10: How to Lock (not sleep) laptop on lid close?
Difference between "generating set" and free product?
How is simplicity better than precision and clarity in prose?
Create an outline of font
Did God make two great lights or did He make the great light two?
The variadic template constructor of my class cannot modify my class members, why is that so?
Wall plug outlet change
Semisimplicity of the category of coherent sheaves?
University's motivation for having tenure-track positions
The following signatures were invalid: EXPKEYSIG 1397BC53640DB551
Can a novice safely splice in wire to lengthen 5V charging cable?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Arduino Pro Micro - switch off LEDs
Bash array with folder paths and wildcards
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?When is double-quoting necessary?List of top level folders with contents no younger than 30 daysBash script error with strings with paths that have spaces and wildcardsfind delete shows “No such file or directory”Exit bash when find gets to a folder with permission deniedHow to set up a pre-defined variable with multiple directory paths to use with the find commandregarding recursive deleting of a specific file typeHow to store a path built with wildcards and containing with spaces into a variableFind and delete folders within directory that are older than x daysCheck if files are deletedCleanup Script for macOS
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:
#!/bin/bash
FILEAGE=15
#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)
#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""
echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then
echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d
else
echo "##### Did not find directory: $d #####"
echo ""
fi
done
My script is returning that it can't find the directories like so:
Did not find directory: /srv/*/folderA
Note: There will be files throughout these folders under 15 days that can't be deleted.
Update
Using @Kusalananda suggestion '/srv/'*'/folderA'
fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.
bash shell-script shell wildcards variable
add a comment |
Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:
#!/bin/bash
FILEAGE=15
#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)
#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""
echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then
echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d
else
echo "##### Did not find directory: $d #####"
echo ""
fi
done
My script is returning that it can't find the directories like so:
Did not find directory: /srv/*/folderA
Note: There will be files throughout these folders under 15 days that can't be deleted.
Update
Using @Kusalananda suggestion '/srv/'*'/folderA'
fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.
bash shell-script shell wildcards variable
I don't think you can provide multiple root paths with e. g.find /path/*/to/places
. The easiest option for me off the top of my head would befor rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.
– DopeGhoti
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?
– Kusalananda♦
yesterday
1
@DopeGhotifind
does support multiple search paths, but the*
is expanded in the wrong place. It should be expanded in the array, not in the execution of thefind
command.
– Kusalananda♦
yesterday
add a comment |
Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:
#!/bin/bash
FILEAGE=15
#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)
#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""
echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then
echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d
else
echo "##### Did not find directory: $d #####"
echo ""
fi
done
My script is returning that it can't find the directories like so:
Did not find directory: /srv/*/folderA
Note: There will be files throughout these folders under 15 days that can't be deleted.
Update
Using @Kusalananda suggestion '/srv/'*'/folderA'
fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.
bash shell-script shell wildcards variable
Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:
#!/bin/bash
FILEAGE=15
#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)
#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""
echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then
echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d
else
echo "##### Did not find directory: $d #####"
echo ""
fi
done
My script is returning that it can't find the directories like so:
Did not find directory: /srv/*/folderA
Note: There will be files throughout these folders under 15 days that can't be deleted.
Update
Using @Kusalananda suggestion '/srv/'*'/folderA'
fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.
bash shell-script shell wildcards variable
bash shell-script shell wildcards variable
edited yesterday
JuanD
asked yesterday
JuanDJuanD
2616
2616
I don't think you can provide multiple root paths with e. g.find /path/*/to/places
. The easiest option for me off the top of my head would befor rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.
– DopeGhoti
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?
– Kusalananda♦
yesterday
1
@DopeGhotifind
does support multiple search paths, but the*
is expanded in the wrong place. It should be expanded in the array, not in the execution of thefind
command.
– Kusalananda♦
yesterday
add a comment |
I don't think you can provide multiple root paths with e. g.find /path/*/to/places
. The easiest option for me off the top of my head would befor rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.
– DopeGhoti
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?
– Kusalananda♦
yesterday
1
@DopeGhotifind
does support multiple search paths, but the*
is expanded in the wrong place. It should be expanded in the array, not in the execution of thefind
command.
– Kusalananda♦
yesterday
I don't think you can provide multiple root paths with e. g.
find /path/*/to/places
. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.– DopeGhoti
yesterday
I don't think you can provide multiple root paths with e. g.
find /path/*/to/places
. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.– DopeGhoti
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?– Kusalananda♦
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?– Kusalananda♦
yesterday
1
1
@DopeGhoti
find
does support multiple search paths, but the *
is expanded in the wrong place. It should be expanded in the array, not in the execution of the find
command.– Kusalananda♦
yesterday
@DopeGhoti
find
does support multiple search paths, but the *
is expanded in the wrong place. It should be expanded in the array, not in the execution of the find
command.– Kusalananda♦
yesterday
add a comment |
2 Answers
2
active
oldest
votes
The shell globbing pattern *
is not expanded within double quotes. This means that your loop
for d in "$dir_array[@]";do
is looping over patterns. In your call to dir_delete
, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find
.
The real show-stopper is that the patterns will also be expanded in the [ -d $d ]
test, which is awkward since the -d
test only ever takes a single pathname. This is, in the end, why the script fails.
Instead, make sure that the patterns are properly expanded when assigning to dir_array
:
dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)
If folderA
or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the *
should not be quoted.
Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.
There may be quoting issues with the following lines:
if [ -d $d ];then
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
dir_delete $d
Also consider using printf
rather than echo
when outputting variable data.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Why is printf better than echo?
Your suggestion of using'/srv/'*'/folderA'
worked for me. Thanks again.
– JuanD
yesterday
add a comment |
You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()
shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)
The expansion would happen at the for d in ... which I think is the correct point.
1
No, just use/srv/*/folderA
unquoted (or'/srv/'*'/folderA'
). It will expand correctly at the time of assigning todir_array
.
– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is usingbash
for (and not/bin/sh
) is that single array. Changing it toset -- patterns
would allow it to run under/bin/sh
(the loop would loop over"$@"
instead). My personal preference is to use/bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.
– Kusalananda♦
yesterday
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%2f512148%2fbash-array-with-folder-paths-and-wildcards%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The shell globbing pattern *
is not expanded within double quotes. This means that your loop
for d in "$dir_array[@]";do
is looping over patterns. In your call to dir_delete
, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find
.
The real show-stopper is that the patterns will also be expanded in the [ -d $d ]
test, which is awkward since the -d
test only ever takes a single pathname. This is, in the end, why the script fails.
Instead, make sure that the patterns are properly expanded when assigning to dir_array
:
dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)
If folderA
or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the *
should not be quoted.
Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.
There may be quoting issues with the following lines:
if [ -d $d ];then
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
dir_delete $d
Also consider using printf
rather than echo
when outputting variable data.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Why is printf better than echo?
Your suggestion of using'/srv/'*'/folderA'
worked for me. Thanks again.
– JuanD
yesterday
add a comment |
The shell globbing pattern *
is not expanded within double quotes. This means that your loop
for d in "$dir_array[@]";do
is looping over patterns. In your call to dir_delete
, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find
.
The real show-stopper is that the patterns will also be expanded in the [ -d $d ]
test, which is awkward since the -d
test only ever takes a single pathname. This is, in the end, why the script fails.
Instead, make sure that the patterns are properly expanded when assigning to dir_array
:
dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)
If folderA
or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the *
should not be quoted.
Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.
There may be quoting issues with the following lines:
if [ -d $d ];then
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
dir_delete $d
Also consider using printf
rather than echo
when outputting variable data.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Why is printf better than echo?
Your suggestion of using'/srv/'*'/folderA'
worked for me. Thanks again.
– JuanD
yesterday
add a comment |
The shell globbing pattern *
is not expanded within double quotes. This means that your loop
for d in "$dir_array[@]";do
is looping over patterns. In your call to dir_delete
, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find
.
The real show-stopper is that the patterns will also be expanded in the [ -d $d ]
test, which is awkward since the -d
test only ever takes a single pathname. This is, in the end, why the script fails.
Instead, make sure that the patterns are properly expanded when assigning to dir_array
:
dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)
If folderA
or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the *
should not be quoted.
Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.
There may be quoting issues with the following lines:
if [ -d $d ];then
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
dir_delete $d
Also consider using printf
rather than echo
when outputting variable data.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Why is printf better than echo?
The shell globbing pattern *
is not expanded within double quotes. This means that your loop
for d in "$dir_array[@]";do
is looping over patterns. In your call to dir_delete
, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find
.
The real show-stopper is that the patterns will also be expanded in the [ -d $d ]
test, which is awkward since the -d
test only ever takes a single pathname. This is, in the end, why the script fails.
Instead, make sure that the patterns are properly expanded when assigning to dir_array
:
dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)
If folderA
or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the *
should not be quoted.
Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.
There may be quoting issues with the following lines:
if [ -d $d ];then
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
dir_delete $d
Also consider using printf
rather than echo
when outputting variable data.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Why is printf better than echo?
edited yesterday
answered yesterday
Kusalananda♦Kusalananda
141k18263439
141k18263439
Your suggestion of using'/srv/'*'/folderA'
worked for me. Thanks again.
– JuanD
yesterday
add a comment |
Your suggestion of using'/srv/'*'/folderA'
worked for me. Thanks again.
– JuanD
yesterday
Your suggestion of using
'/srv/'*'/folderA'
worked for me. Thanks again.– JuanD
yesterday
Your suggestion of using
'/srv/'*'/folderA'
worked for me. Thanks again.– JuanD
yesterday
add a comment |
You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()
shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)
The expansion would happen at the for d in ... which I think is the correct point.
1
No, just use/srv/*/folderA
unquoted (or'/srv/'*'/folderA'
). It will expand correctly at the time of assigning todir_array
.
– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is usingbash
for (and not/bin/sh
) is that single array. Changing it toset -- patterns
would allow it to run under/bin/sh
(the loop would loop over"$@"
instead). My personal preference is to use/bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.
– Kusalananda♦
yesterday
add a comment |
You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()
shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)
The expansion would happen at the for d in ... which I think is the correct point.
1
No, just use/srv/*/folderA
unquoted (or'/srv/'*'/folderA'
). It will expand correctly at the time of assigning todir_array
.
– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is usingbash
for (and not/bin/sh
) is that single array. Changing it toset -- patterns
would allow it to run under/bin/sh
(the loop would loop over"$@"
instead). My personal preference is to use/bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.
– Kusalananda♦
yesterday
add a comment |
You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()
shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)
The expansion would happen at the for d in ... which I think is the correct point.
You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()
shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)
The expansion would happen at the for d in ... which I think is the correct point.
edited yesterday
answered yesterday
XrXcaXrXca
813
813
1
No, just use/srv/*/folderA
unquoted (or'/srv/'*'/folderA'
). It will expand correctly at the time of assigning todir_array
.
– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is usingbash
for (and not/bin/sh
) is that single array. Changing it toset -- patterns
would allow it to run under/bin/sh
(the loop would loop over"$@"
instead). My personal preference is to use/bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.
– Kusalananda♦
yesterday
add a comment |
1
No, just use/srv/*/folderA
unquoted (or'/srv/'*'/folderA'
). It will expand correctly at the time of assigning todir_array
.
– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is usingbash
for (and not/bin/sh
) is that single array. Changing it toset -- patterns
would allow it to run under/bin/sh
(the loop would loop over"$@"
instead). My personal preference is to use/bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.
– Kusalananda♦
yesterday
1
1
No, just use
/srv/*/folderA
unquoted (or '/srv/'*'/folderA'
). It will expand correctly at the time of assigning to dir_array
.– Kusalananda♦
yesterday
No, just use
/srv/*/folderA
unquoted (or '/srv/'*'/folderA'
). It will expand correctly at the time of assigning to dir_array
.– Kusalananda♦
yesterday
@XrXca your suggestion gave me the following message:
[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@XrXca your suggestion gave me the following message:
[: /srv/level1A/folderA: binary operator expected
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.
– JuanD
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.
– XrXca
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is using
bash
for (and not /bin/sh
) is that single array. Changing it to set -- patterns
would allow it to run under /bin/sh
(the loop would loop over "$@"
instead). My personal preference is to use /bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.– Kusalananda♦
yesterday
@XrXca I'm just looking at the code and noticing that the only thing the script is using
bash
for (and not /bin/sh
) is that single array. Changing it to set -- patterns
would allow it to run under /bin/sh
(the loop would loop over "$@"
instead). My personal preference is to use /bin/sh
, which is why I commented the way I did. Apart from that, you may well be right.– Kusalananda♦
yesterday
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%2f512148%2fbash-array-with-folder-paths-and-wildcards%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
-bash, shell, shell-script, variable, wildcards
I don't think you can provide multiple root paths with e. g.
find /path/*/to/places
. The easiest option for me off the top of my head would befor rootpath in /srv/*/folderA; do find "$rootpath" [...]; done
.– DopeGhoti
yesterday
*
will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?– Kusalananda♦
yesterday
1
@DopeGhoti
find
does support multiple search paths, but the*
is expanded in the wrong place. It should be expanded in the array, not in the execution of thefind
command.– Kusalananda♦
yesterday