每天2个Linux命令 cat mv


2017-07-17 每天2个Linux命令 cat命令

cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令。

注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。

(1)用法:

  用法:cat [选项] [文件]...

(2)功能:

  将[文件]或标准输入组合输出到标准输出。

(3)选项参数:

  1)-n, --number                                    对输出的所有行编号

  2) -s, --squeeze-blank                           不输出多行空行,有连续两行以上的空白行,就代换为一行的空白行

  3) -E, --show-ends                                在每行结束处显示 $

  4) -b, --number-nonblank                      对非空输出行编号

  5) -A, --show-all                                   等价于 -vET,显示不可打印字符,行尾显示“$”

  6) -T, --show-tabs                                 将跳格字符显示为 ^I

  7) -v, --show-nonprinting                      使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

  8) --help                                              显示此帮助信息并退出

  9) --version                                          输出版本信息并退出

(4)实例:

由于cat命令是查看文档的,所以首先新建文本文档test1.txt,test2.txt,test3.txt并在文档中写入内容:

方法一:

(1)首先用touch指令新建三个文档:

[sunjimeng@localhost Document]$ touch {text1.txt,text2.txt,text3.txt}
[sunjimeng@localhost Document]$ ll
总用量 0
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text3.txt

(2)用图形界面,打开文档输入数据:

(3)由于在CentOs里文档有自动备份的功能,因此这里有6个文档。其中带~符号的需要用查看备份的软件来打开: 

(4)查看shell中的文档信息:

复制代码
[sunjimeng@localhost Document]$ ll
总用量 12
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~

方法二:在shell中直接修改文档的内容:

复制代码
[sunjimeng@localhost Document]$ touch text4.txt
[sunjimeng@localhost Document]$ cat >text4.txt <<EOF
> test4's first line;
> test4's second line;
> test4's third line;
> EOF
[sunjimeng@localhost Document]$ ll
总用量 16
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:31 text4.txt     //这里并没有创建备份文件,是区别所在
[sunjimeng@localhost Document]$ 

1)[sunjimeng@localhost Document]$ cat -n text4.txt                 将包括空行在内的各行按编号输出

复制代码
[sunjimeng@localhost Document]$ cat >text4.txt <<EOF                   //先修改text4.txt的内容
> text4's first line
> 
> 
> text4's second line
> 
> text4's third line
> 
> 
> EOF

[sunjimeng@localhost Document]$ cat -n text4.txt
 1    text4's first line
 2    
 3    
 4    text4's second line
 5    
 6    text4's third line
 7    
 8    

2)[sunjimeng@localhost Document]$ cat -b text4.txt               将除空行在内的各行按编号输出

复制代码
[sunjimeng@localhost Document]$ cat -b text4.txt
 1    text4's first line


 2    text4's second line

 3    text4's third line

3)[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt        用cat命令直接输出各个文件,可以是一个也可以是多个

复制代码
[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt
test1's first line;
test1's second line;
test1's third line;
test2's first line;
test2's second line;
test2's third line;
test3's first line;
test3's second line;
test3's third line;

4)[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt             将讲text1.txt和text2.txt输出到text5.txt里,和输出到标准输出一样,也可以有-n,-b等参数

       由于这个特性,cat命令可以将多个压缩包合并成一个,可以用tar命令解压

# cat test.tar.gz_?? > test.tar.gz #可以用cat命令将被切割的多个压缩包合并成一个
# tar -xvzf test.tar.gz #再用tar命令解压
复制代码
[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt
[sunjimeng@localhost Document]$ cat text5.txt
test1's first line;
test1's second line;
test1's third line;
test2's first line;
test2's second line;
test2's third line;
[sunjimeng@localhost Document]$ 
复制代码

5)[sunjimeng@localhost Document]$ tac text5.txt                  倒序输出文件的各行内容

复制代码
[sunjimeng@localhost Document]$ tac text5.txt
test2's third line;
test2's second line;
test2's first line;
test1's third line;
test1's second line;
test1's first line;

6)[sunjimeng@localhost Document]$ cat -s text4.txt                    输出文档中的内容,如果有多个空行则用一个代替

复制代码
[sunjimeng@localhost Document]$ cat -s text4.txt                最多连续输出一个空行
text4's first line

text4's second line

text4's third line

[sunjimeng@localhost Document]$ cat text4.txt                   有多少空行,输出多少空行
text4's first line


text4's second line

text4's third line

7)[sunjimeng@localhost Document]$ cat >text6.txt                      从键盘录入内容到文件,回车是保存,退出Ctrl+z

