Timeline for Using the test command to create a directory if it doesn't exist
Current License: CC BY-SA 3.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 20, 2015 at 22:11 | history | edited | Gilles 'SO- stop being evil' |
edited tags
|
|
| Feb 20, 2015 at 8:14 | vote | accept | Wesley Jordt | ||
| Feb 20, 2015 at 7:45 | answer | added | Barmar | timeline score: 2 | |
| Feb 20, 2015 at 7:42 | comment | added | mikeserv |
regarding your edit - it still fails if dir2 exists but is not a directory. you need [ ! -e ... ]. And anyway, when writing scripts, you should never start with assumptions - always start with ruling out edge case possibilities and work your way in to saner territory. What is end of home path? You mean /dir1/ is ~?
|
|
| Feb 20, 2015 at 7:40 | comment | added | Wesley Jordt | What other information is necessary? I apologize, I forgot to add in that Dir1 exists as it is the end of my home path. | |
| Feb 20, 2015 at 7:39 | history | edited | Wesley Jordt | CC BY-SA 3.0 |
added 10 characters in body
|
| Feb 20, 2015 at 7:27 | comment | added | Wesley Jordt | So even if the parent directory, in fact, exists, I still need to test for its existence? sorry, i'm just hung up on that part. Or is what my test doing not verifying the existence of Dir1 and trying to create a subdirectory of Dir1? I thought the test would test for the entire path, not just the existence of Dir1. | |
| Feb 20, 2015 at 7:22 | comment | added | mikeserv |
You asked if your command properly tests for the existence of and creates /dir1/dir2 and I'm just saying that it does not properly do this thing. You do not verify the parent dir's existence but use command syntax that fails to create a child dir of a dir that does not exist. It does not properly test for the existence of or properly create the path you asked about - and that's the long and short of it - sorry. And actually, for the endpoint, [ ! -e ... ] is probably better anyway.
|
|
| Feb 20, 2015 at 7:18 | comment | added | Wesley Jordt | I'm not using mkdir -p. Dir1 already exists, i'm testing to see if dir2 exists as a subdirectory of dir1, and to create it only if it doesn't exist. Why would I need to test for dir1 as well? | |
| Feb 20, 2015 at 7:10 | comment | added | Wesley Jordt | Assuming there exists no issues with permissions,and that dir2 doesn't already exist as a file, my test will work, correct? I can't run to check atm. | |
| Feb 20, 2015 at 7:06 | comment | added | mikeserv |
mkdir -p will fail as well if dir2 exists as a file other than a directory - or if permissions forbid you to create dirs for whatever reason. I usually do like mkdir -p ./target/dir && cd ./target/dir || exit
|
|
| Feb 20, 2015 at 6:55 | comment | added | Wesley Jordt | ya, Dir1 already existed, it was the end of the path of my home directory. We had to run it as a test, I didn't get the extra credit for it and was wondering if it was incorrect. Thank you. | |
| Feb 20, 2015 at 6:47 | comment | added | mikeserv |
No - not unless /dir1/ definitely already exists. Use mkdir -p /dir1/dir2 and forget the test.
|
|
| Feb 20, 2015 at 6:45 | history | edited | John1024 | CC BY-SA 3.0 |
Minor: Fix spelling, format code.
|
| Feb 20, 2015 at 6:34 | history | asked | Wesley Jordt | CC BY-SA 3.0 |