Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145041
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-04 11:19
文章分类

全部博文(70)

文章存档

2018年(69)

2016年(1)

我的朋友

分类: LINUX

2018-06-02 21:31:12

2.10 环境变量PATH
使用which可以查看命令文件所在位置。可是它是从哪里查到的呢?同时还有find命令查找文件,为什么会比which慢呢。
因为,which是在环境变量的相关目录中进行查找,而find需要根据指定目录去查找。
[root@localhost ~]# echo $PATH                                             //目录之间使用冒号做分割。
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

也因为有环境变量,所以才可以直接输入命令执行而不需要输入绝对路径。执行命令使用绝对路径也是可以执行的。

[root@localhost ~]# which ls                        //查找ls命令文件所在位置
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# cp /usr/bin/ls /tmp/ls2        //复制该文件并改名
[root@localhost ~]# /tmp/ls2                            //执行效果与ls命令是一样的
anaconda-ks.cfg
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# ls2            //但是无法直接执行,因为环境变量中没有指定/tmp目录
-bash: ls2: 未找到命令

[root@localhost ~]# PATH=$PATH:/tmp/              //临时添加一个环境变量,仅在添加终端生效
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[root@localhost ~]# ls2                                //添加过环境变量后,可以直接执行了
anaconda-ks.cfg
[root@localhost ~]# which ls2
/tmp/ls2

想要添加的环境变量永久生效:
[root@localhost ~]# vim /etc/profile          //此文件每次系统启动都是加载,添加在最后一行即可
[root@localhost ~]# tail -1 /etc/profile       //打开一个新的终端,发现修改依旧是生效的
PATH=$PATH:/tmp/

想要取消:首先改配置文件,然后再重新设置环境变量        //配置文件怎么改,之后会讲。
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[root@localhost ~]# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# ls2
bash: ls2: 未找到命令


2.11  cp命令
cp = copy 拷贝。复制文件到目标位置,甚至同时改名。

[root@localhost ~]# cp -r /tmp/test/ /tmp/linux            //拷贝目录需要 -r 选项
拷贝前拷贝后的路径是否需要后面的“/”
约定:Linux中目录是默认有”/“结尾。使用cp命令时,改名也需要加上”/“    //下面的例子。
[root@localhost ~]# cp -r /tmp/test/ /tmp/test2/
[root@localhost ~]# tree !$                                    //调用上一条命令的最后一个参数
tree /tmp/test2/
/tmp/test2/
├── 1
│   └── a.txt
└── 2
2 directories, 1 file

[root@localhost ~]# cp /tmp/test/1/a.txt /tmp/test/2/
cp:是否覆盖"/tmp/test/2/a.txt"? n        //因为cp被定义别名有-i选项,所以会提示
[root@localhost ~]# which cp
alias cp='cp -i'
/usr/bin/cp

[root@localhost ~]# /usr/bin/cp /tmp/test/1/a.txt /tmp/test/2/  //不需要提示则使用命令文件即可

[root@localhost ~]# cp -r /tmp/test/1/ /tmp/test/2/
[root@localhost ~]# ls /tmp/test/2                    //当目标目录存在时,将源目录放到该目录下
1  a.txt                                                            //当目标目录不存在,则复制过去并改名。


2.12  mv命令
mv = move  移动。同目录下移动,则是改名

[root@localhost ~]# tree /tmp/
/tmp/
├── one
│   └── 1.txt
└── two
    └── 2.txt
2 directories, 2 files
[root@localhost ~]# mv /tmp/one/ /tmp/three/            //改名
[root@localhost ~]# tree /tmp/
/tmp/
├── three
│   └── 1.txt
└── two
    └── 2.txt
2 directories, 2 files

[root@localhost ~]# mv /tmp/three/1.txt /tmp/two/2.txt
mv:是否覆盖"/tmp/two/2.txt" n    //mv命令同样有-i别名,所以,不需要提示使用文件绝对路径

[root@localhost ~]# mv /tmp/three/ /tmp/two/
[root@localhost ~]# !tree               //目标目录存在则是移动到那个目录下面,不存在就是改名。
tree /tmp/
/tmp/
└── two
    ├── 2.txt
    └── three
        └── 1.txt
2 directories, 2 files

2.13 文档查看命令 cat,more,less,head,tail
cat:
cat查看文件全部内容。tac倒叙查看文件内容。
cat -A 查看所有字符,包括换行符等特殊符号。
cat -n 查看文件的同时,输出行号。

more:
分页显示文件内容,分屏展示。
通过”空格“,”回车“操作,进行向下翻页和翻行的操作。看完之后,自动退出。”ctrl+b“/b向上看。

anaconda-ks.cfg   //自动装系统会使用到的文件

less:
刚进入和more显示差不多,但是支持方向键上下查看。”ctrl+f“,往后看;”ctrl+b“,往前看.并且看到最后不会自动退出,需要使用字母”q“退出,并且退出后会发现,查看的内容不会输出到命令行前端
可以使用”/“从上往下搜索内容,高亮显示搜索的字符串;通过”n“向下查看下一个搜索字符串,通过”N“向上查看下一个搜索字符串
可以使用”?“从下往上搜索内容,高亮显示搜索的字符串;通过”n“向上查看下一个搜索字符串,通过”N“向下查看下一个搜索字符串   //与”/“的搜索方向都是反的。
”shift+g“跳转到文本的最后/末行。 ”g“跳转到文本的开头/首行

head:
默认查看文件的前十行,可以指定行数。
tail:
默认查看文件的末尾十行,可以指定行数。
[root@localhost ~]# head -n 2 anaconda-ks.cfg 
#version=DEVEL
# System authorization information
[root@localhost ~]# tail -n 2 anaconda-ks.cfg
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@localhost ~]# head -2 anaconda-ks.cfg 
#version=DEVEL
# System authorization information
[root@localhost ~]# tail -2 anaconda-ks.cfg
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

tail -f:动态跟踪查看文件内容。首先输出文件的最后十行,然后跟踪输出,不会自动退出,需要手动”ctrl+c“结束执行。


就是上图这样~~一般看日志的时候会用到。

阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~