Linux环境变量PATH、cp和mv命令解析、文档查看相关命令集

本文索引:vim

  • 环境变量PATH
  • cp命令
  • mv命令
  • 文本查看命令集合
    • cat | tac
    • more
    • less
    • head
    • tail

环境变量PATH

咱们运行命令时如ls命令,并非使用其绝对路径形式/bin/ls,只使用相对路径形式的ls命令就能够执行,这时由于/bin在环境变量PATH内,因此该目录的ls二进制文件就能够直接执行了。centos

暂时新增目录至PATH环境变量bash

[root@centos7 ~]# PATH=$PATH:/test

永久追加目录至PATH环境变量less

在其最后一行追加 PATH = $PATH:DIR

[root@centos7 ~]# vim /etc/profile
...
PATH=$PATH:/test

保存退出,重启后生效

拷贝命令 cp

  • -r参数:拷贝目录

拷贝目录时,若是目标目录已存在,会将源目录放置到目标目录下;目标目录不存在,将目录更名为目标目录。ssh

# 将一个目录拷贝到另一个目录中
# 并非单纯将test目录下的文件拷贝到test1内,
# 而是将test目录放置到test1目录下
[root@centos7 ~]# cp -r /test /test1
[root@centos7 ~]# tree /test1
/test1
└── test
    ├── ls
    └── ls.new

1 directory, 2 files
  • -p参数:拷贝时保留原属性
[root@centos7 ~]# cp -p /bin/ls /test/
[root@centos7 ~]# cp /bin/ls /test/ls.new
[root@centos7 ~]# ls -l /test/ls /bin/ls /test/ls.new 
-rwxr-xr-x. 1 root root 117656 11月  6 2016 /bin/ls
-rwxr-xr-x. 1 root root 117656 11月  6 2016 /test/ls
-rwxr-xr-x. 1 root root 117656 10月 19 20:25 /test/ls.new
  • -i参数:拷贝时询问是否覆盖已存在文件,默认cp命令就加上了-i参数
[root@centos7 ~]# alias cp
alias cp='cp -i'

约定:目录表示最后需加上/,如 cp /test/tcp

移动/重命名命令 mv

mv命令(alias mv='mv -i')this

  • mv的目标是目录但不存在,源目录会重命名为目标目录
[root@centos7 test]# ls
dir1  dir2
[root@centos7 test]# mv dir1 dir3
[root@centos7 test]# ls
dir2  dir3
  • mv的目标是目录且存在,源文件或目录会被移到该目标目录下
[root@centos7 test]# ls
dir1  dir2
[root@centos7 test]# mv dir1 dir2
[root@centos7 test]# tree dir2
dir2
└── dir1

1 directory, 0 files
  • mv的目标是文件且存在,询问是否覆盖
[root@centos7 test]# ls
file1  file2
[root@centos7 test]# mv file1 file2
mv:是否覆盖"file2"? y
[root@centos7 test]# ls
file2
  • mv的目标是文件但不存在,重命名为目标文件名
[root@centos7 test]# touch file1
[root@centos7 test]# ls
dir2  dir3  file1
[root@centos7 test]# mv file1 file2
[root@centos7 test]# ls
dir2  dir3  file2

文档信息查看命令

  • 一次性打印出所有:cat | tac(倒序)
  • 分屏显示:more | less
  • 指定打印显示的行数: head | tail(倒序) -n num file

catcentos7

显示文本内容
[root@localhost ~]# cat test.txt 
a
b
c
d
e

显示内容同时显示序号
[root@localhost ~]# cat -n test.txt 
     1	a
     2	b
     3	c
     4	d
     5	e

cat 高级用法日志

[root@localhost ~]# cat > 1.txt
this is a test sentence # 这个是临时输入的,不是读取的,使用ctrl-d退出编辑后会保存内容至1.txt

[root@localhost ~]# cat 1.txt
this is a test sentence

taccode

倒序显示(没有-n参数,显示序号)
[root@localhost ~]# tac test.txt 
e
d
c
b
a

more 将内容分屏显示,能向下(空格键)查看,向上看(ctrl + B),内容显示完自动跳出。

[root@localhost ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
--More--(61%)

less

能够分屏显示,能够向上(J键或方向键的向下键)/下(K键或方向键的向上键)查看,使用q键跳出;

  • 向后搜索查找的字符:/string ,使用n(向后)、N(向前);
  • 使用 ?string,向前搜索,n(向前)、N(向后)。
  • 快速跳转:gg(跳转到开头)、GG(跳转到末尾)

head(不加-n参数,默认显示前十行)

[root@centos7 test]# head -n 4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

tail(不加-n参数,默认显示后十行)

[root@centos7 test]# tail -n 4 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
castiel:x:1000:1000::/home/castiel:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

tail -f file 查看动态变化的文件的后十行,经常使用于查看日志文件

补充说明

!$ 表示上一条命令的最后一个参数

相关文章
相关标签/搜索