9月13日任务
2.10 环境变量PATH
2.11 cp命令
2.12 mv命令
2.13 文档查看cat/more/less/head/tail
环境变量PATH
我们运行命令时如ls命令,并不是使用其绝对路径形式/bin/ls,只使用相对路径形式的ls命令就可以执行,
这时因为/bin在环境变量PATH内,所以该目录的ls二进制文件就可以直接执行了。
暂时新增目录至PATH环境变量
[root@centos7 ~]# PATH=$PATH:/test
永久追加目录至PATH环境变量
在其最后一行追加 PATH = $PATH:DIR
[root@centos7 ~]# vim /etc/profile
...
PATH=$PATH:/test
保存退出,重启后生效
拷贝命令 cp
-
-r参数:拷贝目录
拷贝目录时,如果目标目录已存在,会将源目录放置到目标目录下;目标目录不存在,将目录改名为目标目录。
-
-p参数:拷贝时保留原属性
-
-i参数:拷贝时询问是否覆盖已存在文件,默认cp命令就加上了-i参数
约定:目录表示最后需加上/,如 cp /test/
# 将一个目录拷贝到另外一个目录中
# 并不是单纯将test目录下的文件拷贝到test1内,
# 而是将test目录放置到test1目录下
[root@centos7 ~]# cp -r /test /test1
[root@centos7 ~]# tree /test1
/test1
└── test
├── ls
└── ls.new
1 directory, 2 files
[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 ... /bin/ls
-rwxr-xr-x. 1 root root 117656 ... /test/ls
-rwxr-xr-x. 1 root root 117656 ... /test/ls.new
[root@centos7 ~]# alias cp
alias cp='cp -i'
移动/重命名命令 mv
mv命令(alias mv='mv -i')
-
mv的目标是目录但不存在,源目录会重命名为目标目录
-
mv的目标是目录且存在,源文件或目录会被移到该目标目录下
-
mv的目标是文件且存在,询问是否覆盖
-
mv的目标是文件但不存在,重命名为目标文件名
[root@centos7 test]# ls
dir1 dir2
[root@centos7 test]# mv dir1 dir3
[root@centos7 test]# ls
dir2 dir3
[root@centos7 test]# ls
dir1 dir2
[root@centos7 test]# mv dir1 dir2
[root@centos7 test]# tree dir2
dir2
└── dir1
1 directory, 0 files
[root@centos7 test]# ls
file1 file2
[root@centos7 test]# mv file1 file2
mv:是否覆盖"file2"? y
[root@centos7 test]# ls
file2
[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
cat
显示文本内容
[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
[root@localhost ~]# cat > 1.txt
this is a test sentence # 这个是临时输入的,不是读取的,使用ctrl-d退出编辑后会保存内容至1.txt
[root@localhost ~]# cat 1.txt
this is a test sentence
tac
倒序显示(没有-n参数,显示序号)
[root@localhost ~]# tac test.txt
e
d
c
b
a
more 将内容分屏显示,能向下(空格键)查看,向上看(ctrl + B),内容显示完自动跳出。
less
可以分屏显示,可以向上(J键或方向键的向下键)/下(K键或方向键的向上键)查看,使用q键跳出;
向后搜索查找的字符:/string ,使用n(向后)、N(向前);
使用 ?string,向前搜索,n(向前)、N(向后)。
快速跳转:gg(跳转到开头)、GG(跳转到末尾)
head(不加-n参数,默认显示前十行)
tail(不加-n参数,默认显示后十行)
tail -f file 查看动态变化的文件的后十行,常用于查看日志文件
补充说明
!$ 表示上一条命令的最后一个参数