1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示?
Linux上的文件管理类命令有cp,rm,mv
cp命令:copy
常用选项:
-i:交互式复制,即覆盖之前提醒用户确认;
[root@Dexter_Centos7 tmp]# alias
alias cp='cp -i'
[root@Dexter_Centos7 tmp]# cp -i /etc/passwd /tmp/
cp: overwrite ‘/tmp/passwd’?
-f:强制覆盖目标文件;
-r, -R:递归复制目录;
[root@Dexter_Centos7 tmp]# cp /etc /tmp
cp: omitting directory ‘/etc’
[root@Dexter_Centos7 tmp]# cp -r /etc /tmp
[root@Dexter_Centos7 tmp]#
-d:复制符号链接文件本身,而非其指向的源文件;
[root@Dexter_Centos7 tmp]# ll /etc/system-release
lrwxrwxrwx. 1 root root 14 Aug 14 14:43 /etc/system-release -> centos-release
[root@Dexter_Centos7 tmp]# cp /etc/system-release /tmp
[root@Dexter_Centos7 tmp]# ll
total 4
-rw-r–r–. 1 root root 38 Aug 22 12:19 system-release
[root@Dexter_Centos7 tmp]# cp -d /etc/system-release /tmp
cp: overwrite ‘/tmp/system-release’? y
[root@Dexter_Centos7 tmp]# ll
total 0
lrwxrwxrwx. 1 root root 14 Aug 22 12:20 system-release -> centos-release
-a:-dR –preserve=all, archive,用于实现归档;
–preserv=
mode:权限
ownership:属主和属组
timestamps: 时间戳
context:安全标签
xattr:扩展属性
links:符号链接
all:上述所有属性
mv命令:move
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE..
常用选项:
-i:交互式;
-f:force
rm命令:remove
rm [OPTION]… FILE…
常用选项:
-i:interactive
[root@Dexter_Centos7 tmp]# rm -i dexter1
rm: remove regular empty file ‘dexter1’? y
-f:force
[root@Dexter_Centos7 tmp]# rm dexter1
rm: remove regular empty file ‘dexter1’? n
[root@Dexter_Centos7 tmp]# ll
total 0
-rw-r–r–. 1 root root 0 Aug 22 12:26 dexter1
[root@Dexter_Centos7 tmp]# rm -f dexter1
[root@Dexter_Centos7 tmp]# ll
total 0
-r: recursive
[root@Dexter_Centos7 tmp]# rm -f /tmp/*
rm: cannot remove ‘/tmp/abrt’: Is a directory
rm: cannot remove ‘/tmp/alsa’: Is a directory
rm: cannot remove ‘/tmp/alternatives’: Is a directory
rm: cannot remove ‘/tmp/at-spi2’: Is a directory
[root@Dexter_Centos7 tmp]# rm -rf /tmp/*
[root@Dexter_Centos7 tmp]# ll
total 0
2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示?
命令执行的状态结果:
bash通过状态返回值来输出此结果:
成功:0
失败:1-255
命令执行完成之后,其状态返回值保存于bash的特殊变量$?中;
[root@Dexter_Centos7 tmp]# ll /tmp
total 0
[root@Dexter_Centos7 tmp]# echo $?
0
[root@Dexter_Centos7 tmp]# lless /tmp
bash: lless: command not found…
Similar command is: 'less'
[root@Dexter_Centos7 tmp]# echo $?
127
命令行展开
~:自动展开为用户的家目录,或指定的用户的家目录;
{}:可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径;
例如:/tmp/{a,b} 相当于 /tmp/a /tmp/b
[root@Dexter_Centos7 tmp]# mkdir -pv /tmp/{a,b}
mkdir: created directory ‘/tmp/a’
mkdir: created directory ‘/tmp/b’
[root@Dexter_Centos7 tmp]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 22 12:49 a
drwxr-xr-x. 2 root root 6 Aug 22 12:49 b
3、请使用命令行展开功能来完成以下练习?
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
mkdir -pv /tmp/{a,b}_{c,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@Dexter_Centos7 tmp]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,etc/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/etc’
mkdir: created directory ‘/tmp/mylinux/etc/etc/sysconfig’
mkdir: created directory ‘/tmp/mylinux/etc/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@Dexter_Centos7 tmp]# tree /tmp/mylinux
/tmp/mylinux
|– bin
|– boot
| `– grub
|– dev
|– etc
| |– etc
| | `– sysconfig
| | `– network-scripts
| `– rc.d
| `– init.d
|– lib
| `– modules
|– lib64
|– proc
|– sbin
|– sys
|– tmp
|– usr
| `– local
| |– bin
| `– sbin
`– var
|– lock
|– log
`– run
4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息?
元数据:包括文件的权限,属主、属组信息,inode信息,数据大小,时间信息(atime,mtime,ctime)等
[root@Dexter_Centos7 tmp]# ll -i
total 0
35123168 drwxr-xr-x. 2 root root 6 Aug 22 12:49 a
69412032 drwxr-xr-x. 2 root root 6 Aug 22 12:49 b
时间
modification time (mtime):
当该档案的内容数据变更时,就会更新这个时间!内容数据指的是档案的内容,而不是档案的属性或权限
status time (ctime):
当该档案的状态(status)改变时,就会更新这个时间,举例来说,像是权限与属性被更改了,都会更新这个时间。
access time (atime):
当该档案的内容被取用时,就会更新这个读取时间 (access)。举例来说,我们使用 cat 去读取 /etc/man.config 就会更新该档案的atime 了。
[root@Dexter_Centos7 tmp]# stat /tmp
File: ‘/tmp’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 100663425 Links: 10
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:tmp_t:s0
Access: 2016-08-22 12:49:53.892349320 +0800
Modify: 2016-08-22 12:49:22.714025306 +0800
Change: 2016-08-22 12:49:22.714025306 +0800
Birth: –
修改文件的时间戳信息用touch命令
-a :仅修订 access time;
-c :仅修改档案的时间,若该档案不存在则不建立新档案;
-d :后面可以接欲修订得日期而不用目前的日期,也可以使用 –date="日期或时间"
-m :仅修改 mtime ;
-t :后面可以接欲修订的时间而不用目前的时间,格式为[YYMMDDhhmm]
5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
alias NAME="VALUE"
注意:此种设置方式至对当前Shell有效!如果需要全局有效,可以在/etc/bashrc(全局)、~/.bashrc(用户个人)文件创建别名命令。
引用命令的执行结果
$(COMMAND)或`COMMAND`
6、显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录?
[root@Dexter_Centos7 ~]# ls -d /var/l?[[:lower:]]
/var/lib /var/log
7、显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录?
[root@Dexter_Centos7 ~]# ls -d /etc/[0-9]*[^0-9]
/etc/5bcc
8、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录?
[root@Dexter_Centos7 ~]# ls -d /etc/[^a-z][a-z]*
/etc/5bcc
9、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22?
[root@Dexter_Centos7 tmp]# touch /tmp/tfile-$(date +%Y-%m-%d-%H-%M-%S)
[root@Dexter_Centos7 tmp]# ll
total 0
-rw-r–r–. 1 root root 0 Aug 22 17:24 tfile-2016-08-22-17-24-31
10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中?
cp -r /etc/p*[^0-9] /tmp/mytest1
11、复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中?
cp -r /etc/*.d /tmp/mytest2
12、复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中?
cp -r /etc/[lmn]*.conf /tmp/mytest3
原创文章,作者:N22-昆山-Dexter_Wang,如若转载,请注明出处:http://www.178linux.com/38949