歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux基礎之常用命令(3)

linux基礎之常用命令(3)

日期:2017/3/3 13:53:14   编辑:Linux技術

cp

復制文件 將一個文件test復制到y目錄下,並命名為test

[code][root@iZ28g26851kZ x]# ls
test  y  z
[root@iZ28g26851kZ x]# cp test y
[root@iZ28g26851kZ x]# tree ./
./
├── test
├── y
│   └── test
└── z
    └── k

3 directories, 2 files
[root@iZ28g26851kZ x]#
將一個文件test復制到y目錄下,並命名為test.txt

[code][root@iZ28g26851kZ x]# cp test y/test.txt
[root@iZ28g26851kZ x]# tree ./
./
├── test
├── y
│   ├── test
│   └── test.txt
└── z
    └── k

3 directories, 3 files
將多個文件同時復制到其他目錄

[code][root@iZ28g26851kZ x]# cp y/test y/test.txt z/
[root@iZ28g26851kZ x]# tree ./
./
├── y
│   ├── test
│   └── test.txt
└── z
    ├── test
    └── test.txt

2 directories, 4 files
[root@iZ28g26851kZ x]#

-r

將y目錄及其目錄下的所有文件都復制到z目錄下

[code][root@iZ28g26851kZ x]# cp -r y z
[root@iZ28g26851kZ x]# tree ./
./
├── test
├── y
│   ├── test
│   └── test.txt
└── z
    ├── k
    └── y
        ├── test
        └── test.txt

4 directories, 5 files
[root@iZ28g26851kZ x]#

-p

復制文件,並復制文件的屬性(文件擁有者,文件所在組,時間戳,,,)

可以先看下不用-p的效果,先切換到其他用戶再執行cp

[code][root@iZ28g26851kZ www]# ls -l
total 0
-rw-rw-r-- 1 www www 0 May  4 15:16 txt1
[root@iZ28g26851kZ www]# cp txt1 txt2
[root@iZ28g26851kZ www]# ls -l
total 0
-rw-rw-r-- 1 www  www  0 May  4 15:16 txt1
-rw-r--r-- 1 root root 0 May  4 15:18 txt2
[root@iZ28g26851kZ www]#
可以看出,雖然文件復制成功了,但是文件所有者從www變成root了,也就是變成執行命令的用戶了,再看看使用-p後的效果

[code][root@iZ28g26851kZ www]# cp -p txt1 txt3
[root@iZ28g26851kZ www]# ls -l
total 0
-rw-rw-r-- 1 www  www  0 May  4 15:16 txt1
-rw-r--r-- 1 root root 0 May  4 15:18 txt2
-rw-rw-r-- 1 www  www  0 May  4 15:16 txt3
[root@iZ28g26851kZ www]#
這樣就把屬性也復制過來了

-a

歸檔復制,常用於備份,保存文件的一切屬性

mv

移動文件,

移動整個目錄到其他目錄下

[code][root@iZ28g26851kZ x]# tree
.
├── y
│   ├── test
│   └── test.txt
└── z

2 directories, 2 files
[root@iZ28g26851kZ x]# mv y/ z
[root@iZ28g26851kZ x]# tree
.
└── z
    └── y
        ├── test
        └── test.txt

2 directories, 2 files
[root@iZ28g26851kZ x]#
mv也用做重命名文件

[code][root@iZ28g26851kZ y]# ll
total 8
-rw-r--r-- 1 root root 9 May  4 14:43 test
-rw-r--r-- 1 root root 9 May  4 14:47 test.txt
[root@iZ28g26851kZ y]# mv test test2
[root@iZ28g26851kZ y]# ll
total 8
-rw-r--r-- 1 root root 9 May  4 14:43 test2
-rw-r--r-- 1 root root 9 May  4 14:47 test.txt
[root@iZ28g26851kZ y]#

查看文件

查看文件的命令有很多

cat,more,less,head,tail

tail命令

tail 默認查看一個文件的最後10行

-f

查看文件末尾,但並不退出,等待其他進程向其文件輸入並顯示出內容,

用處:監控日志文件打印的日志

文件處理

cut

處理每一行數據之後再顯示出來 比如說passwd文件

