Linux常用基礎(chǔ)命令
來源:程序員人生 發(fā)布時間:2015-01-15 08:26:12 閱讀次數(shù):3858次
Linux基礎(chǔ)命令
-----------------目錄部份-------------------
【pwd】顯示當(dāng)前所在的絕對目錄
【cd】 切換目錄
cd - 顯示上1個工作目錄
cd ~ 顯示當(dāng)前用戶的家目錄
cd . 顯示當(dāng)前目錄
cd .. 顯示當(dāng)前目錄的上1級目錄
[root@centos ~]# cd /usr/
[root@centos usr]# ls
bin etc games include lib libexec local my sbin share src tmp
[root@centos usr]# cd -
/root
[root@centos ~]# cd .
[root@centos ~]# cd ..
[root@centos /]# cd ~
[root@centos ~]# pwd
/root
【ls】 顯示當(dāng)前所在的目錄文件列表
ls -a 顯示所有文件(包括隱藏文件)
ls -l 與ll命令相同 以列表情勢顯示所有文件
ls -t 按時間排序顯示文件
ls -m 橫向輸出文件名 并以','分割
ls -R 遞歸顯示當(dāng)前目錄及其子目錄
ls -S 以文件大小排序顯示文件
root@centos ~]# ls -R
.:
anaconda-ks.cfg install.log.syslog lnmp-install.log
install.log lnmp vhost.sh
[root@centos ~]# ls -a
. .bash_logout .gconf .lesshst vhost.sh
.. .bash_profile .gconfd lnmp .viminfo
anaconda-ks.cfg .bashrc install.log lnmp-install.log
.bash_history .cshrc install.log.syslog .tcshrc
[root@centos ~]# ls -t
lnmp-install.log lnmp install.log
vhost.sh anaconda-ks.cfg install.log.syslog
[root@centos ~]# ls -l
-rw-------. 1 root root 1383 11 7 05:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 43838 11 7 05:25 install.log
-rw-r--r--. 1 root root 10033 11 7 05:23 install.log.syslog
-rwxr-xr-x. 1 root root 1930 11 7 08:00 lnmp
-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log
-rwxr-xr-x. 1 root root 5307 11 7 08:01 vhost.sh
[root@centos ~]# ls -m
anaconda-ks.cfg, install.log, install.log.syslog, lnmp, lnmp-install.log,
vhost.sh
[root@centos ~]# ls -Sl
1848
-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log
-rw-r--r--. 1 root root 43838 11 7 05:25 install.log
-rw-r--r--. 1 root root 10033 11 7 05:23 install.log.syslog
-rwxr-xr-x. 1 root root 5307 11 7 08:01 vhost.sh
-rwxr-xr-x. 1 root root 1930 11 7 08:00 lnmp
-rw-------. 1 root root 1383 11 7 05:25 anaconda-ks.cfg
【tree】以目錄樹 打印目錄結(jié)構(gòu)
【mkdir】創(chuàng)建目錄
mkdir ./aaa 在當(dāng)前目錄下創(chuàng)建1個aaa目錄(只能創(chuàng)建1層目錄)
mkdir -p ./aaa/bbb/ccc 創(chuàng)建多個目錄(可以創(chuàng)建多層目錄)
[root@centos admin]# mkdir /aaa
[root@centos admin]# rm /aaa
rm: 沒法刪除"/aaa": 是1個目錄
[root@centos admin]# rm -f /aaa
rm: 沒法刪除"/aaa": 是1個目錄
[root@centos admin]# rm -rf /aaa
[root@centos admin]# mkdir ./aaa
[root@centos admin]# ls
aaa
[root@centos admin]# mkdir ./aaa/bbb/ccc
mkdir: 沒法創(chuàng)建目錄"./aaa/bbb/ccc": 沒有那個文件或目錄
[root@centos admin]# mkdir -p ./aaa/bbb/ccc
[root@centos admin]# ls
aaa
[root@centos admin]# tree
.
├── aaa
│ └── bbb
│ └── ccc
【rmdir】刪除目錄
rmdir 只能刪除1個空目錄
[root@centos admin]# rmdir ./aaa
rmdir: 刪除 "./aaa" 失敗: 目錄非空
[root@centos admin]# rmdir ./aaa/bbb/ccc
[root@centos admin]# tree
.
├── aaa
│ └── bbb
rm也可刪除目錄(可以強迫刪除非空目錄 請謹(jǐn)慎使用)
[root@centos admin]# rm /aaa
rm: 沒法刪除"/aaa": 是1個目錄
[root@centos admin]# rm -f /aaa
rm: 沒法刪除"/aaa": 是1個目錄
[root@centos admin]# rm -rf /aaa
[root@centos admin]#
------------------文件部份-------------------------
【touch】創(chuàng)建文件命令
touch 文件名
touch -d yyyymmdd 文件名 修改文件創(chuàng)建時間
[root@centos aaa]# ls
bbb
[root@centos aaa]# touch a.txt
[root@centos aaa]# ll
總用量 4
-rw-r--r-- 1 root root 0 12月 25 21:32 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:21 bbb
[root@centos aaa]# touch -d 20150102 a.txt
[root@centos aaa]# ll
總用量 4
-rw-r--r-- 1 root root 0 1月 2 2015 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:21 bbb
【cp】復(fù)制文件命令
cp 源文件 目標(biāo)文件
cp -r 源文件 目標(biāo)文件 //會復(fù)制全部源文件
[root@centos aaa]# cp -r ./ /var/c
[root@centos c]# cd /var/c
[root@centos c]# ll
總用量 8
-rw-r--r-- 1 root root 0 12月 25 21:42 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:42 bbb
固然我們還可以用 ./* 代表當(dāng)前目錄下的所有文件
再進(jìn)行拷貝也能夠
【mv】移動文件 或 重命名文件
mv 源文件 目標(biāo)文件
【cat】查看文件 從第1行開始往下查看
【tac】從最后1行開始往上查看
【file】 顯示文件類型
file 文件名
[root@centos aaa]# file a.txt
a.txt: ASCII text
【more】1頁1頁查看文件內(nèi)容
more 文件名
/字符串
?字符串
都可以用來搜索指定的字符串
b 或 ctrl + b 向上翻頁
空格 或 回車 向下翻頁
q退出
【less】同more
n 向下翻頁
N 向上翻頁
q 退出
【head】顯示文件內(nèi)容的頭幾行
head -n 數(shù)字 文件名
【tail】顯示文件內(nèi)容的尾幾行
tail -n 數(shù)字 文件名
【man】手冊使用查找命令
-------------------系統(tǒng)部份----------------------
【init】系統(tǒng)命令
參數(shù):
init 0 關(guān)機
init 6 重啟
【shutdown】關(guān)機/正告
shutdown -t 秒數(shù) 幾秒后自動關(guān)機
shutdown -r 重啟
shutdown -h 立即關(guān)機
-------------------命令起別名----------------------
【alias】這個需要修改配置文件
首先:切換到用戶的家目錄 cat ~
其次:編輯文件 .bash vi .bash_profile
再次:在文件中添加 你要修改的命令及其 別名
alias rm = '/bin/rm -f'
保存并退出:esc + !wq
source 1下:source .bash_profile
原理:系統(tǒng)在用戶登錄初始化時 會自動加載 .bash_profile文件
source 其實就是再次加載、履行1下該文件
他與 直接使用 ./XX.sh 履行的shell 腳本不同的是
他在全局范圍內(nèi)有效,shell只在關(guān)聯(lián)目錄內(nèi)有效
-------------------查找基礎(chǔ)命令---------------------
【find】
按時間查找
-atime 讀或履行時更改文件的時間 access time 相當(dāng)于lu -l
-mtime 寫入文件時更改文件的時間 make time 相當(dāng)于ls -l
-ctime 寫入文件、更改權(quán)限、所有者、文件鏈接 相當(dāng)于lc -l
或設(shè)置lnode時間戳而設(shè)置的更改時間 create time
1般使用的命令:
find ./ -mtime n 按時間查找 n天以內(nèi)被修改的文件
-mtime +n n天之前 不包括第n天被修改的文件
-mtime -n n天以后 包括第n天
如 -mtime +3 今天是0103 那末就是查找 1231及之前的所有修改過的文件
-mtime ⑶ 今天是0103 那末就是查找 0101-0103內(nèi)被修改過的文件
find ./ -newer file1 新于文件file1的文件
find ./ -name filename 文件名包括filename的文件
[特殊作用命令]
find ./ -name filename -exec ls -l {} ;
[root@centos ~]# find ./ -name lnmp -exec ls -l "{}" ;
-rwxr-xr-x. 1 root root 1930 11月 7 08:00 ./lnmp
比如我們要將查到的.conf文件都刪除掉
find / -name "*.conf" -exec rm -rf {} ;
又比如我們要拷貝 查到的文件
find / -name "*.conf" -exec cp {} /home/my/ ;
重命名.txt 文件的后綴名改成.txtd
find ./ -name "*.txt" -exec mv "{}" "{}d" ;
解釋:
-exec 參數(shù)后面跟的是command命令,它的終止是以;為結(jié)束標(biāo)志的,
所以這句命令后面的分號是不可缺少的,
斟酌到各個系統(tǒng)中分號會有不同的意義,所之前面加反斜杠。
{} 花括號代表前面find查找出來的文件名。
【history】 查看歷史履行過的命令
history 查看歷史履行命令
history -c 清空歷史履行命令
【echo】打印
echo $PATH 打印環(huán)境變量目錄 以:分割
【grep】管道抓取命令
-c 只輸出匹配行的計數(shù)
-i 不辨別大小寫(只適用于單字符)
-n 顯示匹配的行及行號
-s 不顯示不存在時的毛病信息
-v 顯示不包括匹配文本的所有行
-l 完全匹配行信息
[root@centos ~]# cat a.txt | grep h
5,6,h
7,8,9,h
10,h
[root@centos ~]# cat a.txt | grep -v h
1,n,2
3,4,n
[root@centos ~]# cat a.txt | grep -n h
3:5,6,h
4:7,8,9,h
5:10,h
[root@centos ~]#
下面這些命令 1般不獨立使用 而是在管道后面使用
sort -r 反向排序
cut -d'分隔符' -f取第幾段 多個字段用逗號分隔
uniq -c 計數(shù) //1般在sort后面使用 否則統(tǒng)計可能無效
wc -l 統(tǒng)計出現(xiàn)結(jié)果行數(shù)
[root@centos ~]# ll | cut -d' ' -f5⑴3 | sort -s | uniq -c
1
1 10033 11月 7 05:23 install.log.syslog
1 1383 11月 7 05:25 anaconda-ks.cfg
1 1810073 11月 7 08:01 lnmp-install.log
1 1930 11月 7 08:00 lnmp
1 43838 11月 7 05:25 install.log
1 5307 11月 7 08:01 vhost.sh
[root@centos ~]# ll | cut -d' ' -f5⑴3 | sort -s | wc -l
7
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進(jìn)行捐贈