本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/99075
文件管理 展开式
#1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
###cp
+ 文件复制
+ 1.单源复制
+ 1.DEST不存在则创建此文件,并复制源文件内容到DEST中
+ 2.DEST存在,若DEST是文件,则会覆盖目标文件
+ 3.若DEST是目录,则会事先在目录下创建一个同名的文件,并复制数据流。
+ 2.多源复制(目标必须是目录,且必须存在)
+ 分别复制每个文件至目标目录中,并保持原名
+ 选项
+ 1.-i interactive 交互,覆盖前提示
+ 2.-f force 强制覆盖
+ 3.-r(-R)recursive 递归复制目录
+ 1.DEST 存在,复制文件层级结构到DEST下
+ 2.DEST 不存在,创建目录并复制
+ 3.cp -r目录时软链接会保存,cp软链接会丢失软链接,加-d不丢失
+ 4.-d 不跟踪软链接的源文件,只复制软链接本身
+ 5.–preserve=(mode,ownership,timestamps,context,xattr,links,all) 保留文件属性
+ 6.-a=-dr –preserve=all 归档复制,递归复制目录,复制软链接本身,保留文件所有属性
###mv
+ move 移动或重命名文件或目录
+ 若文件或目录在同一目录下则重命名,否则为移动
+ -i 交互式
+ -f 强制
###rm
+ remove 删除
+ rm -i 交互式
+ rm -f 强制
+ rm -fr 强制删除目录
+ 删除时建议mv到专用目录中(如/root/tmp)当做回收站
“`
[root@localhost tmp]# ls
yum.log
[root@localhost tmp]# mkdir a
[root@localhost tmp]# ls
a yum.log
[root@localhost tmp]# mkdir b
[root@localhost tmp]# ls
a b yum.log
[root@localhost tmp]# cp a b
cp: omitting directory `a’
[root@localhost tmp]# cp yum.log b
[root@localhost tmp]# cp -a a b
[root@localhost tmp]# tree /tmp
/tmp
├── a
├── b
│ ├── a
│ └── yum.log
└── yum.log
3 directories, 2 files
[root@localhost tmp]# cp -a a c
[root@localhost tmp]# tree /tmp
/tmp
├── a
├── b
│ ├── a
│ └── yum.log
├── c
└── yum.log
4 directories, 2 files
[root@localhost tmp]# touch a.txt
[root@localhost tmp]# mv a.txt b
[root@localhost tmp]# tree /tmp
/tmp
├── a
├── b
│ ├── a
│ ├── a.txt
│ └── yum.log
├── c
└── yum.log
4 directories, 3 files
[root@localhost tmp]# mv b/a.txt b.txt
[root@localhost tmp]# tree /tmp
/tmp
├── a
├── b
│ ├── a
│ └── yum.log
├── b.txt
├── c
└── yum.log
4 directories, 3 files
[root@localhost tmp]# mv b a
[root@localhost tmp]# tree /tmp
/tmp
├── a
│ └── b
│ ├── a
│ └── yum.log
├── b.txt
├── c
└── yum.log
4 directories, 3 files
[root@localhost tmp]# mv a c
[root@localhost tmp]# tree /tmp
/tmp
├── b.txt
├── c
│ └── a
│ └── b
│ ├── a
│ └── yum.log
└── yum.log
4 directories, 3 files
[root@localhost tmp]# rm -fr b.txt c
[root@localhost tmp]# tree /tmp
/tmp
└── yum.log
0 directories, 1 file
“`
#2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。
###bash shell基础特性之命令的执行状态结果
+ bash通过状态返回值输出此结果:成功为0,失败为1-255
+ 命令执行完成后,状态返回值保存于特殊变量$?中,仅保存最近一条命令的
+ 命令如果正常执行,会有命令执行结果返回值
+ 使用$()或“可以引用命令的执行结果
“`
[root@localhost tmp]# mkdir $(date +%F-%H-%M-%S)
[root@localhost tmp]# echo $?
0
[root@localhost tmp]# ls
2018-05-18-09-12-41 2018-05-18-09-12-45 ks-script-6ZKL2e yum.log
[root@localhost tmp]# mkdir `date +%F-%H-%M-%S`
[root@localhost tmp]# ls
2018-05-18-09-12-41 2018-05-18-09-13-03 yum.log
2018-05-18-09-12-45 ks-script-6ZKL2e
“`
###bash shell基础特性之命令行展开功能
+ ~自动展开为用户的家目录
+ {}分组以,分隔的路径列表,将其展开为多个路径
“`
[root@localhost tmp]# mkdir -pv /tmp/x/{y1/{a,b},y2}
mkdir: created directory ‘/tmp/x’
mkdir: created directory ‘/tmp/x/y1’
mkdir: created directory ‘/tmp/x/y1/a’
mkdir: created directory ‘/tmp/x/y1/b’
mkdir: created directory ‘/tmp/x/y2’
[root@localhost tmp]# tree /tmp/x
/tmp/x
├── y1
│ ├── a
│ └── b
└── y2
“`
#3、请使用命令行展开功能来完成以下练习:
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
“`
[root@localhost tmp]# mkdir -v {a,b}_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
mkdir: created directory ‘b_d’
“`
(2)、创建/tmp/mylinux目录下的:
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/bin’
mkdir: created directory ‘/tmp/mylinux/usr/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
│ ├── bin
│ ├── local
│ └── sbin
└── var
├── lock
├── log
└── run
24 directories, 0 files
“`
赞 (1)
linux就是这个范儿!浅谈linux的哲学思想与基础命令
上一篇
2018-05-21
系统与内核管理
下一篇
2018-05-21
评论列表(1条)
不错。不过建议命令演示 写在命令编辑器里面。