1. 程式人生 > >環境變量PATH 、cp命令、mv命令、文檔查看cat/morless/head/tail

環境變量PATH 、cp命令、mv命令、文檔查看cat/morless/head/tail

命名 -- devel 內容 mes 查看 out profile 加載

環境變量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