Linux bash脚本在满足条件的情况下查找和删除目录树中带有特殊字符和空格的最旧文件 [英] Linux bash script to find and delete oldest file with special characters and whitespaces in a directory tree if condtion is met
问题描述
如果满足条件,我需要一些帮助来构建linux bash脚本来查找和删除目录树中带有特殊字符和空格的最旧文件.
I need some help building a linux bash script to find and delete oldest file with special characters and white spaces in a directory tree if condtion is met.
我一直在论坛中搜索类似的问题,感谢这里的用户,我提供了如下所示的输出.到目前为止,我还不知道如何将输出文件名通过管道传递到rm,以便将其删除.
I have been searching the forum for questions like this and thanks to users here I came with output as seen under. So far I can't figure out how to pipe the output filename to rm, so that it is being deleted.
目标是检查hdd是否已满运行,如果是,则删除最早的文件,直到满足可用空间要求为止.问题是文件名中填充了特殊字符和空格...
The goal is to check if hdd is running full, and if so delete the oldest file until free-space requirement is met. The problem is, that the filenames are filled with special characters and white-spaces...
这就是我最终得到的(感谢此处的用户!):
This is what I ended up with (thanks to users here!):
find /mnt/volume0/recordings/ -type f -printf '%T+ %p\n' | sort | head -q -n 1 | cat
哪个输出:
2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/some folder (R)/some filename (R)2016-03-1119-00.ts
我如何才能找到文件的完整路径名,并将所有特殊字符和空格通过管道传递给rm以删除文件?
How can I get then pick out the full path name to the file with all special charcters and whitespaces piped to rm for it to delete the file?
这是已回答帖子的输出:
Here is the output from the answered post:
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\n' | sort -d | head -n1
2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/VIDEO_FILE (R)/VIDEO_FILE (R)2016-03-1119-00.ts
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\n' | sort -d | head -n1 | xargs -I{} echo rm -f "{}"
rm -f 2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/VIDEO_FILE (R)/VIDEO_FILE (R)2016-03-1119-00.ts
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\n' | sort -d | head -n1 | xargs -I{} rm -f "{}"
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\n' | sort -d | head -n1
2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/VIDEO_FILE (R)/VIDEO_FILE (R)2016-03-1119-00.ts
从有效的post1输出.仍然无法正常工作...
output from anwered post1. Still not working...
sonontar建议的输出,仍然无法正常工作,新建议仅提供目录名称,而不提供文件...
output from sorontar suggestion, still not working, new suggestion only gives dir name, not file...
新输出:
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\0' | sort -zk1,1 | head -n1 -z | cut -zd ' ' -f2
/mnt/volume0/recordings/Parneviks
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\0' | sort -zk1,1 | head -n1 -z | cut -zd ' ' -f2 | xargs -0 -I{} echo rm -f "{}"
rm -f /mnt/volume0/recordings/Parneviks
最早的文件是这个
root@SERVER:~# find /mnt/volume0/recordings/ -type f -printf '%T+ %p\0' | sort -zk1,1 | head -n1 -z
2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/Parneviks (R)/Parneviks (R)2016-03-1119-00.ts
有什么建议吗?
-f2-而不是-f2使它正常工作.谢谢大家!
-f2- instead of -f2 made it working. Thank you all!
正确的行是:
find /mnt/volume0/recordings/ -type f -printf '%T+ %p\0' | sort -zk1,1 | head -n1 -z | cut -zd ' ' -f2- | xargs -0 -I{} rm -f "{}"
推荐答案
如果您确实要处理带有空格,换行符和任何特殊字符的文件,则必须考虑使用空 \ 0
作为文件名的限制,如下所示:
If you really want to process files with spaces, new lines and any special characters, you must consider using a null \0
as the limit for file names, like this:
dir=/mnt/volume0/recordings
find "$dir"/ -type f -printf '%T+ %p\0' |
sort -zk1,1 |
head -n1 -z |
cut -zd ' ' -f2- |
xargs -0 echo rm -f --
这将在目录中找到文件,并强行删除最旧的文件(只有一个)(如果已删除回显,请在实际使用它之前对其进行测试).
This will find files in a dir and forcefully will remove the oldest file (only one) (if the echo is removed, test it before actually using it).
这篇关于Linux bash脚本在满足条件的情况下查找和删除目录树中带有特殊字符和空格的最旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!