如何在不更改目录的修改时间戳的情况下删除临时文件?

Mic*_*ael 5 directory timestamps

我处理测试用例,每个测试用例都是~/test. 使用类似ls -rtl. 现在我想从测试目录中删除某些临时文件;唉,这会改变目录的时间戳。

是否可以在不更改目录的修改时间戳的情况下从目录中删除文件?

ilk*_*chu 4

删除文件后,您必须重置目录上的时间戳。假设使用 GNU 工具,类似这样的东西应该可以工作:

mtime=$(stat -c %y dir)            # get the timestamp, store in $mtime
rm dir/somefile dir/someotherfile  # do whatever you need
touch -d "$mtime" dir              # set the timestamp back
Run Code Online (Sandbox Code Playgroud)

这会将目录上的修改 ( mtime) 和访问 ( atime) 时间戳重置为原始修改时间戳,同时还将更改时间戳 ( ctime) 设置为当前时间。更改ctime是不可避免的,但您可能不关心它或atime