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
Usefind
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.
- Delete files or directories in bash shell
- rm - remove files or directories
- Ignore Files and Directories in Subversion
- How to diff and merge files or directories on Linux
- Bash Shell in Linux
- List files and directories
- Files and Directories
- 4-Files and Directories
- Files and Directories
- Working with CSV files in Bash
- /bin/bash too many files in system
- AppleScript: match and delete files in folder
- 每天一命令(5) rm (remove files or directories) 1分钟
- ls 顏色設定(in Bash shell)
- CSharp Algorithm - Recursive method (Iterate all directories and files in a directory)
- python3.5.2中pycharm中pip install xgboost出现No files/directories in...
- cp - copy files and directories
- APUE-Chapter4. Files and Directories
- 01--tomcat初识
- 平述factory reset——从main system到重引导流程
- 单例模式Singleton
- QtQuick 全局缩放和放大<DPI>
- Jump to Section Overview Usage Floating Action Icons Design Support Library Animating the Floating A
- Delete files or directories in bash shell
- ViewPager简单实现分析
- 为什么基于DNS的全局负载均衡(GSLB)不起作用?
- 【网络编程2】Java数据报套接字
- R语言︱线性混合模型理论与案例探究(固定效应&随机效应)
- leetcode-Ugly Number(丑数)
- 【Windows】教你一步一步在Windows 7 系统上开启Telnet服务
- Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史.
- UVa 1347 Tour