Linux常用基本命令(rename,basename,dirname)

Posted ghostwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux常用基本命令(rename,basename,dirname)相关的知识,希望对你有一定的参考价值。

rename:重命名文件,

我下面的操作是在ubuntu16.04发行版 演示的,centos下面的语法有些不同

1,首先,生成1到100命名的.html后缀的文件

[email protected]:~/linux/rename$ ls
[email protected]:~/linux/rename$ touch {1..100}.html
[email protected]:~/linux/rename$ ls
100.html  21.html  33.html  45.html  57.html  69.html  80.html  92.html
10.html   22.html  34.html  46.html  58.html  6.html   81.html  93.html
11.html   23.html  35.html  47.html  59.html  70.html  82.html  94.html
12.html   24.html  36.html  48.html  5.html   71.html  83.html  95.html
13.html   25.html  37.html  49.html  60.html  72.html  84.html  96.html
14.html   26.html  38.html  4.html   61.html  73.html  85.html  97.html
15.html   27.html  39.html  50.html  62.html  74.html  86.html  98.html
16.html   28.html  3.html   51.html  63.html  75.html  87.html  99.html
17.html   29.html  40.html  52.html  64.html  76.html  88.html  9.html
18.html   2.html   41.html  53.html  65.html  77.html  89.html
19.html   30.html  42.html  54.html  66.html  78.html  8.html
1.html    31.html  43.html  55.html  67.html  79.html  90.html
20.html   32.html  44.html  56.html  68.html  7.html   91.html

2,通过rename+正则表达式,把.html后缀去掉

[email protected]:~/linux/rename$ rename s/\.html// *.html
[email protected]:~/linux/rename$ ls
1    14  2   25  30  36  41  47  52  58  63  69  74  8   85  90  96
10   15  20  26  31  37  42  48  53  59  64  7   75  80  86  91  97
100  16  21  27  32  38  43  49  54  6   65  70  76  81  87  92  98
11   17  22  28  33  39  44  5   55  60  66  71  77  82  88  93  99
12   18  23  29  34  4   45  50  56  61  67  72  78  83  89  94
13   19  24  3   35  40  46  51  57  62  68  73  79  84  9   95

s:表示替换,  //两个斜杠中没有内容,表示.html 替换为空

3,在文件末尾添加.html后缀

[email protected]:~/linux/rename$ ls
1    14  2   25  30  36  41  47  52  58  63  69  74  8   85  90  96
10   15  20  26  31  37  42  48  53  59  64  7   75  80  86  91  97
100  16  21  27  32  38  43  49  54  6   65  70  76  81  87  92  98
11   17  22  28  33  39  44  5   55  60  66  71  77  82  88  93  99
12   18  23  29  34  4   45  50  56  61  67  72  78  83  89  94
13   19  24  3   35  40  46  51  57  62  68  73  79  84  9   95
[email protected]:~/linux/rename$ rename s/$/\.html/ *
[email protected]:~/linux/rename$ ls
100.html  21.html  33.html  45.html  57.html  69.html  80.html  92.html
10.html   22.html  34.html  46.html  58.html  6.html   81.html  93.html
11.html   23.html  35.html  47.html  59.html  70.html  82.html  94.html
12.html   24.html  36.html  48.html  5.html   71.html  83.html  95.html
13.html   25.html  37.html  49.html  60.html  72.html  84.html  96.html
14.html   26.html  38.html  4.html   61.html  73.html  85.html  97.html
15.html   27.html  39.html  50.html  62.html  74.html  86.html  98.html
16.html   28.html  3.html   51.html  63.html  75.html  87.html  99.html
17.html   29.html  40.html  52.html  64.html  76.html  88.html  9.html
18.html   2.html   41.html  53.html  65.html  77.html  89.html
19.html   30.html  42.html  54.html  66.html  78.html  8.html
1.html    31.html  43.html  55.html  67.html  79.html  90.html
20.html   32.html  44.html  56.html  68.html  7.html   91.html

4,把.html后缀替换为.htm

[email protected]:~/linux/rename$ rename s/\.html/\.htm/ *
[email protected]:~/linux/rename$ ls
100.htm  19.htm  28.htm  37.htm  46.htm  55.htm  64.htm  73.htm  82.htm  91.htm
...

5,basename,返回路径中的文件名部分

[email protected]:~$ basename ./linux/rename/1.htm 
1.htm

6,basename, 返回路径中的文件名,不包括后缀

[email protected]:~$ basename ./linux/rename/1.htm .htm
1

7,dirname,返回文件的路径部分

[email protected]:~$ dirname ./linux/rename/1.htm 
./linux/rename

 

以上是关于Linux常用基本命令(rename,basename,dirname)的主要内容,如果未能解决你的问题,请参考以下文章

Linux常用命令汇总--rename

linux之rename命令

linux之rename命令

linux常用命令:mv 命令

Linux系统中给文件改名的命令是啥/?

linux常用命令mv命令