Linux bash script to find and delete oldest file with special characters and whitespaces in a directory tree if condtion is met

£可爱£侵袭症+ 提交于 2021-02-20 00:44:18

问题


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.

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.

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

Which gives output:

2016-03-11+19:21:44.2814042100 /mnt/volume0/recordings/some folder (R)/some filename (R)2016-03-1119-00.ts

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

EDIT: output from anwered post1. Still not working...

EDIT2: output from sorontar suggestion, still not working, new suggestion only gives dir name, not file...

NEW output:

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

While the oldest file is this:

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

Any suggestions?

EDIT3: -f2- instead of -f2 made it working. Thank you all!

Correct line is:

find /mnt/volume0/recordings/ -type f -printf '%T+ %p\0' | sort -zk1,1 | head -n1 -z | cut -zd ' ' -f2- | xargs -0 -I{} rm -f "{}"

回答1:


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).



来源:https://stackoverflow.com/questions/40985701/linux-bash-script-to-find-and-delete-oldest-file-with-special-characters-and-whi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!