本周作业内容:
1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
文件管理类常用的命令有:cp、mv、rm、file、ls、touch、mkdir
cp 拷贝文件、目录
常用选项 -a 同-dR --preserve=all 保留原文件默认属性(权限,属主/属组,时间戳),并且尽可能的保留上下文、链接、扩展属性等 -i 当目标文件与原文件同名时,提示是否覆盖 -r,-R 递归处理目录 拷贝普通文件 [root@captain ~]# cp /etc/passwd /tmp/ [root@captain ~]# ll /tmp/passwd -rw-r--r--. 1 root root 1713 Aug 9 11:43 /tmp/passwd [root@captain ~]# cp -i /etc/passwd /tmp cp: overwrite `/tmp/passwd'? y 拷贝文件同时修改文件名 [root@captain ~]# cp /etc/passwd /tmp/passwd_bak [root@captain ~]# ll /tmp/passwd_bak -rw-r--r--. 1 root root 1713 Aug 9 12:42 /tmp/passwd_bak 拷贝目录 [root@captain ~]# cp -r /etc /tmp/ [root@captain ~]# ll -d /tmp/etc drwxr-xr-x. 113 root root 12288 Aug 9 11:45 /tmp/etc 通过大括号扩展,可以更快捷的复制/移动并重命名文件 [root@captain network-scripts]# ll ifcfg-eth* -rw-r--r--. 1 root root 224 Jun 6 22:12 ifcfg-eth0 [root@captain network-scripts]# cp ifcfg-eth{0,1} [root@captain network-scripts]# ll ifcfg-eth* -rw-r--r--. 1 root root 224 Jun 6 22:12 ifcfg-eth0 -rw-r--r--. 1 root root 224 Aug 9 12:44 ifcfg-eth1
mv 移动或重命名文件,用法基本同cp命令
常用选项 -i 当目标文件与原文件同名时,提示是否覆盖 [root@captain tmp]# ll passwd* -rw-r--r--. 1 root root 1713 Aug 9 11:45 passwd [root@captain tmp]# mv passwd{,_bak} [root@captain tmp]# ll passwd* -rw-r--r--. 1 root root 1713 Aug 9 11:45 passwd_bak
rm 删除文件或者目录
常用选项 -f 不提示,强制删除(包括不存在的文件或目录) -i 删除前有确认信息 -I 删除多余在三个文件或目录时,只提示一次 -r, -R 递归删除,删除目录。 删除特殊文件 [root@captain ~]# ll -- -abc -rw-r--r--. 1 root root 0 Aug 9 13:36 -abc [root@captain ~]# rm -abc rm: invalid option -- 'a' Try `rm ./-abc' to remove the file `-abc'. Try `rm --help' for more information. [root@captain ~]# rm -- -abc rm: remove regular empty file `-abc'? y 或者rm ./-abc
file 查看文件类型
[root@captain ~]# file /var/log/messages /var/log/messages: ASCII English text [root@captain ~]# file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped [root@captain ~]# file /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit: Bourne-Again shell script text executable [root@captain ~]# file /dev/sda1 /dev/sda1: block special
ls 列出当前目录下的文件
常用选项 -a 列出所有文件,包括隐藏文件或目录 -d 列出目录名 -F 在文件名后添加*/=>@|符号,标识文件类型 -h 以更适合人阅读的方式显示文件大小 -i 显示inode -r 逆向排序 -t 根据修改时间排序 -l 以长格式的形式列出当前目录文件 -Z 显示SELinux安全上下文 [root@captain ~]# ls -l /etc/passwd -rw-r--r--. 1 root root 1713 Jul 8 17:04 /etc/passwd
- rw- r-- r--. | 1 | root | root | 1713 | Jul 8 17:04 | /etc/passwd |
文件类型及权限 -:普通文件 b:块文件 c:字符设备 l:链接文件 p:管道文件 s:socket 权限: r:可读 w:可写 x:可执行 rw-:属主权限 r--:属组权限 r--:其他用户权限 | 文件硬链接数 | 属主 | 属组 | 文件大小(字节数) | 最近修改时间 | 文件名 |
touch 创建文件或者个性文件时间戳
常用选项 -a 仅修改文件访问时间 -m 仅修改文件修改时间 -r, --reference=FILE 参考其它文件设置当前文件时间戳 -t STAMP 使用[[CC]YY]MMDDhhmm[.ss]格式修改文件时间戳
mkdir 创建目录
常用选项 -m 创建目录的时候指定权限 -p 创建目录时,如果上级目录不存在,则同时创建上级目录 -v 显示详细信息 [root@captain mylinux]# mkdir foodir1 [root@captain mylinux]# mkdir -m 700 foodir2 [root@captain mylinux]# ll -d foodir* drwxr-xr-x. 2 root root 4096 Aug 9 14:10 foodir1 drwx------. 2 root root 4096 Aug 9 14:10 foodir2 [root@captain mylinux]# mkdir foodir/dir1 mkdir: cannot create directory `foodir/dir1': No such file or directory [root@captain mylinux]# mkdir -pv foodir/dir1 mkdir: created directory `foodir' mkdir: created directory `foodir/dir1' [root@captain mylinux]# tree foodir foodir └── dir1 1 directory, 0 files
2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。
bash命令行下,命令执行完成后都会返回一个值,成功完成返回0,如果出错,返回非0值。使用echo $?查看上一条指令的返回值。
正常完成:
[root@captain etc]# ls -l total 1888 drwxr-xr-x. 3 root root 4096 Feb 28 19:15 abrt drwxr-xr-x. 4 root root 4096 Feb 28 19:17 acpi -rw-r--r--. 1 root root 46 Aug 7 22:58 adjtime -rw-r--r--. 1 root root 1512 Jan 12 2010 aliases -rw-r--r--. 1 root root 12288 Jul 12 09:04 aliases.db drwxr-xr-x. 2 root root 4096 Feb 28 19:17 alsa drwxr-xr-x. 2 root root 4096 Jul 9 05:05 alternatives -rw-------. 1 root root 541 Nov 10 2015 anacrontab -rw-r--r--. 1 root root 148 May 15 2009 asound.conf -rw-r--r--. 1 root root 1 Jan 30 2012 at.deny drwxr-x---. 3 root root 4096 Feb 28 19:17 audisp drwxr-x---. 3 root root 4096 Feb 28 19:17 audit #...omitted... [root@captain etc]# echo $? 0
命令出错:命令出错又分为命令参数出错,或者命令本身执行问题
参数出错:
[root@captain etc]# ls -3 ls: invalid option -- '3' Try `ls --help' for more information. [root@captain etc]# echo $? 2
命令执行出错
[root@captain etc]# LS -l -bash: LS: command not found [root@captain etc]# echo $? 127
3、请使用命令行展开功能来完成以下练习:
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
[root@captain ~]# mkdir -pv /tmp/{a,b}_{c,d} mkdir: created directory `/tmp/a_c' mkdir: created directory `/tmp/a_d' mkdir: created directory `/tmp/b_c' mkdir: created directory `/tmp/b_d' [root@captain ~]# ls -dl /tmp/{a,b}_{c,d} drwxr-xr-x. 2 root root 4096 Aug 9 14:24 /tmp/a_c drwxr-xr-x. 2 root root 4096 Aug 9 14:24 /tmp/a_d drwxr-xr-x. 2 root root 4096 Aug 9 14:24 /tmp/b_c drwxr-xr-x. 2 root root 4096 Aug 9 14:24 /tmp/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@captain ~]# 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/lock' mkdir: created directory `/tmp/mylinux/log' mkdir: created directory `/tmp/mylinux/run' [root@captain ~]# tree /tmp/mylinux/ /tmp/mylinux/ ├── bin ├── boot │ └── grub ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── lock ├── log ├── proc ├── run ├── sbin ├── sys ├── tmp ├── usr │ └── local │ ├── bin │ └── sbin └── var 24 directories, 0 files
4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
元数据有:权限,属主/组,时间戳等
权限是文件读写、执行权限,说明该文件是否可以被读写、执行。
属主是文件的所有者,属组是文件所在组
时间戳是文件的最近访问、改变、修改时间
查看文件权限,属主/组等信息
[root@captain ~]# ls -l /etc/passwd -rw-r--r--. 1 root root 1713 Jul 8 17:04 /etc/passwd
查看文件时间戳
[root@captain ~]# stat /etc/passwd File: `/etc/passwd' Size: 1713 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 663741 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-08-09 09:34:54.079892021 +0800 Modify: 2016-07-08 17:04:34.980179221 +0800 Change: 2016-07-08 17:04:34.981179234 +0800
修改文件时间戳
#touch -t 修改访问时间和修改时间 [root@captain ~]# touch -t '201101010000' /tmp/passwd_bak [root@captain ~]# stat /tmp/passwd_bak File: `/tmp/passwd_bak' Size: 1713 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 413617 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2011-01-01 00:00:00.000000000 +0800 #访问时间 Modify: 2011-01-01 00:00:00.000000000 +0800 #修改时间 Change: 2016-08-09 14:45:41.115412106 +0800 #更改时间,修改属主,权限等会更改此时间 #touch -at 修改访问时间 [root@captain ~]# touch -at '201301010000' /tmp/passwd_bak [root@captain ~]# stat /tmp/passwd_bak File: `/tmp/passwd_bak' Size: 1713 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 413617 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2013-01-01 00:00:00.000000000 +0800 #访问时间 Modify: 2011-01-01 00:00:00.000000000 +0800 Change: 2016-08-09 14:47:09.765500508 +0800 #touch -mt 修改时间 [root@captain ~]# touch -mt '201501010000' /tmp/passwd_bak [root@captain ~]# stat /tmp/passwd_bak File: `/tmp/passwd_bak' Size: 1713 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 413617 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2013-01-01 00:00:00.000000000 +0800 Modify: 2015-01-01 00:00:00.000000000 +0800 #修改时间 Change: 2016-08-09 14:48:52.145762082 +0800
5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
定义命令别名
alias cmd='commd opt'
在命令中引用另一个命令执行结果的方法:
通过管道
[root@captain ~]# cat /etc/passwd | wc -l 36
通过命令替换
[root@captain ~]# ll `which man` -rwxr-xr-x. 1 root root 56120 Feb 22 2013 /usr/bin/man
通过find
[root@captain ~]# find /etc -type f -name "passwd" -exec ls -l {} \; -rw-r--r--. 1 root root 1713 Jul 8 17:04 /etc/passwd -rw-r--r--. 1 root root 146 Feb 22 2012 /etc/pam.d/passwd
6、显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。
[root@captain ~]# ls -dl /var/l*[[:digit:]]*[[:lower:]] -rw-r--r--. 1 root root 0 Aug 9 15:40 /var/l345ab
7、显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录。
[root@captain ~]# touch /etc/99abc [root@captain ~]# ll -dl /etc/[[:digit:]]*[^[:digit:]] -rw-r--r--. 1 root root 0 Aug 9 15:41 /etc/99abc
8、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录。
[root@captain ~]# touch /etc/9abc [root@captain ~]# ll -dl /etc/[^[:alpha:]][[:alpha:]]* -rw-r--r--. 1 root root 0 Aug 9 15:41 /etc/9abc
9、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-08-06-09-32-22。
[root@captain ~]# touch /tmp/tfile-`date +"%Y-%m-%d-%H-%M-%S"` [root@captain ~]# ll /tmp/tfile-2016-08-09-15-44-41 -rw-r--r--. 1 root root 0 Aug 9 15:44 /tmp/tfile-2016-08-09-15-44-41
10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
[root@captain ~]# mkdir /tmp/mytest1 [root@captain ~]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/ [root@captain ~]# ls -dl /tmp/mytest1/p*[^[:digit:]] drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/pam.d drwxr-xr-x. 3 root root 4096 Aug 9 15:45 /tmp/mytest1/pango -rw-r--r--. 1 root root 1713 Aug 9 15:45 /tmp/mytest1/passwd -rw-r--r--. 1 root root 1660 Aug 9 15:45 /tmp/mytest1/passwd- drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/pcmcia -rw-r--r--. 1 root root 2872 Aug 9 15:45 /tmp/mytest1/pinforc drwxr-xr-x. 9 root root 4096 Aug 9 15:45 /tmp/mytest1/pki drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/plymouth drwxr-xr-x. 5 root root 4096 Aug 9 15:45 /tmp/mytest1/pm -rw-r--r--. 1 root root 370 Aug 9 15:45 /tmp/mytest1/pm-utils-hd-apm-restore.conf drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/popt.d drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/portreserve drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/postfix drwxr-xr-x. 3 root root 4096 Aug 9 15:45 /tmp/mytest1/ppp -rw-r--r--. 1 root root 199640 Aug 9 15:45 /tmp/mytest1/prelink.cache -rw-r--r--. 1 root root 789 Aug 9 15:45 /tmp/mytest1/prelink.conf drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/prelink.conf.d -rw-r--r--. 1 root root 233 Aug 9 15:45 /tmp/mytest1/printcap -rw-r--r--. 1 root root 1839 Aug 9 15:45 /tmp/mytest1/profile drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/profile.d -rw-r--r--. 1 root root 6455 Aug 9 15:45 /tmp/mytest1/protocols drwxr-xr-x. 2 root root 4096 Aug 9 15:45 /tmp/mytest1/pulse
11、复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中。
[root@captain ~]# mkdir /tmp/mytest2 [root@captain ~]# cp -r /etc/*.d /tmp/mytest2/ [root@captain ~]# ll -dl /tmp/mytest2/*.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/bash_completion.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/cgconfig.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/chkconfig.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/cron.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/depmod.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/dracut.conf.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/event.d lrwxrwxrwx. 1 root root 11 Aug 9 15:47 /tmp/mytest2/init.d -> rc.d/init.d drwxr-xr-x. 3 root root 4096 Aug 9 15:47 /tmp/mytest2/latrace.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/ld.so.conf.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/libibverbs.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/logrotate.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/lsb-release.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/makedev.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/modprobe.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/oddjobd.conf.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/pam.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/popt.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/prelink.conf.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/profile.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc0.d -> rc.d/rc0.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc1.d -> rc.d/rc1.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc2.d -> rc.d/rc2.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc3.d -> rc.d/rc3.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc4.d -> rc.d/rc4.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc5.d -> rc.d/rc5.d lrwxrwxrwx. 1 root root 10 Aug 9 15:47 /tmp/mytest2/rc6.d -> rc.d/rc6.d drwxr-xr-x. 10 root root 4096 Aug 9 15:47 /tmp/mytest2/rc.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/request-key.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/rsyslog.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/rwtab.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/setuptool.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/statetab.d drwxr-x---. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/sudoers.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/xinetd.d drwxr-xr-x. 2 root root 4096 Aug 9 15:47 /tmp/mytest2/yum.repos.d
12、复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中。
[root@captain ~]# mkdir /tmp/mytest3 [root@captain ~]# cp -r /etc/[lmn]*.conf /tmp/mytest3/ [root@captain ~]# ls -dl /tmp/mytest3/[lmn]*.conf -rw-r--r--. 1 root root 1662 Aug 9 15:51 /tmp/mytest3/latrace.conf -rw-r--r--. 1 root root 28 Aug 9 15:51 /tmp/mytest3/ld.so.conf -rw-r-----. 1 root root 191 Aug 9 15:51 /tmp/mytest3/libaudit.conf -rw-r--r--. 1 root root 2293 Aug 9 15:51 /tmp/mytest3/libuser.conf -rw-r--r--. 1 root root 662 Aug 9 15:51 /tmp/mytest3/logrotate.conf -rw-r--r--. 1 root root 10814 Aug 9 15:51 /tmp/mytest3/ltrace.conf -rw-r--r--. 1 root root 827 Aug 9 15:51 /tmp/mytest3/mke2fs.conf -rw-r--r--. 1 root root 769 Aug 9 15:51 /tmp/mytest3/mongod.conf -rw-r-----. 1 root root 992 Aug 9 15:51 /tmp/mytest3/named.conf -rw-r--r--. 1 root root 3605 Aug 9 15:51 /tmp/mytest3/nfsmount.conf -rw-r--r--. 1 root root 1688 Aug 9 15:51 /tmp/mytest3/nsswitch.conf -rw-r--r--. 1 root root 1780 Aug 9 15:51 /tmp/mytest3/ntp1.conf -rw-r--r--. 1 root root 1778 Aug 9 15:51 /tmp/mytest3/ntp.conf -rw-r--r--. 1 root root 91 Aug 9 15:51 /tmp/mytest3/numad.conf