linux删除文件时排除特定的文件

2018-08-20 浏览 (1487)

linux删除文件时排除特定的文件有多种方式,下面两种是常用的:

一、rm + extglob

shopt -s extglob

rm -fr !(file1)

1.确认 extglob 已经开启

shopt extglob

#如果是off,开启extglob

shopt -s extglob

#shopt, -s 开启, -u 关闭

2.新建几个文件和文件夹进行测试

touch main.c main.h hello.c hello.h

mkdir dir1 dir2

#保留hello.c dir1

rm -rf !(hello.c|dir1)

#保留.c后缀的文件

#rm -rf !(*.c)

二、rm + ls + egrep|awk

#删除当前目录下所有 *.txt文件,除了test.txt

rm ls *.txt|egrep -v test.txt

#或者

rm ls *.txt|awk '{if($0 != "test.txt") print $0}'

#排除多个文件

rm ls *.txt|egrep -v '(test.txt|fff.txt|ppp.txt)'

rm -f ls *.log.1|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'

rm -f ls *.log|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'

rm -f ls *.log|egrep -v '(20100906.log)'

  • 所属分类: linux
  • 版权声明: 本文链接 https://seaxiang.com/blog/linux_rm