码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-30 22:54:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:root   存在   bin   art   last   emd   自动   direct   name   

环境变量PATH
[root@localhost ~]# which ll
alias ll=‘ls -l --color=auto‘
/usr/bin/ls
用which可以查看命令所在的路径和有没有别名,那么他是怎么查到的呢?而且查的这么快?
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
实际上它是从以上几个目录中去找到的,如果你输入的命令在这几个目录中,那么你就不用输入绝对路径。我们可以做一个小实验,我们先拷贝ls命令到/tmp/ls2
[root@localhost ~]# cp /usr/bin/ls /tmp/ls2
[root@localhost ~]# /tmp/ls2
anaconda-ks.cfg
[root@localhost ~]# ls
anaconda-ks.cfg
然后我们执行两个命令,发现这两个命令得到的结果是一样的,如果我们直接输入ls2呢?
[root@localhost ~]# ls2
-bash: ls2: 未找到命令
原因就是ls2没有在以上的文件目录中,如果我们想用ls2呢,那我们可以将tmp目录加入到PATH中
[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
如果我们打开另外一个终端,就会发现ls2这个命令失效了,那我们如何能让每个终端每次都有效呢?我们可以 vi /etc/profile 然后再最后一行写入PATH=$PATH:/tmp/然后保存退出,这样每次开机和打开新的终端时都能加载PATH变量了。
如果我们不想要这个了,那么我们就让PATH把它去掉
[root@localhost ~]# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# ls2
-bash: ls2: 未找到命令

cp拷贝命令
[root@localhost ~]# cp /etc/passwd /tmp/1.txt
拷贝目录的时候我们要加-r
[root@localhost ~]# cp -r /tmp/aminglinux/ /tmp/aming
[root@localhost ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
├── 1
│?? └── 2
└── 4
└── 5
└── 6

5 directories, 0 files
[root@localhost ~]# tree /tmp/aming
/tmp/aming
├── 1
│?? └── 2
└── 4
└── 5
└── 6

5 directories, 0 files
可以发现两个是一样的,但是我们发现拷贝的源我们加了/,而拷贝的目标我们没有加/,那我们在这里约定一下,所有目录应该要加/。再有一点就是,我们拷贝文件的时候会有提示是否覆盖,而拷贝目录的时候则没有提示。
[root@localhost ~]# cp -r /tmp/aminglinux/ /tmp/aming1/
[root@localhost ~]# tree /tmp/aming1/
├── 1
│?? └── 2
├── 4
│?? └── 5
│?? └── 6
└── aminglinux
├── 1
│?? └── 2
└── 4
└── 5
└── 6

11 directories, 0 files
这说明当目标目录已经存在时,源目录会直接放在目标目录下面去,如果目标目录不存在,那么他会把源目录拷贝过来改个目标目录的名子。

mv移动命令
mv - move (rename) files 移动或重命名文件
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1402 12月 14 22:02 anaconda-ks.cfg
[root@localhost ~]# mv anaconda-ks.cfg anaconda-ks.cfg.1
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1402 12月 14 22:02 anaconda-ks.cfg.1
改名成功
当目标目录不存在的时候,mv也是完成改名。如果目标目录存在,他就会把源目录放到目标目录下面去。

移动和改名同时完成
[root@localhost tmp]# mv 1.txt /root/2.txt
[root@localhost tmp]# ls /root
2.txt anaconda-ks.cfg.1
[root@localhost tmp]#

文本文档相关的命令
cat查看文本文档,加参数-n可以显示行号
[root@localhost ~]# cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19 chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20 aming:x:1000:1000::/home/aming:/bin/bash

more分屏显示,按空格键可以向下翻,看到最后一行时自动退出。

less查看

/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n:重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
空格键 滚动一行
回车键 滚动一页

12.head
head - output the first part of files
显示文件头部内容(默认前十行)
命令参数:
-n 显示前n行数据
[root@localhost ~]# head -n 1 anaconda-ks.cfg.1
#version=DEVEL
显示文件前1行

tail
tail - output the last part of files
显示文件尾部文件(默认后十行)
[root@localhost ~]# tail -n 1 anaconda-ks.cfg.1
aming:x:1000:1000::/home/aming:/bin/bash
显示文件后1行,如果加参数-f可以看到文件尾的变化。

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

标签:root   存在   bin   art   last   emd   自动   direct   name   

原文地址:http://blog.51cto.com/13067688/2067066

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!