[code][root@iZ28g26851kZ opt]# cat /etc/passwd
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
、、、、、、、、、、、、、、、、、、、、、、、、、
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
mysql:x:500:500::/home/mysql:/bin/bash
www:x:501:501::/home/www:/bin/bash
[root@iZ28g26851kZ opt]#
當只想顯示用戶名的時候我們可以這樣

[code]cut -d: -f1
解釋:-d表示用什麼將這一行文本分割開,這裡采用分號“:”,

-f表示顯示第幾列,這裡顯示第一列

[code][root@iZ28g26851kZ opt]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
、、、、、、、、、、、、、、、、、、、、、、、、、、
sshd
tcpdump
nscd
mysql
www
[root@iZ28g26851kZ opt]#
同時顯示第一列和第3列

[code][root@iZ28g26851kZ opt]# cut -d: -f1,3 /etc/passwd
root:0
bin:1
daemon:2
、、、、、、、、、、、、、、、、、、、、、
abrt:173
sshd:74
tcpdump:72
nscd:28
mysql:500
www:501
顯示一到三列

[code][root@iZ28g26851kZ opt]# cut -d: -f1-3 /etc/passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
、、、、、、、、、、、、、、、、、
tcpdump:x:72
nscd:x:28
mysql:x:500
www:x:501

sort

[code]-n:數值排序
-r:降序
-t:列分隔符
-k:以哪個列為關鍵字
-u:排序時相同的行只顯示一次
-f:排序時忽略字符大小寫

再拿passwd文件為例,以第三列數字降序排列

[code]sort -t: -k3 -r -n /etc/passwd
結果:

[code][root@iZ28g26851kZ opt]# sort -t: -k3 -r -n /etc/passwd
www:x:501:501::/home/www:/bin/bash
mysql:x:500:500::/home/mysql:/bin/bash
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@iZ28g26851kZ opt]#

uniq

-c: 顯示文件中行重復的次數

-d: 只顯示重復的行

wc(word count)

文本統計
[code][root@iZ28g26851kZ opt]# wc test 
 82  78 610 test

解釋:82行 78個單詞 610字節

tr

字符處理命令 -d:刪除出現在字符集中的所有字符

例:

[code][root@iZ28g26851kZ opt]# tr -d w
asdaswwwwwwwwwwwasdasd
asdasasdasd
^C
[root@iZ28g26851kZ opt]#
字符替換

[code][root@iZ28g26851kZ opt]# tr a b
aaaaaaccccccccccdddddddd
bbbbbbccccccccccdddddddd
^C
[root@iZ28g26851kZ opt]#

alias

給命令起別名

alias CMDALIAS=’COMMAND [options] [arguments]’

例:

[code]alias list='ls -l'
效果:

[code][root@iZ28g26851kZ ~]# list
-bash: list: command not found
[root@iZ28g26851kZ ~]# alias list='ls -l'
[root@iZ28g26851kZ ~]# list
total 30300
drwxr-xr-x 22 root root      4096 Dec 27 22:32 libiconv-1.14
-rw-r--r--  1 root root   4984397 Aug  8  2011 libiconv-1.14.tar.gz
-rw-r--r--  1 root root   1360132 Dec 27 22:16 mhash-0.9.4.tar.gz
drwxrwxrwx 33 7155 wheel     4096 Dec 28 11:02 mysql-5.1.51
-rw-r--r--  1 root root  23830456 Dec 28 10:50 mysql-5.1.51.tar.gz
drwxr-xr-x  9 1001  1001     4096 Dec 24 17:38 nginx-1.8.0
-rw-r--r--  1 root root    832104 Dec 24 17:35 nginx-1.8.0.tar.gz
[root@iZ28g26851kZ ~]#
當然,這樣只能在當前shell生命周期中有效,

alias默認不帶任何參數則顯示當前所有的別名

[code][root@iZ28g26851kZ ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias list='ls -l'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@iZ28g26851kZ ~]#

unalias

取消別名

例: unalias list

[code][root@iZ28g26851kZ ~]# unalias list
[root@iZ28g26851kZ ~]# list
-bash: list: command not found
[root@iZ28g26851kZ ~]#

Copyright © Linux教程網 All Rights Reserved