Delete files or directories in bash shell

来源:互联网 发布:如何创建域名 编辑:程序博客网 时间:2024/05/17 01:44

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$ lsa.txt  b.txt  c.sh  d.cpp  test.sedgehan@gehan-Lenovo-G480:~/test$ rm -i *.txtrm: remove regular empty file ‘a.txt’? yrm: remove regular empty file ‘b.txt’? ygehan@gehan-Lenovo-G480:~/test$ lsc.sh  d.cpp  test.sedgehan@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$ lscheck.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.hgehan@gehan-Lenovo-G480:~/test$ rm !(*.sh|*.c)gehan@gehan-Lenovo-G480:~/test$ lscheck.sh  list.c  run.sh  stack.cgehan@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$ lscheck.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.hgehan@gehan-Lenovo-G480:~/test$ GLOBIGNORE=*.c:*.shgehan@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$ lscheck.sh  list.c  run.sh  stack.cgehan@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$ lscheck.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.hgehan@gehan-Lenovo-G480:~/test$ find . -type f -not -name '*.sh' -deletegehan@gehan-Lenovo-G480:~/test$ lscheck.sh  run.sh##OR another command format##gehan@gehan-Lenovo-G480:~/test$ lscheck.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.hgehan@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$ lscheck.sh  run.shgehan@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$ lscheck.sh  list.c  list.h  log.txt  run.sh  stack.c  stack.hgehan@gehan-Lenovo-G480:~/test$ find . -type f -not \( -name '*.c' -or -name '*.h' \) -deletegehan@gehan-Lenovo-G480:~/test$ lslist.c  list.h  stack.c  stack.hgehan@gehan-Lenovo-G480:~/test$ 

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

0 0
原创粉丝点击