Linux2.3PATH、cp、mv及文档查看

环境变量PATH

为什么可以在任何地方执行/usr/bin/ls这个命令?为什么在任何目录下输入ls就一定显示出信息而不是提示找不到/usr/bin/ls?

这是因为环境变量的帮助PATH。系统会依照PATH的设置去每个PATH定义的目录下查询文件名ls的可执行文件。

[root@chy001 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

    可以通过对PATH再赋值,添加路径。

[root@chy001 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@chy001 ~]# cp /usr/bin/ls /tmp/ls2
[root@chy001 ~]# ls2
-bash: ls2: 未找到命令
[root@chy001 ~]# /tmp/ls2 /tmp/
chyuanliu  ks-script-ZdjEMb  ls2  yum.log
[root@chy001 ~]# PATH=$PATH:/tmp/      #这种方法另起终端会失效,需对/etc/profile最后加上该行命令可永久
[root@chy001 ~]# ls2 /tmp/
chyuanliu  ks-script-ZdjEMb  ls2  yum.log
[root@chy001 ~]# !e
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/

    通过 可以直接使用绝对命令,而避开别名。

[root@chy001 ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@chy001 ~]# ls /tmp
chyuanliu  ks-script-ZdjEMb  yum.log
[root@chy001 ~]# ls /tmp
chyuanliu  ks-script-ZdjEMb  yum.log

cp

    拷贝目录约定在最后加斜杠。

    拷贝目录,当目标目录已经存在的时候,在目标目录下生成该源目录;当目标目录不存在的时候,移动源目录改名字。

[root@chy001 ~]# ls /tmp/chyuanliu/
[root@chy001 ~]# ls /tmp/
chy1  chyuanliu  chyu.txt  ks-script-ZdjEMb  ls2  yum.log
[root@chy001 ~]# cp /tmp/chy1/ /tmp/chyuanliu/
cp: 略过目录"/tmp/chy1/"
[root@chy001 ~]# ls /tmp/chyuanliu
[root@chy001 ~]# cp -r /tmp/chy1/ /tmp/chyuanliu/  #tmp有目标目录
[root@chy001 ~]# ls /tmp/chyuanliu/
chy1
[root@chy001 ~]# cp -r /tmp/chy1/ /tmp/chy2/          #tmp没有目标目录
[root@chy001 ~]# ls /tmp/
chy1  chy2  chyuanliu  chyu.txt  ks-script-ZdjEMb  ls2  yum.log

# -i 询问    -f 强制     -r递归,用于目录复制

mv

    同文件夹下相当于改名字。

[root@chy001 chy1]# ls
chy1.txt
[root@chy001 chy1]# mv chy1.txt chy2.txt
[root@chy001 chy1]# ls
chy2.txt

 异文件夹就是移动位置,若被移动的文件夹如果存在源文件会询问是否覆盖。 

[root@chy001 chy2]# mv chy2.txt /tmp/chy1/
mv:是否覆盖"/tmp/chy1/chy2.txt"? y

 移动目录,且目标目录不存在,也相当于改名字。

[root@chy001 chy2]# mv /tmp/chyuanliu/ /tmp/chy2/
[root@chy001 chy2]# ls
chy1.txt  chyuanliu

  mv一个目录时,如果目标目录已经存在,源目录会放到目标目录下面,与cp命令相同

文件查看

cat

[root@chy001 tmp]# cat 1.txt
operator:*:16231:0:99999:7:::
games:*:16231:0:99999:7:::
ftp:*:16231:0:99999:7:::
nobody:*:16231:0:99999:7:::
[root@chy001 tmp]# tac 1.txt    #相对于cat,倒序
nobody:*:16231:0:99999:7:::
ftp:*:16231:0:99999:7:::
games:*:16231:0:99999:7:::
operator:*:16231:0:99999:7:::
[root@chy001 tmp]# cat -n 1.txt      #-n显示行号
     1  operator:*:16231:0:99999:7:::
     2  games:*:16231:0:99999:7:::
     3  ftp:*:16231:0:99999:7:::
     4  nobody:*:16231:0:99999:7:::

more

相对于cat一次性将数据显示到屏幕上,more为一页一页翻动显示。掌握less就可以。

less

空格键:向下翻动一页

[Page Down]:向下翻动一页

[Page Up]:向上翻动一页

/字符串:向下查询字符串功能

?字符串:向上查询字符串

n:重复前一个查询

N:反向重复前一个查询

g:文件首行

G:文件末行

q:退出

head

[root@chy001 tmp]# head /etc/passwd      #默认显示10行
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@chy001 tmp]# head -2 /etc/passwd            #  -n 显示n行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

tail

#与head相反,默认倒数后10行
#-n 倒数后n行
[root@chy001 tmp]# tail -2 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chyuanliu:x:1000:1000::/home/chyuanliu:/bin/bash


#-f动态显示文件最后10行。如果文件不断增加,则用-f选项。查看系统日志
tail   -f   /var/log/messages

  

原文地址:https://www.cnblogs.com/chyuanliu/p/7718272.html