0
点赞
收藏
分享

微信扫一扫

CentOS 文件管理命令(2)

复制命令

cp:copy
cp [option]... source dest
cp [选项] 源文件... 目标路径件
默认,只能拷贝文件

## 选项
-t:目标路径 源文件
-p:保持文件属性
-r:递归复制,复制目录及目录下的所有文件
-a:相当于-rp,既可以拷贝目录,又可以保持文件属性
-i:提示
-v:显示,拷贝的过程
## 用法
023:29:43 root@leidage,10.0.0.100:<sub> # cp oldboy.txt /tmp/oldboy

# 举例:cp -t 目标路径 源文件
023:42:58 root@leidage,10.0.0.100:</sub> # cp -t /tmp/oldboy oldboy.txt
# 举列:cp -p 保持文件属性
023:48:51 root@leidage,10.0.0.100:<sub> # ll
total 4
-rw-r--r--. 1 root root 0 Mar 23 23:47 lol.txt
023:48:53 root@leidage,10.0.0.100:</sub> # cp -p lol.txt /tmp/oldboy
023:49:32 root@leidage,10.0.0.100:<sub> # cd /tmp/oldboy
023:49:45 root@leidage,10.0.0.100:/tmp/oldboy # ll
total 0
-rw-r--r--. 1 root root 0 Mar 23 23:47 lol.txt
023:49:48 root@leidage,10.0.0.100:/tmp/oldboy #
# 举列:cp -r 递归复制,复制目录及目录下的所有文件
023:52:49 root@leidage,10.0.0.100:/abc # cp -r /abc /tmp
023:53:19 root@leidage,10.0.0.100:/abc # cd /tmp
023:53:32 root@leidage,10.0.0.100:/tmp # ls
abc oldboy red vmware-root_526-2957190390 vmware-root_528-2957714671 vmware-root_530-2966037869 vmware-root_533-4282367668 vmware-root_535-4290690870 vmware-root_551-4257134878
ks-script-yUnBp4 oldgirl vmware-root_525-4281712298 vmware-root_527-4290035496 vmware-root_529-4290559793 vmware-root_532-2965906794 vmware-root_534-2957583592 vmware-root_536-2991137505 yum.log
023:53:33 root@leidage,10.0.0.100:/tmp # cd abc
023:53:52 root@leidage,10.0.0.100:/tmp/abc # ls
qwe.txt
023:53:56 root@leidage,10.0.0.100:/tmp/abc #
# 举列:cp -a 相当于-rp,既可以拷贝目录,又可以保持文件属性
000:01:27 root@leidage,10.0.0.100:</sub> # ll /
total 20
drwxr-xr-x. 2 root root 21 Mar 23 23:52 abc
000:02:22 root@leidage,10.0.0.100:<sub> # cp -a /abc /tmp/red
000:02:57 root@leidage,10.0.0.100:</sub> # cd /tmp/red
000:03:22 root@leidage,10.0.0.100:/tmp/red # ll
total 0
drwxr-xr-x. 2 root root 21 Mar 23 23:52 abc
# 举列:cp -i =cp
000:12:23 root@leidage,10.0.0.100:<sub> # cp /abc/qwe.txt /tmp/red
cp: overwrite ‘/tmp/red/qwe.txt’?
提示是否要覆盖,输入y则覆盖,输入n则不覆盖
# 举列:cp -v 显示,拷贝的过程
000:17:12 root@leidage,10.0.0.100:</sub> # cp -v /abc/qwe.txt /tmp/red
‘/abc/qwe.txt’ -> ‘/tmp/red/qwe.txt’
000:17:40 root@leidage,10.0.0.100:<sub> #

## 复制并改名
cp /abc/oldboy.txt /tmp/ooo
ooo是否存在
- 存在
1)存在,并且ooo是个目录:将oldboy.txt放到/tmp/ooo目录下,不改名
2)存在,并且ooo是个普通文件:则提示是否要覆盖,输入y则覆盖,输入n则不覆盖
- 不存在:复制到/tmp下,并且改名为ooo
#举列:ooo存在,ooo是个目录
000:23:46 root@leidage,10.0.0.100:</sub> # cp /abc/qwe.txt /tmp/ooo
000:24:11 root@leidage,10.0.0.100:<sub> # ls /tmp/ooo
qwe.txt
#举列:ooo存在,是个普通文件
000:27:10 root@leidage,10.0.0.100:</sub> # cp /abc/qwe.txt /tmp/ooo.txt
cp: overwrite ‘/tmp/ooo.txt’? y
#举列:ooo不存在
000:30:19 root@leidage,10.0.0.100:<sub> # cp /abc/qwe.txt /tmp/ooo
000:30:46 root@leidage,10.0.0.100:</sub> # ls /tmp
ooo
000:30:56 root@leidage,10.0.0.100:<sub> #

