环境变量PATH/cp命令/mv命令/文档查看cat/more/less/head/tail

学习环境变量PATN前,我们可以先学习一个命令叫:which,which命令是用来查看某个命令的路径,以及是否有其他别名的一个命令,示范如下:

[root@localhost ~]# which rmdir

/usr/bin/rmdir

[root@localhost ~]# which rm

alias rm='rm -i'

/usr/bin/rm

[root@localhost ~]# which ls

alias ls='ls --color=auto'

/usr/bin/ls

在上面我们可以看到rmls命令是两个特殊命令,它们都有相对的别名,前面我们学习alias命令的时候学习过,rm -i中的-i是个安全参数,加上这个参数在执行rm这个命令的时候系统就会询问你是否确定要这样操作。

我们用which命令可以看到rm的绝对路径为/usr/bin/rmdir,这时你可能就会好奇,为什么在使用命令的时候,只用直接打出命令,而没有使用命令的绝对路径呢,这就是因为环境变量PATH起了作用,我们可以用echo命令来看下:

[root@localhost ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

因为/bin目录在PATH的设定中,所以自然可以找到ls,但是如果ls的命令文件不在PATH设定的目录下,则执行ls命令是就会失败,提示“没有那个文件或目录”.

[root@localhost ~]# mv /usr/bin/ls /root/

[root@localhost ~]# ls

-bash: ls: 未找到命令

[root@localhost ~]# PATH=$PATH:/root

[root@localhost ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root

[root@localhost ~]# ls

anaconda-ks.cfg  hch  ls

另外一种方法就是输入绝对路径,如下:

[root@localhost ~]# /root/ls

anaconda-ks.cfg  hch  ls

将ls文件还原:[root@localhost ~]# mv /root/ls /usr/bin/

cp命令

cp是copy(复制)的递归简写,命令的格式为:cp [参数][来源文件][目的文件],打比方我们要将word1.txt复制成word2.txt我们就可以这么操作:

[root@localhost ~]# touch word1.txt

[root@localhost ~]# ls

anaconda-ks.cfg  word1.txt

[root@localhost ~]# cp word1.txt word2.txt

[root@localhost ~]# ls

anaconda-ks.cfg  word1.txt  word2.txt

-r : 如果你要拷贝一个目录,必须要加-r选项,否则你是拷贝不了目录的, 和 ‘rm’ 类似。

[root@localhost ~]# mkdir hello

[root@localhost ~]# cd hello

[root@localhost hello]# touch 1.txt

[root@localhost hello]# cd

[root@localhost ~]# cp -r hello 88

[root@localhost ~]# cd 88

[root@localhost 88]# ls

1.txt

mv命令

mv是move的简写,该命令的格式为:mv [选项][源文件或目录][目标文件或目录],该命令有一下几种情况。

*目标文件是目录,但该目录不存在。

*目标文件是目录,且该目录存在。

*目标文件是文件,且该文件不存在。

*目标文件是文件,但该文件不存。

当目标文件是目录时,该目录存在,则会把源文件或者目录移动到该目录中。如果该目录不存在,则会把源目录重命名为给定的目录文件名。

当目标文件是文件时,如果文件存在,则会询问是否覆盖,如果文件不存在是,则会把源文件重命名为给顶的目标文件名

[root@localhost ~]# mkdir dira dirb

[root@localhost ~]# ls

88  anaconda-ks.cfg  dira  dirb  hello  word1.txt  word2.txt

[root@localhost ~]# mv dira dirc

[root@localhost ~]# ls

88  anaconda-ks.cfg  dirb  dirc  hello  word1.txt  word2.txt

目标文件为目录,并且目标目录不存在,相当于把 ‘dira’ 重命名为 ‘dirc’.

[root@localhost ~]# mv dirc dirb

[root@localhost ~]# ls

88  anaconda-ks.cfg  dirb  hello  word1.txt  word2.txt

[root@localhost ~]# ls dirb

dirc

目标文件为目录,且目标目录存在,则会把 ‘dirc’ 移动到 ‘dirb’ 目录里。

[root@localhost ~]# touch filed

[root@localhost ~]# ls

88  anaconda-ks.cfg  dirb  filed  hello  word1.txt  word2.txt

[root@localhost ~]# mv filed filee

[root@localhost ~]# ls

88  anaconda-ks.cfg  dirb  filee  hello  word1.txt  word2.txt

[root@localhost ~]# mv filee dirb

[root@localhost ~]# ls

88  anaconda-ks.cfg  dirb  hello  word1.txt  word2.txt

[root@localhost ~]# ls dirb

dirc  filee

文档查看cat/more/less/head/tail

比较常用的一个命令,即查看一个文件的内容并显示在屏幕上, 后面可以不加任何选项直接跟文件名

-n : 查看文件时,把行号也显示到屏幕上。

[root@localhost ~]# echo '111111111' > dirb/filee

[root@localhost ~]# echo  '222222222' >> dirb/filee

[root@localhost ~]# cat dirb/filee

111111111

222222222

[root@localhost ~]# cat -n dirb/filee

1 111111111

2 222222222

命令: tac

和 ‘cat’ 一样,用来把文件的内容显示在屏幕上,只不过是先显示最后一行,然后是倒数第二行,最后显示的是第一行。

[root@localhost ~]# tac dirb/filee

222222222

111111111

命令: more

也是用来查看一个文件的内容,后面直接跟文件名,当文件内容太多,一屏幕不能占下,而你用 ‘cat’ 肯定是看不前面的内容的,那么使用 ‘more’ 就可以解决这个问题了。当看完一屏后按空格键继续看下一屏。但看完所有内容后就会退出。如果你想提前退出,只需按 ‘q’ 键即可。

命令: less

作用跟more一样,后面直接跟文件名,但比more好在可以上翻,下翻。空格键同样可以翻页,而按 ‘j’ 键可以向下移动(按一下就向下移动一行),按 ‘k’ 键向上移动。在使用more和less查看某个文件时,你可以按一下 ‘/’ 键,然后输入一个word回车,这样就可以查找这个word了。如果是多个该word可以按 ‘n’ 键显示下一个。另外你也可以不按 ‘/’ 而是按 ‘?’ 后边同样跟word来搜索这个word,唯一不同的是, ‘/’ 是在当前行向下搜索,而 ‘?’ 是在当前行向上搜索。

命令head

head的命令是用来显示文件的前10行,如果加个参数-n,则显示文件的前几行,示例:

[root@localhost ~]# head /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

[root@localhost ~]#  head -2 /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

‘-n’后可以有空格也可以无空格。

命令: tail

和head一样,后面直接跟文件名,则显示文件最后十行。如果加-n 选项则显示文件最后n行。

[root@localhost ~]# tail -n2 /etc/passwd

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

chrony:x:998:996::/var/lib/chrony:/sbin/nologin

-f : 动态显示文件的最后十行,如果文件是不断增加的,则用-f 选项。如:tail-f/var/log/messages该选项特别特别常用,请熟记

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容