Linux:批量操纵有序文件名的文件或路径
在Linux中,我们通常会用以下的方法去重建一批文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[root@localhost script]# pwd /script [root@localhost script]# [root@localhost script]# ls -ltr total 0 [root@localhost script]# [root@localhost script]# mkdir -p the_world/{human/{iron_man},angel,god/{thorns,odin}} [root@localhost script]# [root@localhost script]# tree ../script/ ../script/ └── the_world ├── angel ├── god │ ├── odin │ └── thorns └── human └── {iron_man} 7 directories, 0 files [root@localhost script]# [root@localhost script]# mkdir -p numbers/{1,2,3,4,5,6} [root@localhost script]# [root@localhost script]# tree ../script/ ../script/ ├── numbers │ ├── 1 │ ├── 2 │ ├── 3 │ ├── 4 │ ├── 5 │ └── 6 └── the_world ├── angel ├── god │ ├── odin │ └── thorns └── human └── {iron_man} 14 directories, 0 files [root@localhost script]# |
假设场景:
1. 我需要创建文件名连续的一组文件
2. 但是因为数量过大,我不能够一一列出来
比较正常的思路是通过【for】循环来实现。
这种方式是一定可以做到的。
但是有没有更简单的方法?比如在一行命令中实现?
有。
下面来具体演示一下。
先看看错误的例子:
1 2 3 4 5 6 7 8 |
[root@localhost script]# touch {$(seq -s "," 1 10)} [root@localhost script]# [root@localhost script]# ls -ltr total 8 drwxr-xr-x 5 root root 4096 May 14 07:06 the_world drwxr-xr-x 8 root root 4096 May 14 07:07 numbers -rw-r--r-- 1 root root 0 May 14 07:10 {1,2,3,4,5,6,7,8,9,10} [root@localhost script]# |
正确的方式是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
[root@localhost script]# pwd /script [root@localhost script]# [root@localhost script]# touch $(seq 1 10) [root@localhost script]# [root@localhost script]# ls -ltr total 0 -rw-r--r-- 1 root root 0 May 14 07:11 9 -rw-r--r-- 1 root root 0 May 14 07:11 8 -rw-r--r-- 1 root root 0 May 14 07:11 7 -rw-r--r-- 1 root root 0 May 14 07:11 6 -rw-r--r-- 1 root root 0 May 14 07:11 5 -rw-r--r-- 1 root root 0 May 14 07:11 4 -rw-r--r-- 1 root root 0 May 14 07:11 3 -rw-r--r-- 1 root root 0 May 14 07:11 2 -rw-r--r-- 1 root root 0 May 14 07:11 10 -rw-r--r-- 1 root root 0 May 14 07:11 1 [root@localhost script]# [root@localhost script]# touch $(seq -f 'file_%g' 1 10) [root@localhost script]# [root@localhost script]# ls -ltr total 0 -rw-r--r-- 1 root root 0 May 14 07:17 file_9 -rw-r--r-- 1 root root 0 May 14 07:17 file_8 -rw-r--r-- 1 root root 0 May 14 07:17 file_7 -rw-r--r-- 1 root root 0 May 14 07:17 file_6 -rw-r--r-- 1 root root 0 May 14 07:17 file_5 -rw-r--r-- 1 root root 0 May 14 07:17 file_4 -rw-r--r-- 1 root root 0 May 14 07:17 file_3 -rw-r--r-- 1 root root 0 May 14 07:17 file_2 -rw-r--r-- 1 root root 0 May 14 07:17 file_10 -rw-r--r-- 1 root root 0 May 14 07:17 file_1 [root@localhost script]# |
如果我要对其中的序列【2】到【8】的文件执行删除,可以这么做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[root@localhost script]# ls -ltr total 0 -rw-r--r-- 1 root root 0 May 14 07:17 file_9 -rw-r--r-- 1 root root 0 May 14 07:17 file_8 -rw-r--r-- 1 root root 0 May 14 07:17 file_7 -rw-r--r-- 1 root root 0 May 14 07:17 file_6 -rw-r--r-- 1 root root 0 May 14 07:17 file_5 -rw-r--r-- 1 root root 0 May 14 07:17 file_4 -rw-r--r-- 1 root root 0 May 14 07:17 file_3 -rw-r--r-- 1 root root 0 May 14 07:17 file_2 -rw-r--r-- 1 root root 0 May 14 07:17 file_10 -rw-r--r-- 1 root root 0 May 14 07:17 file_1 [root@localhost script]# [root@localhost script]# rm -rf $(seq -f 'file_%g' 2 8) [root@localhost script]# [root@localhost script]# ls -ltr total 0 -rw-r--r-- 1 root root 0 May 14 07:17 file_9 -rw-r--r-- 1 root root 0 May 14 07:17 file_10 -rw-r--r-- 1 root root 0 May 14 07:17 file_1 [root@localhost script]# |
——————————————
Done。
seq -f ‘file_%04g’ 2 8