## 报错
000:17:40 root@leidage,10.0.0.100:</sub> # cp /abc /tmp
cp: omitting directory ‘/abc’
abc为目录 cp只能复制普通文件

## 避免提示有两种方法
1.使用转义符:让转义符后面的内容,做它自己
[root@localhost <sub>]# \cp -a /etc /tmp/
# 举列:
00:35:53 root@leidage,10.0.0.100:</sub> # cp -a /abc /tmp
cp: overwrite ‘/tmp/abc/qwe.txt’? ^C
00:36:28 root@leidage,10.0.0.100:<sub> # \cp -a /abc /tmp
00:36:51 root@leidage,10.0.0.100:</sub> #
2.使用命令的绝对路径
[root@localhost <sub>]# /bin/cp -a /etc /tmp
#举列:
000:36:51 root@leidage,10.0.0.100:</sub> # which cp
alias cp='cp -i'
/usr/bin/cp
000:40:02 root@leidage,10.0.0.100:<sub> # /bin/cp -a /abc /tmp
000:40:59 root@leidage,10.0.0.100:</sub> #

注意:当拷贝文件有多个源文件的情况下,最后一个必须是目录,不能是普通文件,也不能不存在
#举列:cp a b c d e
最后一个e必须是目录

删除命令

rm:remove
默认,只能删除普通文件
## 语法
rm [OPTION]... FILE...
rm 选项... 文件...

## 选项
-i:提示
-r:递归删除
-f:force 强制
------------------
-d:只能删除空目录
rm: remove regular empty file ‘/tmp/ooo’? y
提示:是否删除?y是同意,n是不同意
# 举列:rm 默认,只能删除普通文件
000:40:59 root@leidage,10.0.0.100:<sub> # rm /tmp/ooo
rm: remove regular empty file ‘/tmp/ooo’? y
000:55:41 root@leidage,10.0.0.100:</sub> #
# 举列:rm -i =rm
000:40:59 root@leidage,10.0.0.100:<sub> # rm /tmp/ooo
rm: remove regular empty file ‘/tmp/ooo’?
提示:是否删除?y是同意,n是不同意
#举列:rm -r 递归删除, 是删除目录及目录下的文件
000:55:41 root@leidage,10.0.0.100:</sub> # rm -r /abc
rm: descend into directory ‘/abc’? y
rm: remove regular empty file ‘/abc/qwe.txt’? y
# 举列:rm -f 强制删除,强制删除普通文件
001:04:30 root@leidage,10.0.0.100:/tmp/red # rm -f ./*

## 报错
001:06:05 root@leidage,10.0.0.100:~ # rm abc
rm: cannot remove ‘abc’: Is a directory
abc是一个目录,rm不能直接删除

普通文件查看命令

cat 只能查看文件内容
## 语法
cat [OPTION]... [FILE]...
cat 选项... 普通文件...

## 选项
-n:number 打印行号,显示行号
-E:end ,查看文件每一行的结尾,以$符表示
-T:Tab,查看文件中每一行内容是否含有制表符。以^I表示一个制表符
-A:既显示制表符,又显示文件的结尾

# 举列:cat 只能查看文件内容
001:11:36 root@leidage,10.0.0.100:<sub> # echo 1 > a.txt
001:12:40 root@leidage,10.0.0.100:</sub> # echo 2 > b.txt
001:12:56 root@leidage,10.0.0.100:<sub> # echo 2 > c.txt
001:14:02 root@leidage,10.0.0.100:</sub> # cat a.txt b.txt c.txt
1
2
2
# 举列:cat -n 打印行号,显示行号
001:18:07 root@leidage,10.0.0.100:<sub> # 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:998:User for polkitd:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nolog
# 举列:cat -E 查看文件每一行的结尾,以$符表示
001:22:11 root@leidage,10.0.0.100:</sub>/abc # cat -E xx.txt
1$
2 $
3 $
4 $
5 $
6 $
# 举列: cat -T Tab,查看文件中每一行内容是否含有制表符。以^I表示一个制表符
001:24:47 root@leidage,10.0.0.100:<sub>/abc # cat -T xx.txt
^I1
^I^I2
^I3
^I^I^I4
# 举列: cat-A 既查看制表符,又查看文件结尾
001:25:00 root@leidage,10.0.0.100:</sub>/abc # cat -A xx.txt
^I1$
^I^I2$
^I3$
^I^I^I4 $

