I have a Bash script that renames files after being listened to:
FILE=$(ls -tr|head -n 1) # get the oldest file
aplay $FILE # where $FILE is a wav file
mv $FILE $(date +"%Y%m%d_%H%M").wav
However the resulting file doesn't get its timestamp changed, so I have to:
FILE=$(ls -tr|head -n 1
aplay $FILE
touch $FILE
mv $FILE $(date +"%Y%m%d_%H%M").wav
which is fine, but I wonder if there is a way for "mv" to update the timestamp without using "touch".
Thanks.
On 20.07.2023 11:01, Ottavio Caruso wrote:
I have a Bash script that renames files after being listened to:
FILE=$(ls -tr|head -n 1) # get the oldest file
aplay $FILE # where $FILE is a wav file
mv $FILE $(date +"%Y%m%d_%H%M").wav
However the resulting file doesn't get its timestamp changed, so I have to: >>
FILE=$(ls -tr|head -n 1
aplay $FILE
touch $FILE
mv $FILE $(date +"%Y%m%d_%H%M").wav
which is fine, but I wonder if there is a way for "mv" to update the
timestamp without using "touch".
No, 'touch' is the command to use if you want to update time-stamps.
What you may also want to consider...
Quote all uses of $FILE.
Use 'mv -i' to prevent name clashes with your name's minute resolution.
Keep the output of 'date' in a variable and use it also for 'touch -d'.
You can also use 'stat' to get the file date(s) (access, modification, change) directly from the file without 'date', so that you may choose
whether to use initial date of the file or the date of last playing it.
Am 20/07/2023 um 09:14 schrieb Janis Papanagnou:
On 20.07.2023 11:01, Ottavio Caruso wrote:
I have a Bash script that renames files after being listened to:
FILE=$(ls -tr|head -n 1) # get the oldest file
aplay $FILE # where $FILE is a wav file
mv $FILE $(date +"%Y%m%d_%H%M").wav
However the resulting file doesn't get its timestamp changed, so I have to: >>>
FILE=$(ls -tr|head -n 1
aplay $FILE
touch $FILE
mv $FILE $(date +"%Y%m%d_%H%M").wav
which is fine, but I wonder if there is a way for "mv" to update the
timestamp without using "touch".
No, 'touch' is the command to use if you want to update time-stamps.
What you may also want to consider...
Quote all uses of $FILE.
Use 'mv -i' to prevent name clashes with your name's minute resolution.
Keep the output of 'date' in a variable and use it also for 'touch -d'.
You can also use 'stat' to get the file date(s) (access, modification,
change) directly from the file without 'date', so that you may choose
whether to use initial date of the file or the date of last playing it.
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new file, hence modifying creation date.
[...] Has this been "mv" standard behaviour from the UNIX days? You
would expect that renaming a file literally creates a new file, hence modifying creation date.
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new file, hence modifying creation date.
which is fine, but I wonder if there is a way for "mv" to update the timestamp without using "touch".
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new file, hence modifying creation date.
On Thu, 20 Jul 2023 14:11:59 +0000, Ottavio Caruso wrote:[snip]
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
No, under Unix, you would /NOT/ expect that mv(1) would create a new
file, nor would it modify a "creation date".
On 2023-07-20, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
Files do not have a "creation date" in Unix, only a modification time.
On Thu, 20 Jul 2023 17:09:37 -0000 (UTC), Kaz Kylheku wrote:
On 2023-07-20, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new >>> file, hence modifying creation date.
Files do not have a "creation date" in Unix, only a modification time.
Glad I run linux then:
$ cat /etc/release
Mageia release 9 (Cauldron) for x86_64
for ARG in w x y z ; do echo -n " -$ARG " ;\
stat --format=%$ARG /etc/profile ; done ;\
man stat | grep human-
-w 2023-03-16 20:43:32.784828906 -0500
-x 2023-07-20 03:11:22.485829606 -0500
-y 2023-01-02 12:37:29.982724141 -0600
-z 2023-03-16 20:43:32.784828906 -0500
%w time of file birth, human-readable; - if unknown
%x time of last access, human-readable
%y time of last data modification, human-readable
%z time of last status change, human-readable
$ stat --version
stat (GNU coreutils) 9.1
Copyright (C) 2022 Free Software Foundation, Inc.
Files do not have a "creation date" in Unix, only a modification time.
On Thu, 20 Jul 2023 17:09:37 -0000 (UTC), Kaz Kylheku wrote:
On 2023-07-20, Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new >>> file, hence modifying creation date.
Files do not have a "creation date" in Unix, only a modification time.
Glad I run linux then:
$ cat /etc/release
Mageia release 9 (Cauldron) for x86_64
for ARG in w x y z ; do echo -n " -$ARG " ;\
stat --format=%$ARG /etc/profile ; done ;\
man stat | grep human-
-w 2023-03-16 20:43:32.784828906 -0500
-x 2023-07-20 03:11:22.485829606 -0500
-y 2023-01-02 12:37:29.982724141 -0600
-z 2023-03-16 20:43:32.784828906 -0500
%w time of file birth, human-readable; - if unknown
%x time of last access, human-readable
%y time of last data modification, human-readable
%z time of last status change, human-readable
$ stat --version
stat (GNU coreutils) 9.1
Copyright (C) 2022 Free Software Foundation, Inc.
On 20.07.23 16:11, Ottavio Caruso wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
As many other people already said, that assumption is wrong. And to go
one step further, IMO it rather sucks that UNIX "cp" _does_ update the modification date unless using "-p". I never understood why it behaves
this way as the content is untouched and why B should have a different
time stamp than A after "cp A B". Even brain-dead systems don't do that.
On 20.07.23 16:11, Ottavio Caruso wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
As many other people already said, that assumption is wrong. And to go one step further, IMO it rather sucks that UNIX "cp" _does_ update the modification date unless using "-p". I never understood why it behaves this way as the content is untouched and why B should have a different time
stamp than A after "cp A B". Even brain-dead systems don't do that.
On 20.07.23 16:11, Ottavio Caruso wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
As many other people already said, that assumption is wrong. And to go
one step further, IMO it rather sucks that UNIX "cp" _does_ update the modification date unless using "-p". I never understood why it behaves
this way as the content is untouched and why B should have a different
time stamp than A after "cp A B". Even brain-dead systems don't do that.
[*] And this reminds me the pathological case where there's no short
option available; remember the 'ls --ignore-fail-on-non-empty' from
a thread one year ago? - Compared to that 'cp -p' and 'mv -i' are trivialities.
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
[...]
[*] And this reminds me the pathological case where there's no short
option available; remember the 'ls --ignore-fail-on-non-empty' from
a thread one year ago? - Compared to that 'cp -p' and 'mv -i' are
trivialities.
"--ignore-fail-on-non-empty" is an option for rmdir, not ls.
And it can be abbreviated to "--i" (but not to "-i").
On 20.07.23 16:11, Ottavio Caruso wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new
file, hence modifying creation date.
As many other people already said, that assumption is wrong. And to go
one step further, IMO it rather sucks that UNIX "cp" _does_ update the modification date unless using "-p". I never understood why it behaves
this way as the content is untouched and why B should have a different
time stamp than A after "cp A B".
On 22.07.2023 08:13, Kaz Kylheku wrote:
The model is very clear: cp just replicates the content without
any meta-data. All meta-data updates are opt-in.
Can you explain that "without any meta-data" part?
My understanding is that cp(1) uses write(2) and that this system
call will generally update the modification time meta-data on the
file.
(Any option to "preserve" (i.e. take over) the time from the
original file would need separate system calls. - Is that what you
meant?)
The model is very clear: cp just replicates the content without
any meta-data. All meta-data updates are opt-in.
Am 20/07/2023 um 17:09 schrieb Kaz Kylheku:
Files do not have a "creation date" in Unix, only a modification time.
If that is the case, how about:
$ man ls
--time=WORD
change the default of using modification times;
access time
(-u): atime, access, use; change time (-c): ctime,
status; birth time: birth, creation;
...
Thanks, but it sucks. Has this been "mv" standard behaviour from the UNIX days? You
would expect that renaming a file literally creates a new file, hence modifying
creation date.
On 2023-07-21, Frank Winkler <usenet@f.winkler-ka.de> wrote:
On 20.07.23 16:11, Ottavio Caruso wrote:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a new >> >file, hence modifying creation date.
As many other people already said, that assumption is wrong. And to go
one step further, IMO it rather sucks that UNIX "cp" _does_ update the
modification date unless using "-p". I never understood why it behaves
this way as the content is untouched and why B should have a different
time stamp than A after "cp A B".
- Because it often has different permissions and ownership, too.
- Because you might later want to know the time when B was copied.
In order for cp to set the modification time on B, it has to overwrite
the mofication time which B already has naturally. That perpetrates a
kind of lie. The object B did not even exist at the time indicated by
its modification time stamp.
The model is very clear: cp just replicates the content without
any meta-data. All meta-data updates are opt-in. Command line
OPT-ions for OPT-in behavior; imagine that!
No file systems that I
know of really note a modification time in the sense of the file being actually changed; they note a "write time".
After "cp A B" B is freshly
written but has (in some sense) not been modified.
Ben Bacarisse wrote:
No file systems that I
know of really note a modification time in the sense of the file being
actually changed; they note a "write time".
The modification time records when the contents of the file were
modified.
The contents can be changed by other operations than writing.
In particular, truncation will modify it (including "truncation" to a
larger size using truncate() - the extra bytes have not been "written"
but will be read as null bytes, so the file's contents have effectively changed).
After "cp A B" B is freshly
written but has (in some sense) not been modified.
It makes more sense if you think of what is being done here in terms of
the low-level operations that cp performs, rather than the overall
result.
"Ottavio" == Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a
new file, hence modifying creation date.
Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
Thanks, but it sucks. Has this been "mv" standard behaviour from the
UNIX days? You would expect that renaming a file literally creates a
new file, hence modifying creation date.
I would not expect that. Analogously, when a person changes name
(e.g. upon marriage) its the same person under a different name; no new >person has been created.
(*) Note that, at the nuts-and-bolts level, in Unix terms, a "rename"
always boils down to a link followed by an unlink.
Classic Unix filesystems do *not* have a "creation date". They have a "ctime" field, which updates when the file is opened for write, or the metadata about the file has changed (name, number of links, and so on).
Renaming a file traditionally has updated ctime,
On 2023-07-30, Randal L. Schwartz <merlyn@stonehenge.com> wrote:
Classic Unix filesystems do *not* have a "creation date". They have a
"ctime" field, which updates when the file is opened for write, or the
metadata about the file has changed (name, number of links, and so on).
Renaming a file traditionally has updated ctime,
... because it was implemented as a sequence of link(2) and unlink(2),
which touched the inode's link count.
I have a vague memory that the atomicity (or lack thereof) of the
later rename(2) system call was a much discussed topic, but I don't
remember what's the status on modern operating systems.
This part made me curious...
On 30.07.2023 13:49, Kenny McCormack wrote:
(*) Note that, at the nuts-and-bolts level, in Unix terms, a "rename"
always boils down to a link followed by an unlink.
Inspecting the system log file during a file "rename" invoked by mv(1)
I can only spot a rename(2) system call (but no link(2) or unlink(2)).
"Ottavio" == Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
Ottavio> Thanks, but it sucks. Has this been "mv" standard behaviour
Ottavio> from the UNIX days? You would expect that renaming a file
Ottavio> literally creates a new file, hence modifying creation date.
Classic Unix filesystems do *not* have a "creation date".
They have a
"ctime" field, which updates when the file is opened for write, or the metadata about the file has changed (name, number of links, and so
on).
But the real point here, which has been alluded to by various posters (but not stated explicitly), is that many new users (including, I believe, OP) assume that the "c" in "ctime" stands for "creation". In fact, it doesn't. It stands for "change" - specifically, an inode change, as opposed to a
data change.
On 2023-07-30, Randal L. Schwartz <merlyn@stonehenge.com> wrote:
Classic Unix filesystems do *not* have a "creation date". They have a
"ctime" field, which updates when the file is opened for write, or the
metadata about the file has changed (name, number of links, and so on).
Renaming a file traditionally has updated ctime,
... because it was implemented as a sequence of link(2) and unlink(2),
which touched the inode's link count.
I have a vague memory that the atomicity (or lack thereof) of the
later rename(2) system call was a much discussed topic, but I don't
remember what's the status on modern operating systems.
But the real point here, which has been alluded to by various posters (but not stated explicitly), is that many new users (including, I believe, OP) assume that the "c" in "ctime" stands for "creation". In fact, it doesn't. It stands for "change" - specifically, an inode change, as opposed to a
data change.
And, in fact, when you rename a file (*), "ctime" *is* updated.
In article <ua5pko$2uu8g$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
This part made me curious...
On 30.07.2023 13:49, Kenny McCormack wrote:
(*) Note that, at the nuts-and-bolts level, in Unix terms, a "rename"
always boils down to a link followed by an unlink.
Inspecting the system log file during a file "rename" invoked by mv(1)
I can only spot a rename(2) system call (but no link(2) or unlink(2)).
Obviously, somebody was going to post that.
But I think even lower, at the real nuts&bolts level, rename is doing the equivalent of link/unlink. The point of making rename a system call was to make it possible to make certain guarantees of atomicity.
And, in fact, when you rename a file (*), "ctime" *is* updated.
Some systems update it, some don't. It may well be an old SysV v. BSD difference, as Solaris updates it but macOS does not.
Some systems update it, some don't. It may well be an old SysV v. BSD >difference, as Solaris updates it but macOS does not.
On 30.07.2023 18:03, Kenny McCormack wrote:
But I think even lower, at the real nuts&bolts level, rename is doing
the equivalent of link/unlink. The point of making rename a system
call was to make it possible to make certain guarantees of atomicity.
If atomicity is the topic then a statement "always boils down to a link followed by an unlink" makes even less sense. But okay, YMMV.
On 2023-07-31, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote:
And, in fact, when you rename a file (*), "ctime" *is* updated.
Some systems update it, some don't. It may well be an old SysV v. BSD
difference, as Solaris updates it but macOS does not.
FreeBSD and OpenBSD do update it.
Christian Weisgerber wrote:
On 2023-07-31, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote:
And, in fact, when you rename a file (*), "ctime" *is* updated.
Some systems update it, some don't. It may well be an old SysV v. BSD
difference, as Solaris updates it but macOS does not.
FreeBSD and OpenBSD do update it.
Thanks. I suppose that means either the macOS rename() internals
didn't come from FreeBSD or there has been a change in FreeBSD since
Apple used it as the basis for OS X.
"Ottavio" == Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> writes:
Ottavio> Thanks, but it sucks. Has this been "mv" standard behaviour
Ottavio> from the UNIX days? You would expect that renaming a file
Ottavio> literally creates a new file, hence modifying creation date.
Classic Unix filesystems do *not* have a "creation date". They have a >"ctime" field, which updates when the file is opened for write, or the >metadata about the file has changed (name, number of links, and so on).
On 30.07.2023 14:33, Christian Weisgerber wrote:
On 2023-07-30, Randal L. Schwartz <merlyn@stonehenge.com> wrote:
Classic Unix filesystems do *not* have a "creation date". They have a
"ctime" field, which updates when the file is opened for write, or the
metadata about the file has changed (name, number of links, and so on).
Renaming a file traditionally has updated ctime,
... because it was implemented as a sequence of link(2) and unlink(2),
which touched the inode's link count.
I have a vague memory that the atomicity (or lack thereof) of the
later rename(2) system call was a much discussed topic, but I don't
remember what's the status on modern operating systems.
I think the "later" and the "modern" should be put in a quantified
age context. My Stevens' APUE from 1993 (that's three decades ago)
already describes rename(2) (and also mentions POSIX.1 changes).
An older Unix book (based on UNIX Version 7) from 1984 didn't have
it listed.
gazelle@shell.xmission.com (Kenny McCormack) writes:
But the real point here, which has been alluded to by various posters (but >> not stated explicitly), is that many new users (including, I believe, OP)
assume that the "c" in "ctime" stands for "creation". In fact, it doesn't. >> It stands for "change" - specifically, an inode change, as opposed to a
data change.
That's a new interpretation. In the original paper on Unix, the i-node
is described as having a creation time. The field names, st_ctime,
st_mtime and st_atime were chosen with these meanings. When (very soon >after) a new kind of time had to be recorded, st_ctime was re-purposed.
So what the "c" in ctime stands for has changed. It's no wonder people
are confused because atime, mtime and ctime are so obvious. Had it >originally been used for its later purpose, I doubt it would have been
called ctime.
Christian Weisgerber wrote:
On 2023-07-31, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote: >>>> And, in fact, when you rename a file (*), "ctime" *is* updated.
Some systems update it, some don't. It may well be an old SysV v. BSD
difference, as Solaris updates it but macOS does not.
FreeBSD and OpenBSD do update it.
Thanks. I suppose that means either the macOS rename() internals
didn't come from FreeBSD or there has been a change in FreeBSD since
Apple used it as the basis for OS X.
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 30.07.2023 18:03, Kenny McCormack wrote:
But I think even lower, at the real nuts&bolts level, rename is doing
the equivalent of link/unlink. The point of making rename a system
call was to make it possible to make certain guarantees of atomicity.
If atomicity is the topic then a statement "always boils down to a link
followed by an unlink" makes even less sense. But okay, YMMV.
“rename is link followed by unlink, but atomic” doesn’t reflect what happens with directories, where you need to add an additional unlink
followed by link to reflect the overall outcome (at least in traditional
Unix filesystsem).
It’s also troublesome as an explanatory model in filesystems which don’t support hard links (i.e. which can’t have two names for one file
existing at once).
Finally it’s not a great model when talking to an audience which isn’t familiar or comfortable with the idea of multiple names for a single
file.
With those caveats in mind, though, I agree with Kenny, and the reason
for that is that you can still see reflections of the link+unlink design (generally surrounded by a lot of additional complexity) in some modern kernel implementations.
Geoff Clare <geoff@clare.See-My-Signature.invalid> writes:
Thanks. I suppose that means either the macOS rename() internals
didn't come from FreeBSD or there has been a change in FreeBSD since
Apple used it as the basis for OS X.
My understanding is that MacOS was based on an older BSD (and other
sources), not on FreeBSD. FreeBSD and MacOS are siblings or cousins,
not parent and child.
Geoff Clare <geoff@clare.See-My-Signature.invalid> writes:
Christian Weisgerber wrote:
On 2023-07-31, Geoff Clare <geoff@clare.See-My-Signature.invalid> wrote: >>>>> And, in fact, when you rename a file (*), "ctime" *is* updated.
Some systems update it, some don't. It may well be an old SysV v. BSD
difference, as Solaris updates it but macOS does not.
FreeBSD and OpenBSD do update it.
Thanks. I suppose that means either the macOS rename() internals
didn't come from FreeBSD or there has been a change in FreeBSD since
Apple used it as the basis for OS X.
My understanding is that MacOS was based on an older BSD (and other
sources), not on FreeBSD. FreeBSD and MacOS are siblings or cousins,
not parent and child.
[snip]
Speaking about "models", I wouldn't consider explaining rename(2) by
link(2) and unlink(2) (or explaining mv(1) by cp(1) and rm(1), just to
make my point clear) a sensible approach if it doesn't reflect reality
(which is optimized for the simple standard cases). If an "audience"
is operating/working on _that_ OS level it should IMO know the details
and differences of the supported actual cases. A model that gives the >impression that an [atomic] system function is effectively built upon
more than one system function - thus conceptually/model-wise becoming >non-atomic - I'd not consider a preferable description. Introducing
functions that aren't actually used to explain a function I also don't >consider sensible. (The formal part of the POSIX specs, for example,
doesn't mention them; they are unnecessary for understanding.) But as
I said upthread, mileages (obviously) vary.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (0 / 16) |
Uptime: | 75:53:09 |
Calls: | 9,819 |
Calls today: | 7 |
Files: | 13,757 |
Messages: | 6,190,028 |