Delete files or directories in bash shell

As a new user to study Linux, this blog will introduces the command that be used to remove files or directories under bash shell. Furthermore, I will specify a special circumstance that will use some other command and regular expression to achieve.
Now I will to specify the command rm that be used to remove files and directories, and rmdir which remove empty directories.

Command

1. rm

This command removes files and directories, its synopsis should be rm [option]...FILE....
Now assume we will remove all .txt files under a path, its command should be as below:

gehan@gehan-Lenovo-G480:~/test$ ls
a.txt  b.txt  c.sh  d.cpp  test.sed
gehan@gehan-Lenovo-G480:~/test$ rm -i *.txt
rm: remove regular empty file ‘a.txt’? y
rm: remove regular empty file ‘b.txt’? y
gehan@gehan-Lenovo-G480:~/test$ ls
c.sh  d.cpp  test.sed
gehan@gehan-Lenovo-G480:~/test$ 

As above code chip,we use rm -i *.txt to remove all .txt files,and it achieved the result that we want. And here are explains about important and common options that command rm use.
-i: promote before every removal;
-f, –force: ignore non-existent files and arguments, never prompt;
-r,-R,–recursive: remove directories and their contents recursively;
-d, –dir: remove empty directories, its effect just like rmdir;

2. rmdir

This command removes empty directories, its synopsis should be rm [option]...DIRECTORY....

If you want to know more details about bash shell command, you can use command man to read it, its syntax like man rm.

Example

But in real work, we often have to deal with more complex situations.In here,we want to remove all files except .sh and **.c**files.

  • Method #1
    Run rm command in bash shell directly:
##Delete all files except file1##
rm !(file1)
##Delete all files except file1 and file2##
rm !(file1|file2)

##Run command in bash shell on Linux OS##
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ rm !(*.sh|*.c)
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  run.sh  stack.c
gehan@gehan-Lenovo-G480:~/test$ 
  • Method #2
    Using bash GLOBIGNORE variable to remove all files except specific ones.
##only work with bash shell##
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ GLOBIGNORE=*.c:*.sh
gehan@gehan-Lenovo-G480:~/test$ rm -v *
removed ‘list.h’
removed ‘log.txt’
removed ‘stack.h’
gehan@gehan-Lenovo-G480:~/test$ unset GLOBIGNORE 
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  run.sh  stack.c
gehan@gehan-Lenovo-G480:~/test$ 
  • Method #3
    Use find command to select and then remove files as requirement.
    To remove all files except *.sh files under path ~/test directory:
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ find . -type f -not -name '*.sh' -delete
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  run.sh
##OR another command format##
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ find . -type f -not -name '*.sh' -print0 | xargs -0 -I {} rm -v {}
removed ‘./list.c’
removed ‘./list.h’
removed ‘./stack.c’
removed ‘./stack.h’
removed ‘./log.txt’
gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  run.sh
gehan@gehan-Lenovo-G480:~/test$ 

Now we want to delete all files except .c and .h file, command syntax as below:

gehan@gehan-Lenovo-G480:~/test$ ls
check.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ find . -type f -not \( -name '*.c' -or -name '*.h' \) -delete
gehan@gehan-Lenovo-G480:~/test$ ls
list.c  list.h  stack.c  stack.h
gehan@gehan-Lenovo-G480:~/test$ 

For more details and information, you can search with google and see man page.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值