Skip to main content
33 events
when toggle format what by license comment
Apr 29, 2024 at 6:32 comment added Ron at BiophysicsLab I am using zsh, not bash. For a zsh script, the -d option only works for absolute paths (@mach6 above points how to just remove ~ ) . It took me some time to realize that a relative path needs to be converted to absolute first. For ex: "~/Desktop/" fails. I convert from relative to an absolute path like this: DIRECTORY=$HOME$DIRECTORY[2,-1]
Mar 11, 2024 at 14:51 comment added mach6 just want to point out if the $DIRECTORY is started with ~, you need DIRECTORY="${DIRECTORY/#~/$HOME}"
Aug 1, 2022 at 1:21 history edited Mateen Ulhaq CC BY-SA 4.0
Shorten.
Sep 16, 2021 at 21:25 comment added Illuminatus On the vein of teach a man to fish I rarely see anyone suggesting checking man test in the comments. Since it describes each test option I would suggest reviewing it as a fast way to determine comparisons you can perform.
May 7, 2020 at 11:54 history edited Peter Mortensen CC BY-SA 4.0
Active reading [<https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences> (see also <https://twitter.com/PeterMortensen/status/1199839973215739907>) ].
Apr 5, 2020 at 21:11 comment added William Turrell The spaces within [ and ], around the -d or ! -d are important, otherwise script will fail with command not found.
Apr 5, 2020 at 17:41 comment added Shayan Is there any difference between -L and -h? This website gnu.org/savannah-checkouts/gnu/bash/manual/… says they're both True if file exists and is a symbolic link. So what's the difference?
Jan 10, 2019 at 10:40 comment added Danijel Why doesn't this work for relative paths? For example ../../../../../my_dir.
Jun 20, 2018 at 11:23 history edited Zoe - Save the data dump CC BY-SA 4.0
deleted 14 characters in body
Mar 30, 2018 at 1:25 review Suggested edits
Mar 30, 2018 at 10:24
Oct 3, 2017 at 9:41 comment added henon Using test does NOT work with an unplugged usb stick, since the journaling FS pretends the directory is still there if you already checked it before. I found that in this case checking the exit code of ls was the only way to check reliably if the directory exists: if ls "$DIRECTORY"; then echo "dir exists" ; fi
May 23, 2017 at 12:34 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Jan 17, 2017 at 9:21 comment added ghoti Instead of testing for both the directory (-d) and the symlink (-L), it's easier just to append a slash to the variable, like if [ -d "${THING:+$THING/}" ]. A directory won't mind the extra slash. A file will evaluate to false. Empty will remain empty, so false. And a symlink will be resolved to its destination. Of course, it depends on your goal. If you want to go there, this is fine. If you want to delete it, then the code in this answer is better.
Jul 28, 2015 at 14:00 review Suggested edits
Jul 28, 2015 at 14:36
Nov 15, 2013 at 16:09 comment added BlueCacti DATE1='date -I -d "1 day ago"' ; TARGET_DIR="/backup" which is a link; RSYNC_PARAMS="-azrtplH"
Nov 15, 2013 at 16:02 comment added BlueCacti For some reason, I'm getting an error with this: if [ -d "$TARGET_DIR/$DATE1" ]; then $RSYNC_PARAMS="$RSYNC_PARAMS --link-dest=$TARGET_DIR/$DATE1" fi
Sep 9, 2013 at 11:51 comment added Alfe It might be worth mentioning that as soon as the check has been performed the situation can have changed already due to other processes. In many cases it is better to just create or use the directory and react on a failure.
Jul 26, 2013 at 3:45 comment added Jürgen Paul should I use [ ] or [[ ]]?
Apr 10, 2013 at 8:36 history edited Zombo CC BY-SA 3.0
deleted 15 characters in body
Sep 26, 2012 at 12:52 comment added Rob Kielty Minor note. In bash, help [, man test (as already noted) and info test (on "Not UNIXes") provide documentation.
Jul 21, 2012 at 13:38 history suggested Alan W. Smith CC BY-SA 3.0
Updated link to supporting answer to use the provided "link" format at the end of each question. This corrects an issue in some browsers (e.g. Safari 5.1) where the original link would load the page at the top instead of anchoring at the intended answer.
Jul 21, 2012 at 13:22 review Suggested edits
Jul 21, 2012 at 13:38
S May 12, 2012 at 23:53 history suggested Jens CC BY-SA 3.0
This is a general shell question and not bash specific.
May 12, 2012 at 17:06 review Suggested edits
S May 12, 2012 at 23:53
Aug 9, 2011 at 23:46 comment added Keith Thompson One thing to keep in mind: [ ! -d "$DIRECTORY" ] will be true either if $DIRECTORY doesn't exist, or if does exist but isn't a directory. Consider something like if [ ! -d "$DIRECTORY" ] ; then mkdir "$DIRECTORY" ; fi; this will fail if "$DIRECTORY" is a file. (Of course you should check whether mkdir succeeded anyway; there are a number of reasons it can fail.)
Jul 27, 2011 at 14:17 history edited Costi Ciudatu CC BY-SA 3.0
added 4 characters in body
Mar 24, 2011 at 14:22 comment added fpmurphy For modern versions of bash, ksh, etc. [...] is a builtin
Mar 18, 2010 at 11:50 history edited Grundlefleck CC BY-SA 2.5
Clarified the "will enter here" statement.
Jul 21, 2009 at 16:36 comment added Marc Mutz - mmutz If you want to play it safe with the GNU tools, use of -- is highly recommended (end-of-options marker). Otherwise, if your variable contains something that looks like an option, the script'll fail just as with spaces.
Mar 26, 2009 at 21:18 vote accept Grundlefleck
Sep 16, 2008 at 11:28 history edited Grundlefleck CC BY-SA 2.5
Added note to consider variables with spaces in.
Sep 12, 2008 at 21:27 history edited Grundlefleck CC BY-SA 2.5
Added description of dealing with the gotcha of symbolic links, following Jon Ericson's post
Sep 12, 2008 at 20:07 history answered Grundlefleck CC BY-SA 2.5