加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux常用命令(一)

发布时间:2020-12-14 01:42:52 所属栏目:Linux 来源:网络整理
导读:linux常用命令(一) 1 touch命令 1.1 命令参数 -a change only the access time -c,--no-create do not create any files -d,--date=STRING parse STRING and use it instead of current time -f (ignored) -h,--no-dereference affect each symbolic link
linux常用命令(一)

1 touch命令

1.1 命令参数

-a                     change only the access time
    -c,--no-create        do not create any files
    -d,--date=STRING      parse STRING and use it instead of current time
    -f                     (ignored)
    -h,--no-dereference   affect each symbolic link instead of any referenced
                           file (useful only on systems that can change the
                           timestamps of a symlink)
    -m                     change only the modification time
    -r,--reference=FILE   use this file‘s times instead of current time
    -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
    --time=WORD            change the specified time:
                             WORD is access,atime,or use: equivalent to -a
                             WORD is modify or mtime: equivalent to -m
        --help     display this help and exit
        --version  output version information and exit

1.2 命令实例

touch file    创建一个名字为file的文件
    [[email?protected] shenwei]# touch file
    [[email?protected] shenwei]# ls
    file
    touch file{1..10}  创建file1—file10共10个文件
    [[email?protected] shenwei]# touch file{1..10}
    [[email?protected] shenwei]# ls
    file  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

2 mv命令

2.1 命令参数

-b :若需覆盖文件,则覆盖前先行备份。 
    -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
    -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
    -u :若目标文件已经存在,且 source 比较新,才会更新(update)
    -t : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

2.2 命令实例

1.修改文件名称

[[email?protected] shenwei]# ls
    test.txt
    [[email?protected] shenwei]# mv test.txt test.log
    [[email?protected] shenwei]# ls
    test.log

2.将多个文件移动到指定的目录中。

**note:目标目录要放置在前面**
    [[email?protected] /]# cd shen
    [[email?protected] shen]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
    [[email?protected] shen]# mv -t /shenwei file*
    [[email?protected] shen]# ls
    [[email?protected] shen]# cd ..
    [[email?protected] /]# cd shenwei
    [[email?protected] shenwei]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

3.将文件file1改名为file2,如果file2已经存在,增加参数 i 可以进行提示,省略也可以

