Article 4SNVV bash script question

bash script question

by
circus78
from LinuxQuestions.org on (#4SNVV)
Hi,
my goal is to delete all files/directories in specific path, but retain the last "n".
I mean:

Code:[~] # ls -lt /share/CACHEDEV1_DATA/Backup/NetBakData/User
drwxrwxrwx 3 admin administ 4096 Oct 16 09:59 2019-10-16-10-00-00/
drwxrwxrwx 3 admin administ 4096 Oct 15 09:59 2019-10-15-10-00-00/
drwxrwxrwx 3 admin administ 4096 Oct 14 09:59 2019-10-14-10-00-00/
drwxrwxrwx 3 admin administ 4096 Oct 9 09:59 2019-10-09-10-00-00/
drwxrwxrwx 3 admin administ 4096 Oct 8 09:59 2019-10-08-10-00-00/
drwxrwxrwx 3 admin administ 4096 Oct 7 09:59 2019-10-07-10-00-00/I would like to keep most recent 4 directories.

If I type this command in relevant path (/share/CACHEDEV1_DATA/Backup/NetBakData/User), all is ok:

$ cd /share/CACHEDEV1_DATA/Backup/NetBakData/User
$ ls -lt | tail -n+4 | xargs rm -rf

If I use same command with absolute path, nothing happens:

$ ls -lt /share/CACHEDEV1_DATA/Backup/NetBakData/User | tail -n+4 | xargs rm -rf

why?

Thank you!latest?d=yIl2AUoC8zA latest?i=CLxlBfb3mqc:twPHqzBXIBY:F7zBnMy latest?i=CLxlBfb3mqc:twPHqzBXIBY:V_sGLiP latest?d=qj6IDK7rITs latest?i=CLxlBfb3mqc:twPHqzBXIBY:gIN9vFwCLxlBfb3mqc
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments