linux基础之常用命令(3)

cp


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

[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

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

3 directories, 3 files

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

[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目录下

[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

[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后的效果

[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


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

[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也用做重命名文件

[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文件

[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]# 

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

cut -d: -f1

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

[root@iZ28g26851kZ opt]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
、、、、、、、、、、、、、、、、、、、、、、、、、、
sshd
tcpdump
nscd
mysql
www
[root@iZ28g26851kZ opt]# 

同时显示第一列和第3列

[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

显示一到三列

[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


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

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

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

结果:

[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)


文本统计

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

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

tr


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

[root@iZ28g26851kZ opt]# tr -d w
asdaswwwwwwwwwwwasdasd
asdasasdasd
^C
[root@iZ28g26851kZ opt]# 

字符替换

[root@iZ28g26851kZ opt]# tr a b
aaaaaaccccccccccdddddddd
bbbbbbccccccccccdddddddd
^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 ~]# 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默认不带任何参数则显示当前所有的别名

[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

[root@iZ28g26851kZ ~]# unalias list
[root@iZ28g26851kZ ~]# list
-bash: list: command not found
[root@iZ28g26851kZ ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值