一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关演示。
A、copy命令
a、 单元复制:cp [OPTION]… [-T] SOURCE DEST
1、 如果dest不存在:则事先创建此文件,并复制源文件的数据流至dest中;
[root@localhost /]# ls -l /tmp
total 0
[root@localhost /]# cp /etc/fstab /tmp/fstab
[root@localhost /]# diff /etc/fstab /tmp/fstab
[root@localhost /]#
2、 如果dest存在:
#如果dest是非目录文件,则覆盖目标文件:
[root@localhost /]# touch /tmp/a b c d e f
[root@localhost /]# cp /etc/fstab /tmp/a
cp: overwrite ‘/tmp/a’?
[root@localhost /]#
#如果dest是目录文件:则先在dest目录下创建一个与源文件同名的文件,并复制其数据流:
[root@localhost /]# cp /etc/hostname /tmp
[root@localhost /]# ls -l /tmp
total 8
-rw-r–r–. 1 root root 0 Sep 6 23:42 a
-rw-r–r–. 1 root root 501 Sep 6 23:41 fstab
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
b、多源复制:cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE..
#如果dest不存在:错误:
[qiu@localhost ~]$ cp /etc/{fstab,yum.conf} /tmp/rrr.txt
cp: target ‘/tmp/rrr.txt’ is not a directory
#如果dest是非目录文件:错误
[root@localhost ~]# ls -l /tmp
total 16
-rw-r–r–. 1 root root 0 Sep 6 23:52 a
-rw-rw-rw-. 1 root root 501 Sep 7 00:00 b
-rw-r–r–. 1 root root 0 Sep 6 23:52 c
-rw-r–r–. 1 root root 0 Sep 6 23:52 d
-rw-r–r–. 1 root root 501 Sep 6 23:41 fstab
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
drwxr-xr-x. 2 root root 33 Sep 7 01:09 test
-rw-r–r–. 1 root root 970 Sep 7 01:02 yum.conf
[root@localhost ~]# cp /etc/{yum.conf,fstab} /tmp/a
cp: target ‘/tmp/a’ is not a directory
#如果dest是目录文件:分别复制每个文件至目录中,并保持原名;
[root@localhost ~]# mkdir /tmp/test2
[root@localhost ~]# cp /etc/{yum.conf,fstab} /tmp/test2
[root@localhost ~]# ls -l /tmp/test2
total 8
-rw-r–r–. 1 root root 501 Sep 7 01:17 fstab
-rw-r–r–. 1 root root 970 Sep 7 01:17 yum.conf
常用选项:
-i:交互式复制,即覆盖之前提醒用户确认:-f:强制覆盖目标文件:
[qiu@localhost ~]$ cp -i /etc/fstab /tmp/b
cp: overwrite ‘/tmp/b’? y
[qiu@localhost ~]$
-r:递归复制目录:
[root@localhost /]# cp -r /var/log /tmp
[root@localhost /]# ls -l /tmp
total 20
-rw-r–r–. 1 root root 0 Sep 6 23:52 a
-rw-rw-rw-. 1 root root 501 Sep 7 00:00 b
-rw-r–r–. 1 root root 0 Sep 6 23:52 c
-rw-r–r–. 1 root root 0 Sep 6 23:52 d
-rw-r–r–. 1 root root 501 Sep 6 23:41 fstab
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
drwxr-xr-x. 19 root root 4096 Sep 7 01:27 log
[root@localhost /]# cp -r /var/{adm,log} /tmp/test2
[root@localhost /]# ls -l /tmp/test2
total 12
drwxr-xr-x. 2 root root 6 Sep 7 01:30 adm
-rw-r–r–. 1 root root 501 Sep 7 01:17 fstab
drwxr-xr-x. 19 root root 4096 Sep 7 01:30 log
-rw-r–r–. 1 root root 970 Sep 7 01:17 yum.conf
-d:复制符号链接文件本身,而非其指向的源文件;
-a:-dr –preserve=all ,archive,用于实现归档;
–preserve=
mode:权限
ownership:属主和属组
timestamps:时间戳
context:安全标签
xattr:扩展属性
links:符号链接
all:上述所有属性
B、mv命令
a、单元移动:mv [OPTION]… [-T] SOURCE DEST
1、dest路径一定要存在,否者无法移动
root@localhost /]# mv /tmp/a /tmp/g
mv: cannot stat ‘/tmp/a’: No such file or directory
2、dest如果是文件,则覆盖文件内容
[root@localhost /]# mv /tmp/a /tmp/b
mv: overwrite ‘/tmp/b’? y
[ root@localhost /]# ls -l /tmp
total 16
-rw-r–r–. 1 root root 501 Sep 6 23:41 b
3、如果源文件是目录,则目标也必须是目录,否则会报错。
[root@localhost /]# mv /var/log /tmp/c
mv: overwrite ‘/tmp/c’? y
mv: cannot overwrite non-directory ‘/tmp/c’ with directory ‘/var/log’
[root@localhost /]# mv /var/log /tmp/test3
[root@localhost /]# ls -l /tmp/test3 | wc -l
2
[root@localhost /]# ls -l /tmp/test3
total 4
drwxr-xr-x. 19 root root 4096 Sep 6 22:53 log
常用选项:
-i:交互式
[root@localhost /]# mv -i /tmp/b /tmp/d
mv: overwrite ‘/tmp/d’? n
[root@localhost /]#
-f:force
[root@localhost /]# mv -f /tmp/b /tmp/d
[root@localhost /]# ls -l /tmp
total 12
-rw-r–r–. 1 root root 0 Sep 6 23:52 c
-rw-r–r–. 1 root root 501 Sep 6 23:41 d
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
drwxr-xr-x. 2 root root 33 Sep 7 01:09 test
drwxr-xr-x. 4 root root 53 Sep 7 01:30 test2
C、rm命令
rm [OPTION]… FILE…
常用选项:
-i:交互式
[root@localhost /]# rm -i /tmp/c
rm: remove regular empty file ‘/tmp/c’? y
[root@localhost /]# ls -l /tmp
total 12
-rw-r–r–. 1 root root 501 Sep 6 23:41 d
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
-f:force
[root@localhost /]# rm -f /tmp/d
You have new mail in /var/spool/mail/root
[root@localhost /]# ls -l /tmp
total 8
-rw-r–r–. 1 root root 22 Sep 6 23:49 hostname
drwxr-xr-x. 2 root root 33 Sep 7 01:09 test
drwxr-xr-x. 4 root root 53 Sep 7 01:30 test2
drwxr-xr-x. 3 root root 16 Sep 7 01:57 test3
-rw-r–r–. 1 root root 970 Sep 7 01:02 yum.conf
-r :递归删除
[root@localhost /]# rm -ir /tmp/test
rm: descend into directory ‘/tmp/test’? y
rm: remove regular file ‘/tmp/test/yum.conf’? y
rm: remove regular file ‘/tmp/test/fstab’? y
rm: remove directory ‘/tmp/test’? y
危险操作:rm -rf /*
注意:所有不用的文件建议不要直接删除。
二、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示
bash的基础特性:
命令的执行状态结果:
bash通过状态返回值来输出此结果:
成功:0
失败:1-255
命令执行完成之后,其状态返回值保存于bash的特殊变量$?中:
命令正常执行时,有的还回有命令返回值;
根据命令及其功能不同,结果各不相同;
引用命令的执行结果:
$(COMMAND) 或‘(COMMAND)’
[root@localhost /]# rm -rf /tmp/test2
[root@localhost /]# echo $?
0
三、请使用命令行展开功能来完成以下练习:
1、创建/tmp目录下的:a_c, a_d,b_c,b_d
root@localhost /]# mkdir /tmp/{a_c,a_d,b_c,b_d}
[root@localhost /]# ls -l /tmp
total 0
drwxr-xr-x. 2 root root 6 Sep 7 03:05 a_c
drwxr-xr-x. 2 root root 6 Sep 7 03:05 a_d
drwxr-xr-x. 2 root root 6 Sep 7 03:05 b_c
drwxr-xr-x. 2 root root 6 Sep 7 03:05 b_d
2、创建/tmp/mylinux目录下的:
/tmp/mylinux
├── bin
├── boot
└── grub
├── dev
├── etc
├── rc.d
└── init.d
└── sysconfig
└── network-scripts
├── lib
└── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
└── local
├── bin
└── sbin
└── var
├── lock
├── log
└── run
[root@localhost /]# mkdir -pv
/tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
mkdir: created directory ‘/tmp/mylinux’
mkdir: created directory ‘/tmp/mylinux/bin’
mkdir: created directory ‘/tmp/mylinux/boot’
mkdir: created directory ‘/tmp/mylinux/boot/grub’
mkdir: created directory ‘/tmp/mylinux/dev’
mkdir: created directory ‘/tmp/mylinux/etc’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’
mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’
mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’
mkdir: created directory ‘/tmp/mylinux/lib’
mkdir: created directory ‘/tmp/mylinux/lib/modules’
mkdir: created directory ‘/tmp/mylinux/lib64’
mkdir: created directory ‘/tmp/mylinux/proc’
mkdir: created directory ‘/tmp/mylinux/sbin’
mkdir: created directory ‘/tmp/mylinux/sys’
mkdir: created directory ‘/tmp/mylinux/tmp’
mkdir: created directory ‘/tmp/mylinux/usr’
mkdir: created directory ‘/tmp/mylinux/usr/local’
mkdir: created directory ‘/tmp/mylinux/usr/local/bin’
mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’
mkdir: created directory ‘/tmp/mylinux/var’
mkdir: created directory ‘/tmp/mylinux/var/lock’
mkdir: created directory ‘/tmp/mylinux/var/log’
mkdir: created directory ‘/tmp/mylinux/var/run’
[root@localhost /]# tree /tmp/mylinux
/tmp/mylinux
├── bin
├── boot
└── grub
├── dev
├── etc
├── rc.d
└── init.d
└── sysconfig
└── network-scripts
├── lib
└── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
└── local
├── bin
└── sbin
└── var
├── lock
├── log
└── run
四、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳。
[root@localhost /]# stat /tmp/a_c
File: ‘/tmp/a_c’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 803h/2051d Inode: 808456133 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-09-07 03:05:02.800231682 -0700
Modify: 2017-09-07 03:05:02.800231682 -0700
Change: 2017-09-07 03:05:02.800231682 -0700
Birth: –
size:文件大小 Blocks:文件块 IO Block:IO 块数
Device:设备号 links:硬件连接数
Access:属主属组其他权限 Uid:用户id号 Gid:组id号
Access time:上次访问时间
Modify time : 上次更改时间
Change time : 上次改动时间
touch命令:
touch – change file timestamps
touch [OPTION]… FILE…
-c:指定的文件路径不存在时不予创建;
-a:仅修改access time
-m:仅修改modify time
-t:stamps
[root@localhost /]# touch -a -t 201611121200 /tmp/a_c/file
You have new mail in /var/spool/mail/root
[root@localhost /]# stat /tmp/a_c/file
File: ‘/tmp/a_c/file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 808456184 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-11-12 12:00:00.000000000 -0800
Modify: 2017-09-07 04:48:06.854340802 -0700
Change: 2017-09-07 04:50:19.768254065 -0700
Birth: –
[root@localhost /]# touch -m -t 201611121200 /tmp/a_c/file
You have new mail in /var/spool/mail/root
[root@localhost /]# stat /tmp/a_c/file
File: ‘/tmp/a_c/file’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 808456184 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2016-11-12 12:00:00.000000000 -0800
Modify: 2016-11-12 12:00:00.000000000 -0800
Change: 2017-09-07 05:04:26.164027171 -0700
原创文章,作者:N27_Sunny day,如若转载,请注明出处:http://www.178linux.com/86386
评论列表(1条)
再接再励。