复制代码
[sunjimeng@localhost Document]$ cat >text6.txt
I am MenAngel!                                         //除了最后一个回车之后,其余回车是文档中数据的换行并保存
Practice Order!
^Z          //回车后是Ctrl+Z命令退出
[3]+  已停止               cat > text6.txt
[sunjimeng@localhost Document]$ cat text6.txt
I am MenAngel!
Practice Order!

8)[sunjimeng@localhost Document]$ cat -E text4.txt                    输出各行文本,并且以$符结尾

复制代码
[sunjimeng@localhost Document]$ cat -E text4.txt
text4's first line$
$
$
text4's second line$
$
text4's third line$
$
$

9)[sunjimeng@localhost Document]$ cat >text6.txt <<EOF              用$取表达式的值小小范例:

[sunjimeng@localhost Document]$ cat >text6.txt <<EOF
> pwd=$(pwd)
> EOF
[sunjimeng@localhost Document]$ cat text6.txt
pwd=/home/sunjimeng/Document

10)[sunjimeng@localhost Document]$ cat --help

复制代码
[sunjimeng@localhost Document]$ cat --help
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。

  -A, --show-all           等于-vET
  -b, --number-nonblank    对非空输出行编号
  -e                       等于-vE
  -E, --show-ends          在每行结束处显示"$"
  -n, --number             对输出的所有行编号
  -s, --squeeze-blank      不输出多行空行
  -t                       与-vT 等价
  -T, --show-tabs          将跳格字符显示为^I
  -u                       (被忽略)
  -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

如果没有指定文件,或者文件为"-",则从标准输入读取。

示例:
  cat f - g  先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
  cat        将标准输入的内容复制到标准输出。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误
要获取完整文档,请运行:info coreutils 'cat invocation'

11)[sunjimeng@localhost Document]$ cat --version

复制代码
[sunjimeng@localhost Document]$ cat --version
cat (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由Torbjörn Granlund 和Richard M. Stallman 编写。

2017-07-17 每天2个Linux命令 mv命令

mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。

注意事项:mv与cp的结果不同,mv好像文件“搬家”,文件个数并未增加。而cp对文件进行复制,文件个数增加了。


(1)用法:

 用法:   mv [选项]...   [-T]    源文件    目标文件

 或:   mv [选项]...             源文件... 目录

 或:   mv [选项]...   -t        目录       源文件...

(2)功能:

   将源文件重命名为目标文件,或将源文件移动至指定目录。

(3)选项参数:

  1) -b:     当文件存在时,覆盖前,为其创建一个备份

  2) -f       若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录 

  3)  -i       交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名
            ,则询问用户是否覆盖目标文件。这样可以避免误将文件覆盖。

  4) -f  -force      强制的意思,如果目标文件已经存在,不会询问而直接覆盖

  5) -u    若目标文件已经存在,且 source 比较新,才会更新(update)

(4)实例:

 1)[sunjimeng@localhost Document]$ mv text1 mytext  由于此处源文件test1与目标文件是在同一目录下,
    可以看作仅仅是改了文件的名字 

复制代码
[sunjimeng@localhost Document]$ ll                        //目录下为空
总用量 0
[sunjimeng@localhost Document]$ cat >text1 <<EOF         //新建文件文档并从标准输入中输入数据到文件
> I am MenAngel
> PWD=$(pwd)
> I am testing the order of mv!
> EOF
[sunjimeng@localhost Document]$ ll
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 text1
[sunjimeng@localhost Document]$ mv text1 mytext         //执行mv命令
[sunjimeng@localhost Document]$ cat mytext
I am MenAngel
PWD=/home/sunjimeng/Document
I am testing the order of mv!
[sunjimeng@localhost Document]$ ll                     //可见已经改名
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext
[sunjimeng@localhost Document]$ 
复制代码

2)[sunjimeng@localhost Document]$ mv mytext{,.txt} 与[sunjimeng@localhost Document]$ mv text text.txt          给文件名增加后缀

