Bash IF folder contains subdirectory named x
I'm trying to make a bash script that adds folders to a zip.
Using this scenario, here is what the outcome I'm looking for something like:
IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI
I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.
linux bash if-statement scripting sh
add a comment |
I'm trying to make a bash script that adds folders to a zip.
Using this scenario, here is what the outcome I'm looking for something like:
IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI
I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.
linux bash if-statement scripting sh
3
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, tryif [[ -d "$dir" ]]
. Syntax forzip
can be found here.
– Paul Hodges
Nov 12 '18 at 16:48
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Look at Glenn's suggestion below. Afor
only processes directories that exist, and the name will be in the variable.
– Paul Hodges
Nov 12 '18 at 19:37
add a comment |
I'm trying to make a bash script that adds folders to a zip.
Using this scenario, here is what the outcome I'm looking for something like:
IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI
I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.
linux bash if-statement scripting sh
I'm trying to make a bash script that adds folders to a zip.
Using this scenario, here is what the outcome I'm looking for something like:
IF /home/*/users EXISTS
ADD the folder containing the subdirectory users to the .zip
FI
I'm pretty new to it all, and I've searched for similar things around, but I haven't found anything.
linux bash if-statement scripting sh
linux bash if-statement scripting sh
asked Nov 12 '18 at 16:40
JordanJordan
31
31
3
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, tryif [[ -d "$dir" ]]
. Syntax forzip
can be found here.
– Paul Hodges
Nov 12 '18 at 16:48
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Look at Glenn's suggestion below. Afor
only processes directories that exist, and the name will be in the variable.
– Paul Hodges
Nov 12 '18 at 19:37
add a comment |
3
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, tryif [[ -d "$dir" ]]
. Syntax forzip
can be found here.
– Paul Hodges
Nov 12 '18 at 16:48
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Look at Glenn's suggestion below. Afor
only processes directories that exist, and the name will be in the variable.
– Paul Hodges
Nov 12 '18 at 19:37
3
3
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try
if [[ -d "$dir" ]]
. Syntax for zip
can be found here.– Paul Hodges
Nov 12 '18 at 16:48
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try
if [[ -d "$dir" ]]
. Syntax for zip
can be found here.– Paul Hodges
Nov 12 '18 at 16:48
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Look at Glenn's suggestion below. A
for
only processes directories that exist, and the name will be in the variable.– Paul Hodges
Nov 12 '18 at 19:37
Look at Glenn's suggestion below. A
for
only processes directories that exist, and the name will be in the variable.– Paul Hodges
Nov 12 '18 at 19:37
add a comment |
3 Answers
3
active
oldest
votes
I would use a for
loop and an inline test.
for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done
add a comment |
You'll want to iterate over the expansion of the wildcard. It sounds like you want:
for dir in /home/*/; do
if [[ -d "$dir/users" ]]; then
add "$dir" to the zip
fi
done
The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile
exists, it will not be included in the loop.
add a comment |
Consider passing all of the directories to a single zip
call. No loop needed.
zip -r users.zip /home/*/users/
If you want to zip up the parent directory then:
zip -r users.zip /home/*/users/..
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53266512%2fbash-if-folder-contains-subdirectory-named-x%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would use a for
loop and an inline test.
for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done
add a comment |
I would use a for
loop and an inline test.
for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done
add a comment |
I would use a for
loop and an inline test.
for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done
I would use a for
loop and an inline test.
for dir in /home/*/users
do [[ -d "$dir" ]] && : your zip command
done
answered Nov 12 '18 at 19:39
Paul HodgesPaul Hodges
3,1311322
3,1311322
add a comment |
add a comment |
You'll want to iterate over the expansion of the wildcard. It sounds like you want:
for dir in /home/*/; do
if [[ -d "$dir/users" ]]; then
add "$dir" to the zip
fi
done
The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile
exists, it will not be included in the loop.
add a comment |
You'll want to iterate over the expansion of the wildcard. It sounds like you want:
for dir in /home/*/; do
if [[ -d "$dir/users" ]]; then
add "$dir" to the zip
fi
done
The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile
exists, it will not be included in the loop.
add a comment |
You'll want to iterate over the expansion of the wildcard. It sounds like you want:
for dir in /home/*/; do
if [[ -d "$dir/users" ]]; then
add "$dir" to the zip
fi
done
The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile
exists, it will not be included in the loop.
You'll want to iterate over the expansion of the wildcard. It sounds like you want:
for dir in /home/*/; do
if [[ -d "$dir/users" ]]; then
add "$dir" to the zip
fi
done
The trailing slash in the wildcard in the for statement is used to limit the results to only directories. For example, if some file named /home/somefile
exists, it will not be included in the loop.
answered Nov 12 '18 at 17:58
glenn jackmanglenn jackman
166k26143237
166k26143237
add a comment |
add a comment |
Consider passing all of the directories to a single zip
call. No loop needed.
zip -r users.zip /home/*/users/
If you want to zip up the parent directory then:
zip -r users.zip /home/*/users/..
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
add a comment |
Consider passing all of the directories to a single zip
call. No loop needed.
zip -r users.zip /home/*/users/
If you want to zip up the parent directory then:
zip -r users.zip /home/*/users/..
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
add a comment |
Consider passing all of the directories to a single zip
call. No loop needed.
zip -r users.zip /home/*/users/
If you want to zip up the parent directory then:
zip -r users.zip /home/*/users/..
Consider passing all of the directories to a single zip
call. No loop needed.
zip -r users.zip /home/*/users/
If you want to zip up the parent directory then:
zip -r users.zip /home/*/users/..
edited Nov 13 '18 at 1:16
answered Nov 12 '18 at 19:44
John KugelmanJohn Kugelman
241k53400454
241k53400454
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
add a comment |
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
I like that best, as long as there will certainly be at least one.
– Paul Hodges
Nov 12 '18 at 20:05
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
Wouldn't this just have the effect of a folder just containing the users subfolder? I want everything inside of the parent dir.
– Jordan
Nov 12 '18 at 23:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53266512%2fbash-if-folder-contains-subdirectory-named-x%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
3
Start with the basic documentation for bash and zip. This site is a great overall bash reference. I sincerely recommend you read every page, it will save you a lot of time and grief in the future. For testing directory existence, try
if [[ -d "$dir" ]]
. Syntax forzip
can be found here.– Paul Hodges
Nov 12 '18 at 16:48
Thanks, I've got the zip part down, It's just referencing to a directory that I don't know the name of due to the *
– Jordan
Nov 12 '18 at 17:03
Look at Glenn's suggestion below. A
for
only processes directories that exist, and the name will be in the variable.– Paul Hodges
Nov 12 '18 at 19:37