## 报错
001:31:56 root@leidage,10.0.0.100:~ # cat /tmp
cat: /tmp: Is a directory
/tmp 是一个目录,cat只能查看普通文件

难搞用法

##(在文件不存在的情况下,创建文件并写入内容)
# 1.直接vi一个不存在的文件
#举列:
001:35:19 root@leidage,10.0.0.100:<sub> # vi d.txt
001:36:03 root@leidage,10.0.0.100:</sub> # ll
total 20
-rw-r--r--. 1 root root 2 Mar 24 01:12 a.txt
-rw-r--r--. 1 root root 2 Mar 24 01:12 b.txt
-rw-r--r--. 1 root root 2 Mar 24 01:14 c.txt
-rw-r--r--. 1 root root 9 Mar 24 01:36 d.txt
001:36:21 root@leidage,10.0.0.100:<sub> # cat d.txt
1
2
3
4
# 2.echo内容重定向到一个文件中
#举列:
001:37:56 root@leidage,10.0.0.100:</sub> # echo e > e.txt
001:40:15 root@leidage,10.0.0.100:<sub> # ll
total 24
-rw-r--r--. 1 root root 2 Mar 24 01:12 a.txt
-rw-r--r--. 1 root root 2 Mar 24 01:12 b.txt
-rw-r--r--. 1 root root 2 Mar 24 01:14 c.txt
-rw-r--r--. 1 root root 9 Mar 24 01:36 d.txt
-rw-r--r--. 1 root root 2 Mar 24 01:40 e.txt
001:40:20 root@leidage,10.0.0.100:</sub> # cat e.txt
e
# 3.使用cat创建文件并写入内容
#举列:
001:40:32 root@leidage,10.0.0.100:<sub> # cat >> q.txt <<EOF
> QQ
> EOF
001:43:53 root@leidage,10.0.0.100:</sub> # ll
-rw-r--r--. 1 root root 2 Mar 24 01:12 a.txt
-rw-r--r--. 1 root root 2 Mar 24 01:12 b.txt
-rw-r--r--. 1 root root 2 Mar 24 01:14 c.txt
-rw-r--r--. 1 root root 9 Mar 24 01:36 d.txt
-rw-r--r--. 1 root root 2 Mar 24 01:40 e.txt
-rw-r--r--. 1 root root 3 Mar 24 01:43 q.txt
001:46:12 root@leidage,10.0.0.100:~ # cat q.txt
QQ

## 注意:cat查看文件时,会将文件所有内容全部加载到内存里
比如说:内存只有4G,加载内容有5G,内存就会爆了

普通文件查看命令 扩展

# more
more 文件名
按回车,一行一行向下显示
按空格,一页一页向下显示
Ctrl + b:向上查看
Ctrl + f:向下查看
#举列:
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
# http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name port/protocol [aliases ...] [# comment]

tcpmux 1/tcp # TCP port service multiplexer
tcpmux 1/udp # TCP port service multiplexer
rje 5/tcp # Remote Job Entry
rje 5/udp # Remote Job Entry
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null

# less
less 文件名
Ctrl + b:向上查看
Ctrl + f:向下查看
按回车,一行一行向下显示
按空格,一页一页向下显示
输入/关键字,高亮显示,所有匹配到的内容
按n,查找下一个关键字
Shift + n 查找上一个关键字
## head
head 文件名
默认显示一个文件的前10行内容
## 选项
-n:指定查看文件开头的前N行,n可以省略

#举列:head /etc/services
002:07:32 root@leidage,10.0.0.100:<sub> # head /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports

#举列: head -n
002:04:33 root@leidage,10.0.0.100:</sub> # head -6 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10

## tail
tail 文件名
默认显示一个文件的后10行内容
#举列:tail /etc/services
002:07:44 root@leidage,10.0.0.100:<sub> # tail /etc/services
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker

## 选项
-n:指定查看文件末尾的后N行,n可以省略
-f:follow,跟踪,动态跟踪一个文件的末尾

#举列:tail -n(n后加数字,n可以省略)
002:13:05 root@leidage,10.0.0.100:</sub> # tail -5 /etc/services
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
#举列:tail -f
002:20:24 root@leidage,10.0.0.100:~ # tail -f e.txt
举报

相关推荐

0 条评论