GNU bug report logs -
#9742
touch option for existence?
Previous Next
Reported by: Morty <morty+coreutils <at> frakir.org>
Date: Thu, 13 Oct 2011 01:52:01 UTC
Severity: normal
Tags: notabug
Done: Jim Meyering <jim <at> meyering.net>
Bug is archived. No further changes may be made.
Full log
Message #8 received at 9742 <at> debbugs.gnu.org (full text, mbox):
On 10/13/2011 02:45 AM, Morty wrote:
> [This is more of a feature request than a bug request. But I don't
> see where I can make feature requests.]
>
> It would be nice if touch had an option to only "touch" if the file
> doesn't already exist. Sort of like -c, but the other way around.
> This is useful because often, the reason one is using "touch" in a
> script is because one wants to make sure that a file exists before
> doing an operation that expects the file to already exist. But if the
> file already exists, then touch has a side effect of changing the
> mtime or the atime. [On many systems, one can work on a file without
> changing the atime thanks to mount options such as noatime or
> relatime.]
>
> It's easy enough to wrap touch in an if:
>
> if [ ! -e $file ]; then touch $file; fi
>
> But it would be nicer if this common case were built in. I would
> suggest -e, for exists. Thanks!
Note the above is easier to express in shell like:
[ -e "$file" ] || touch "$file"
But that is racy. If you were using touch for locking purposes
then adding -e (must create), would allow one to add O_EXCL to the open(),
so you could then do things like:
until touch -e "$lock_file"; do sleep .1; done
However that functionality is already supported like:
until mkdir "$lock_dir"; do sleep .1; done
So I'm not sure this is warranted.
I'm 60:40 against currently.
cheers,
Pádraig.
This bug report was last modified 13 years and 228 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.