-rw-r--r-- 1 SUPER.SUPER SUPER 48615 May 30 11:47 do.jck8b
-rw-r--r-- 1 SUPER.SUPER SUPER 48629 May 30 11:47 do.jck8b.64
drwxrwxrwx 1 SUPER.SUPER SUPER 4096 May 30 12:16 2018-05-28
drwxrwxrwx 1 SUPER.SUPER SUPER 4096 May 30 14:28 J22
drwxrwxrwx 1 SUPER.SUPER SUPER 4096 Jun 4 15:54 2018-05-29
drwxrwxrwx 1 SUPER.SUPER SUPER 4096 Jun 4 15:54 2018-05-23
drwxrwxrwx 1 SUPER.SUPER SUPER 4096 Jun 4 21:26 2018-05-22
我的要求是删除除最新3个文件夹以外的所有文件夹, 示例输出为:
2018-05-29
2018-05-23
2018-05-22
我尝试过这种方式,但是需要花费大量时间才能删除,是否有任何改变方式...
find . -name "*" | grep -v 2018-05-29 | grep -v 2018-05-23 | grep -v 2018-05-22 | grep -v do.jck | xargs rm -rf
答案 0 :(得分:0)
# Starting with these directory contents
$ ls -l
total 24
drwxr-xr-x 2 chuckx chuckx 4096 Jun 5 08:27 2018-05-25
drwxr-xr-x 2 chuckx chuckx 4096 Jun 5 08:26 2018-05-26
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-27
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-28
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-29
-rw-r--r-- 1 chuckx chuckx 98 Jun 4 23:29 mkdirs.sh
# List only directories and sort by reversed modified time
$ ls -dlrt */
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-29/
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-28/
drwxr-xr-x 2 chuckx chuckx 4096 Jun 4 23:29 2018-05-27/
drwxr-xr-x 2 chuckx chuckx 4096 Jun 5 08:26 2018-05-26/
drwxr-xr-x 2 chuckx chuckx 4096 Jun 5 08:27 2018-05-25/
# Removed the detailed listing to just get filenames
$ ls -drt */
2018-05-29/ 2018-05-28/ 2018-05-27/ 2018-05-26/ 2018-05-25/
# Strip the last three lines (i.e. the most recent directories)
$ ls -drt */ | head -n -3
2018-05-29/
2018-05-28/
# Pipe the results to xargs to execute rmdir
$ ls -drt */ | head -n -3 | xargs rmdir
# Look at the results
$ ls
2018-05-25 2018-05-26 2018-05-27 mkdirs.sh
# Starting with these directory contents
$ ls
2018-05-25 2018-05-26 2018-05-27 2018-05-28 2018-05-29 mkdirs.sh
# List only the directories
$ ls -d */
2018-05-25/ 2018-05-26/ 2018-05-27/ 2018-05-28/ 2018-05-29/
# Pipe to sort, just to be certain
$ ls -d */ | sort
2018-05-25/
2018-05-26/
2018-05-27/
2018-05-28/
2018-05-29/
# Strip the last three lines (i.e. most recent directories)
$ ls -d */ | sort | head -n -3
2018-05-25/
2018-05-26/
# Pipe the results to xargs to execute rmdir
$ ls -d */ | sort | head -n -3 | xargs rmdir
# Look at the results
$ ls
2018-05-27 2018-05-28 2018-05-29 mkdirs.sh