**note:-i 可以省去不写**
    [[email?protected] shen]# cd /shenwei
    [[email?protected] shenwei]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  test.log
    [[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# mv -i file1 /shenwei
    mv: overwrite `/shenwei/file1‘? yes
    [[email?protected] shen]# ls
    file2
    [[email?protected] shen]# cd /shenwei
    [[email?protected] shenwei]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  test.log

4.将文件file1改名为file2,如果file2已经存在,则强制覆盖,增加参数 -f

[[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# ls
    file2
    [[email?protected] shen]# cd /shenwei
    [[email?protected] shenwei]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  test.log
    [[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# mv -f file2 /shenwei
    [[email?protected] shen]# cd /shenwei
    [[email?protected] shenwei]# ls
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  test.log
    [[email?protected] shenwei]#
    [[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# ls

3 cp命令

该命令的功能是将给出的文件或目录拷贝到另一文件或目录中

3.1 语法

语法: cp [选项] [源文件或目录 目标文件或目录]

3.2 命令参数

-a 该选项通常在拷贝目录时使用。他保留连接、文件属性,并且递归地拷贝目录,其作用等于后面的dpr选项的组合
    -d 拷贝时保留连接
    -f 删除已经存在的目标文件并且不进行提示
    -i 和f相反,在覆盖文件之前将给出提示要求用户确认。回答y时目标文件将被覆盖,是交互式拷贝。 此参数可以时cp命令安全,建议用户在拷贝的时候增加。
    -p 除了复制源文件中的内容之外,还将其修改时间和访问权限也复制到新文件中。
    -r 递归复制目中的文件
    -l 不做拷贝,只做链接文件
    -v —— 带有 -v (verbose)选项,cp命令将告诉用户正在做什么。很多Linux命令都带有具有相同意义的 -v 选项。

3.3 命令实例

1.将文档 file 复制成 file1 . –i为提示确认

note:这个与mv的区别,mv是重命名,而cp是进行复制文件
    [[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# ls
    file  file4
    [[email?protected] shen]# cp -i file file1
    [[email?protected] shen]# ls
    file  file1  file4
    2.将文档 file 复制到目录shemwei下,文件名仍为 file
    [[email?protected] shenwei]# ls
    file1  file10  file11  file2  file3  file4  file5  file6  file7  file8  file9  test.log
    [[email?protected] shenwei]# cd /shen
    [[email?protected] shen]# ls
    file  file1  file4
    [[email?protected] shen]# cp file /shenwei
    [[email?protected] shen]# cd /shenwei
    [[email?protected] shenwei]# ls
    file  file1  file10  file11  file2  file3  file4  file5  file6  file7  file8  file9  test.log
    [[email?protected] shenwei]#

4.递归复制目录

note:如果目标文件不存在则新建文件夹并将文件复制到其中,倘若文件夹存在则在将源文件夹及其包含的内容复制到其中。
    [[email?protected] /]# ls
    bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
    [[email?protected] /]# mkdir shen
    [[email?protected] /]# ls
    bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  shen  srv  sys  tmp  usr  var
    [[email?protected] /]# cd shen
    [[email?protected] shen]# mkdir shen{1..10}
    [[email?protected] shen]# ls
    shen1  shen10  shen2  shen3  shen4  shen5  shen6  shen7  shen8  shen9
    [[email?protected] shen]# cd ..
    [[email?protected] /]# cp -r shen shenwei
    [[email?protected] /]# ls
    bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  shen  shenwei  srv  sys  tmp  usr  var
    [[email?protected] /]# cd shenwei
    [[email?protected] shenwei]# ls
    shen1  shen10  shen2  shen3  shen4  shen5  shen6  shen7  shen8  shen9
    [[email?protected] shenwei]# cd ..
    [[email?protected] /]# cp -r shen shenwei
    [[email?protected] /]# cd shenwei
    [[email?protected] shenwei]# ls
    shen  shen1  shen10  shen2  shen3  shen4  shen5  shen6  shen7  shen8  shen9
    [[email?protected] shenwei]# cd shen
    [[email?protected] shen]# ls
    shen1  shen10  shen2  shen3  shen4  shen5  shen6  shen7  shen8  shen9
    [[email?protected] shen]#

4 cat命令

cat命令常用来查看文件内容,并且可以跟几个文件一起连接起来显示,可以跟重定向命令(后续会讲到)一起使用为文件输入或追加内容。

cat主要有三大功能:

1. 一次显示整个文件:cat filename
    2. 从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件.
    3. 将几个文件合并为一个文件:cat file1 file2 > file

4.1 语法

语法cat [选项] [文件]...

4.2 命令参数

-A,--show-all           等价于 -vET
    -b,--number-nonblank    对非空输出行编号
    -e                       等价于 -vE
    -E,--show-ends          在每行结束处显示 $
    -n,--number     对输出的所有行编号,由1开始对所有输出的行数编号
    -s,--squeeze-blank  有连续两行以上的空白行,就代换为一行的空白行 
    -t                       与 -vT 等价
    -T,--show-tabs          将跳格字符显示为 ^I
    -u                       (被忽略)
    -v,--show-nonprinting   使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

4.3 命令实例

  1. 把 Shen2019.log 的文件内容加上行号后输入 Shen2019.log.cat 这个文件中

    [[email?protected] test]# ls
    Shen2019.log
    [[email?protected] test]# cat Shen2019.log
    Shen2019-01
    Shen2019-02
    Shen2019-03
    Shen2019-04
    Shen2019-05
    Shen2019-06
    Shen2019-07
    Shen2019-08
    Shen2019-09
    Shen2019-10
    Shen2019-11
    Shen2019-12
    [[email?protected] test]# cat -n Shen2019.log > Shen2019.log.cat
    [[email?protected] test]# cat Shen2019.log.cat
         1  Shen2019-01
         2  Shen2019-02
         3  Shen2019-03
         4  Shen2019-04
         5  Shen2019-05
         6  Shen2019-06
         7  Shen2019-07
         8  Shen2019-08
         9  Shen2019-09
        10  Shen2019-10
        11  Shen2019-11
        12  Shen2019-12
    [[email?protected] test]# ls
    Shen2019.log  Shen2019.log.cat
  2. 使用here doc来生成文件

    输出:
    [[email?protected] test]# cat > log_test <<EOF
    > Hello
    > World
    > Linux
    > PWD = $(pwd)
    > EOF
    [[email?protected] test]# cat log_test
    Hello
    World
    Linux
    PWD = /test
    [[email?protected] test]#
    
    说明:
    注意粗体部分,here doc可以进行字符串替换。
    备注:
  3. tac命令 (反向列示)

    [[email?protected] test]# tac log_test
    PWD = /test
    Linux
    World
    Hello
    [[email?protected] test]#

说明:tac 是将 cat 反写过来,所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在萤幕上,而 tac 则是由最后一行到第一行反向在萤幕上显示出来!

5 head命令

用来显示文件开头的内容,如果不指定参数,则默认显示前10行

5.1 语法

语法:head[参数][文件]

5.2 命令参数

-q 隐藏文件名
    -v 显示文件名
    -c<字节> 显示字节数    
    -n<行数> 显示的行数

5.3 命令实例

  1. 显示文件的默认前10行

    [[email?protected] test]# head Shen2019.log 
    Shen2019-01
    Shen2019-02
    Shen2019-03
    Shen2019-04
    Shen2019-05
    Shen2019-06
    Shen2019-07
    Shen2019-08
    Shen2019-09
    Shen2019-10
    Shen2019-11
    Shen2019-12
  2. 显示文件的前 n 行

    [[email?protected] test]# head -n 4 Shen2019.log 
    Shen2019-01
    Shen2019-02
    Shen2019-03
    Shen2019-04
  3. 显示文件前n个字节

    [[email?protected] test]# head -c 10 Shen2019.log
    Shen2019-0[[email?protected] test]#
  4. 显示文件除了后n行之外的全部内容

    [[email?protected] test]# head -n -4 Shen2019.log 
    Shen2019-01
    Shen2019-02
    Shen2019-03
    Shen2019-04
    Shen2019-05
    Shen2019-06
    Shen2019-07
    Shen2019-08
  5. 显示文件除了最后n个字节以外的内容

    [[email?protected] test]#  head -c -40 Shen2019.log
    Shen2019-01
    Shen2019-02
    Shen2019-03
    Shen2019-04
    Shen2019-05
    Shen2019-06
    Shen2019-07
    Shen2019-08
    Shen2019[[email?protected] test]#

6 tail命令

tail 命令从指定点开始将文件写到标准输出,如果不指定参数,则默认显示后10行

6.1 语法

语法:tail[参数][文件]

6.2 命令参数

-f 循环读取 
    -q 不显示处理信息
    -v 显示详细的处理信息
    -c<数目> 显示的字节数
    -n<行数> 显示行数
    -s,--sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒 
    note:使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容.

6.3 命令实例

  1. 显示文件末尾指定的行数内同

    [[email?protected] test]# tail -n 5 Shen2019.log 
    Shen2019-08
    Shen2019-09
    Shen2019-10
    Shen2019-11
    Shen2019-12
  2. 显示默认的10行数的内容

    [[email?protected] test]# tail Shen2019.log 
    Shen2019-03
    Shen2019-04
    Shen2019-05
    Shen2019-06
    Shen2019-07
    Shen2019-08
    Shen2019-09
    Shen2019-10
    Shen2019-11
    Shen2019-12
  3. 循环查看文件的内容

    [[email?protected] ~]# ping 10.0.0.21 > test.log(‘>‘表示重定向,后面会讲到)
    [[email?protected] ~]# tail -f test.log 
    PING 10.0.0.21 (10.0.0.21) 56(84) bytes of data.
    64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms
    64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms
    64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms
    64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms
    64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms
    64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms
    64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms
    64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms
    64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读