I'm using CentOS 7. In a specific directory, I want to delete all the "*.log" files except the newest one. I tried this
[rails@server ~]$ ls -t ~/.forever | tail -n +2 | xargs rm --
rm: cannot remove ‘pids’: No such file or directory
rm: cannot remove ‘5QEM.log’: No such file or directory
rm: cannot remove ‘sock’: No such file or directory
rm: cannot remove ‘8BVT.log’: No such file or directory
rm: cannot remove ‘lf4N.log’: No such file or directory
rm: cannot remove ‘Jf8F.log’: No such file or directory
rm: cannot remove ‘H1UG.log’: No such file or directory
rm: cannot remove ‘sNbx.log’: No such file or directory
rm: cannot remove ‘D30J.log’: No such file or directory
rm: cannot remove ‘_yj1.log’: No such file or directory
rm: cannot remove ‘Tz9c.log’: No such file or directory
rm: cannot remove ‘ur0M.log’: No such file or directory
rm: cannot remove ‘pX6o.log’: No such file or directory
rm: cannot remove ‘8P_i.log’: No such file or directory
rm: cannot remove ‘kBX_.log’: No such file or directory
rm: cannot remove ‘n4Ot.log’: No such file or directory
rm: cannot remove ‘VVdY.log’: No such file or directory
rm: cannot remove ‘T1QJ.log’: No such file or directory
rm: cannot remove ‘Zdeo.log’: No such file or directory
rm: cannot remove ‘5ejy.log’: No such file or directory
rm: cannot remove ‘dQEL.log’: No such file or directory
Not exactly sure what the output means, but the result is nothing is deleted. I'm only interested in deleting direct child files (as opposed to files in subfolders).
ls -t ~/.forever
intotail -n +2
?tail
outputs the last part of files and what you have will output the contents of the files inside starting with the 2nd line. – Nasir Riley May 15 '18 at 01:51ls
: https://mywiki.wooledge.org/ParsingLs – SparedWhisle May 15 '18 at 03:28tail -n +2
without file name arguments outputs the standard input to tail starting from line 2. – Johan Myréen May 15 '18 at 05:39