gpt4 book ai didi

linux - 为什么 find piped to xargs mv 删除了我的文件?

转载 作者:行者123 更新时间:2023-12-04 19:40:35 25 4
gpt4 key购买 nike

今天我经历了一件难以置信的事情。我的目标是将超过 7 天的文件 mv 到另一个目录。目录存在。
我使用了命令:

find ./* -newermt $(date +%Y-%m-%d -d '7 day ago') -type f -print | xargs -I '{}' mv {} ../update_error_handled
然后,令人难以置信的文件不见了,我去了使用的文件夹 ls -lA并没有找到我移动的任何文件。发生了什么? CentOS 7.0,没有目录挂载,原始文件丢失,尝试 grep -r "content" / - 什么都没找到……
那么为什么它会这样呢?
之前我启动了 find ./* -newermt $(date +%Y-%m-%d -d '7 day ago') -type f -print它返回:
 ./file66.xml
./file67.xml
...etc.
以这种方式丢失数据确实很糟糕。
澄清一下:目录在移动文件之前就已经存在。目录不包含我今天尝试移动的文件,仅包含较旧的文件。

最佳答案

您是否提前在 ../update_error_handled 创建了一个目录? ?
如果没有,那么您从文件中剩下的所有内容将是最后一个,将被称为 ../update_error_handled .
为了避免此类错误,我总是通过添加 /. 来确保目标目录存在。在目标目录名称的末尾。
不安全的做法:

$ rm -rf file dest_dir
$ touch file
$ ls -ld file dest_dir
ls: cannot access 'dest_dir': No such file or directory
-rw-rw-r--. 1 u u 0 Sep 28 11:53 file
$ mv file dest_dir
$ ls -ld file dest_dir
ls: cannot access 'file': No such file or directory
-rw-rw-r--. 1 u u 0 Sep 28 11:53 dest_dir
使用不安全的方法, file被重命名为一个名为 dest_dir 的文件.
安全方法:
$ rm -rf file dest_dir
$ touch file
$ ls -ld file dest_dir
ls: cannot access 'dest_dir': No such file or directory
-rw-rw-r--. 1 u u 0 Sep 28 11:54 file
$ mv file dest_dir/.
mv: cannot move 'file' to 'dest_dir/.': No such file or directory
$ ls -ld file dest_dir
ls: cannot access 'dest_dir': No such file or directory
-rw-rw-r--. 1 u u 0 Sep 28 11:54 file
使用安全方法, mv命令失败,文件 file保持完好。

关于linux - 为什么 find piped to xargs mv 删除了我的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64098546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com