复制代码
[sunjimeng@localhost Document]$ mv mytext{,.txt}                   //增加后缀名的原始方法
[sunjimeng@localhost Document]$ ll
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
[sunjimeng@localhost Document]$ touch text
[sunjimeng@localhost Document]$ ll
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text
[sunjimeng@localhost Document]$ mv text text.txt                   //利用mv的改名目录增加后缀
[sunjimeng@localhost Document]$ ll
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text.txt

3)[root@localhost Documents]# mv ../Document/* . 将文件从源目录移动到目标目录,这里源目录和目标目录可以任意指定。
.代表当前目录

复制代码
[sunjimeng@localhost Document]$ ll    //Document下游两个文件
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text.txt
[sunjimeng@localhost Document]$ cd ../Documents  //进入同级兄弟目录Documents,发现其下为空
[sunjimeng@localhost Documents]$ ll
总用量 0
[sunjimeng@localhost Documents]$ mv ../Document/* .  //将Document下的所有文件(*),移动到当前目录(.)。
mv: 无法将"../Document/mytext.txt" 移动至"./mytext.txt": 权限不够   //Linux用组名和用户名来管理文件,
此时当前用户没有权限移动文件,必须改为root用户
mv: 无法将"../Document/text.txt" 移动至"./text.txt": 权限不够    
[sunjimeng@localhost Documents]$ su root
密码:
ABRT 已检测到 '1' 个问题。预了解详细信息请执行:abrt-cli list --since 1462423345
[root@localhost Documents]# mv ../Document/* .
[root@localhost Documents]# ll                                   //移动完成
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text.txt
[root@localhost Documents]# ls -l ../Document                    //查看Document目录已经没有任何东西
总用量 0
复制代码

4)[root@localhost Documents]# mv -t ../Document ./*   功能同(3),但区别是源文件的路径和目标路径的位置发生了变化

复制代码
[root@localhost Documents]# ll
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text.txt
[root@localhost Documents]# mv -t ./* ../Document   //-t参数的功能就是让他们的位置发生变化,
这里第一个参数是目标路径
mv: 目标"./mytext.txt" 不是目录
[root@localhost Documents]# mv -t ../Document ./*   //位置调换一下就行了
[root@localhost Documents]# ll
总用量 0
[root@localhost Documents]# ll
总用量 0
[root@localhost Documents]# ls -l ../Document
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text.txt

5)[root@localhost Document]# mv mytext.txt mytext  如果第二个参数不是目录名,才将源文件改名,
    否则,移动源文件到该目录下(与实例1作比较)

复制代码
[root@localhost Document]# mkdir mytext         
[root@localhost Document]# ll
总用量 4
drwxr-xr-x. 2 root      root       6 5月   5 22:34 mytext
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text
[root@localhost Document]# mv mytext.txt mytext  /与实例一不同的是,这里mytext是个目录
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root      root      23 5月   5 22:35 mytext
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   5 22:08 text
[root@localhost Document]# ls -l mytext
总用量 4
-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月   5 22:02 mytext.txt 

6)[root@localhost Document]# mv -b myword text    源文件和目标文件都是存在的,因此会有覆盖提醒,-b用于在覆盖时备份文件 

复制代码
[root@localhost Document]# cat >myword <<EOF
> this is my word!
> EOF
[root@localhost Document]# cat >text <<EOF
> this is my text!
> EOF
[root@localhost Document]# mv -b myword text  //在一个文件即将覆盖另一个文件时,默认是提醒的,
所以加上-i参数和不加是一样的
mv:是否覆盖"text"? y
[root@localhost Document]# cat myword
cat: myword: 没有那个文件或目录
[root@localhost Document]# cat text
this is my word!
[root@localhost Document]# ll
总用量 8
drwxr-xr-x. 2 root      root      23 5月   5 22:35 mytext  //这里text里存的是前面myword的内容,
text的内容备份到text~中,需要特殊软件才能查看
-rw-r--r--. 1 root      root      17 5月   5 22:41 text
-rw-rw-r--. 1 sunjimeng sunjimeng 17 5月   5 22:41 text~

7) [root@localhost text]# mv * ../                                将当前目录下的所有内容移动到父级目录(特殊情况)

复制代码
[root@localhost Document]# mkdir text
[root@localhost Document]# touch ./text/{text1,text2,text3}
[root@localhost Document]# cd text
[root@localhost text]# mv * ../
[root@localhost text]# cd ../
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月   5 22:57 text
-rw-r--r--. 1 root root 0 5月   5 22:57 text1
-rw-r--r--. 1 root root 0 5月   5 22:57 text2
-rw-r--r--. 1 root root 0 5月   5 22:57 text3

8)[root@localhost Document]# mv -f text2 text3  强制执行操作,并不做任何提醒

9)[root@localhost Document]# mv -i text2 text3  加不加-i在覆盖时都会提醒

复制代码
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月   5 23:05 text
-rw-r--r--. 1 root root 0 5月   5 22:57 text2
-rw-r--r--. 1 root root 0 5月   5 22:57 text3
-rw-r--r--. 1 root root 0 5月   5 22:57 text4
[root@localhost Document]# mv text2 text3
mv:是否覆盖"text3"? n
[root@localhost Document]# mv -i text2 text3
mv:是否覆盖"text3"? n
[root@localhost Document]# mv -f text2 text3
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 2 root root 6 5月   5 23:05 text
-rw-r--r--. 1 root root 0 5月   5 22:57 text3
-rw-r--r--. 1 root root 0 5月   5 22:57 text4

10)[root@localhost Document]# mv Dir text 将Dir目录移动到text目录下(text存在时),如果不存在直接将Dir改名为text

复制代码
[root@localhost Document]# mkdir testDir
[root@localhost Document]# ll                       //下面的操作先将文件text3和text4放到textDir目录下
总用量 0
drwxr-xr-x. 2 root root 6 5月   5 23:09 testDir
drwxr-xr-x. 2 root root 6 5月   5 23:05 text
-rw-r--r--. 1 root root 0 5月   5 22:57 text3
-rw-r--r--. 1 root root 0 5月   5 22:57 text4
[root@localhost Document]# mv {text3,text4} ./testDir
[root@localhost Document]# mv testDir Dir          //由于Dir不存在,所以testDir改名为Dir
[root@localhost Document]# mv Dir text             //由于text是存在的,所以将Dir移到text目录下
[root@localhost Document]# ll
总用量 0
drwxr-xr-x. 3 root root 16 5月   5 23:10 text      //下面验证了这一点
[root@localhost Document]# cd text
[root@localhost text]# ll
总用量 0
drwxr-xr-x. 2 root root 30 5月   5 23:09 Dir
[root@localhost text]# cd Dir
[root@localhost Dir]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月   5 22:57 text3
-rw-r--r--. 1 root root 0 5月   5 22:57 text4

11)[root@localhost /]# mv --help

复制代码
[root@localhost /]# mv --help
用法:mv [选项]... [-T] 源文件 目标文件
 或:mv [选项]... 源文件... 目录
 或:mv [选项]... -t 目录 源文件...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       为每个已存在的目标文件创建备份
  -b                           类似--backup 但不接受参数
  -f, --force                  覆盖前不询问
  -i, --interactive            覆盖前询问
  -n, --no-clobber             不覆盖已存在文件
如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
      --strip-trailing-slashes    去掉每个源文件参数尾部的斜线
  -S, --suffix=SUFFIX        替换常用的备份文件后缀
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告mv 的翻译错误
要获取完整文档,请运行:info coreutils 'mv invocation'

12)[root@localhost /]# mv --version

复制代码
[root@localhost /]# mv --version
mv (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由Mike Parker、David MacKenzie 和Jim Meyering 编写。

(5)其他:

  用-b做备份时:      

  -b 不接受参数,mv会去读取环境变量VERSION_CONTROL来作为备份策略。

  --backup该选项指定如果目标文件存在时的动作,共有四种备份策略:

    1.CONTROL=none或off : 不备份。

    2.CONTROL=numbered或t:数字编号的备份

    3.CONTROL=existing或nil:如果存在以数字编号的备份,则继续编号备份m+1...n:

    执行mv操作前已存在以数字编号的文件log2.txt.~1~,那么再次执行将产生log2.txt~2~,以次类推。
    如果之前没有以数字编号的文件,则使用下面讲到的简单备份。

    4.CONTROL=simple或never:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录