天天看点

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' -print | xargs - -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:

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

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

继续阅读