linux基础之常用命令(3)

来源:互联网 发布:2016年4月进出口数据 编辑:程序博客网 时间:2024/06/06 03:18

cp


复制文件
将一个文件test复制到y目录下,并命名为test

[root@iZ28g26851kZ x]# lstest  y  z[root@iZ28g26851kZ x]# cp test y[root@iZ28g26851kZ x]# tree ././├── test├── y│   └── test└── z    └── k3 directories, 2 files[root@iZ28g26851kZ x]# 

将一个文件test复制到y目录下,并命名为test.txt

[root@iZ28g26851kZ x]# cp test y/test.txt[root@iZ28g26851kZ x]# tree ././├── test├── y│   ├── test│   └── test.txt└── z    └── k3 directories, 3 files

将多个文件同时复制到其他目录

[root@iZ28g26851kZ x]# cp y/test y/test.txt z/[root@iZ28g26851kZ x]# tree ././├── y│   ├── test│   └── test.txt└── z    ├── test    └── test.txt2 directories, 4 files[root@iZ28g26851kZ x]# 

-r

将y目录及其目录下的所有文件都复制到z目录下

[root@iZ28g26851kZ x]# cp -r y z[root@iZ28g26851kZ x]# tree ././├── test├── y│   ├── test│   └── test.txt└── z    ├── k    └── y        ├── test        └── test.txt4 directories, 5 files[root@iZ28g26851kZ x]# 

-p

复制文件,并复制文件的属性(文件拥有者,文件所在组,时间戳,,,)
可以先看下不用-p的效果,先切换到其他用户再执行cp

[root@iZ28g26851kZ www]# ls -ltotal 0-rw-rw-r-- 1 www www 0 May  4 15:16 txt1[root@iZ28g26851kZ www]# cp txt1 txt2[root@iZ28g26851kZ www]# ls -ltotal 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后的效果

[root@iZ28g26851kZ www]# cp -p txt1 txt3[root@iZ28g26851kZ www]# ls -ltotal 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


移动文件,
移动整个目录到其他目录下

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

mv也用做重命名文件

[root@iZ28g26851kZ y]# lltotal 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]# lltotal 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文件

[root@iZ28g26851kZ opt]# cat /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin、、、、、、、、、、、、、、、、、、、、、、、、、sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinnscd:x:28:28:NSCD Daemon:/:/sbin/nologinmysql:x:500:500::/home/mysql:/bin/bashwww:x:501:501::/home/www:/bin/bash[root@iZ28g26851kZ opt]# 

当只想显示用户名的时候我们可以这样

cut -d: -f1

解释:-d表示用什么将这一行文本分割开,这里采用分号“:”,
-f表示显示第几列,这里显示第一列

[root@iZ28g26851kZ opt]# cut -d: -f1 /etc/passwdrootbindaemonadmlp、、、、、、、、、、、、、、、、、、、、、、、、、、sshdtcpdumpnscdmysqlwww[root@iZ28g26851kZ opt]# 

同时显示第一列和第3列

[root@iZ28g26851kZ opt]# cut -d: -f1,3 /etc/passwdroot:0bin:1daemon:2、、、、、、、、、、、、、、、、、、、、、abrt:173sshd:74tcpdump:72nscd:28mysql:500www:501

显示一到三列

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

sort


-n:数值排序-r:降序-t:列分隔符-k:以哪个列为关键字-u:排序时相同的行只显示一次-f:排序时忽略字符大小写

再拿passwd文件为例,以第三列数字降序排列

sort -t: -k3 -r -n /etc/passwd

结果:

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

uniq


-c: 显示文件中行重复的次数
-d: 只显示重复的行

wc(word count)


文本统计

[root@iZ28g26851kZ opt]# wc test  82  78 610 test

解释:82行 78个单词 610字节

tr


字符处理命令
-d:删除出现在字符集中的所有字符
例:

[root@iZ28g26851kZ opt]# tr -d wasdaswwwwwwwwwwwasdasdasdasasdasd^C[root@iZ28g26851kZ opt]# 

字符替换

[root@iZ28g26851kZ opt]# tr a baaaaaaccccccccccddddddddbbbbbbccccccccccdddddddd^C[root@iZ28g26851kZ opt]# 

alias


给命令起别名
alias CMDALIAS=’COMMAND [options] [arguments]’
例:

alias list='ls -l'

效果:

[root@iZ28g26851kZ ~]# list-bash: list: command not found[root@iZ28g26851kZ ~]# alias list='ls -l'[root@iZ28g26851kZ ~]# listtotal 30300drwxr-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.gzdrwxrwxrwx 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.gzdrwxr-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默认不带任何参数则显示当前所有的别名

[root@iZ28g26851kZ ~]# aliasalias 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

[root@iZ28g26851kZ ~]# unalias list[root@iZ28g26851kZ ~]# list-bash: list: command not found[root@iZ28g26851kZ ~]# 
0 0
原创粉丝点击