第二周 文件管理

1.Linux上的文件管理类型命令都有哪些,其常用的使用方法及其相关示例演示

mkdir—— 创建目录  make directory

命令格式——mkdir [OPTION]… DIRECTORY…

-p:自动按需创建父目录;

-v:verbose,显示详细过程;

-m MODE:创建目录直接给定权限,否则是默认权限;

注意:路径基名方为命令的作用对象;基名之前的路径必须得存在

示例:

1.创建名为letter 的目录,在它的目录下包含有子目录 important

[root@localhost ~]# mkdir -p letter/important

[root@localhost ~]# ls -R

.:

letter

 

./letter:

important

 

./letter/important:

2.打印创建ubuntu redhat slackware目录的过程

[root@localhost ~]# mkdir -v ubuntu redhat slackware

mkdir: created directory ‘ubuntu’

mkdir: created directory ‘redhat’

mkdir: created directory ‘slackware’

3.创建le目录,并赋予le目录为只读权限

[root@localhost ~]# mkdir -m=r– le

[root@localhost ~]# ls -l

dr–r–r– 2 root root 4096 Dec 10 05:53 le

 

rmdir—— 删除目录 remove empty directories 移除空目录

命令格式——rmdir [OPTION]… DIRECTORY…

-p:删除某目录后,如果其父目录为空,则一并删除之;

-v:显示过程;

 

touch—— 创建文件 创建空白文本文件与设置文件的各种时间

命令格式——touch [选项] [文件]

-a:仅修改“访问时间”(atime)

-m:仅修改“更改时间”(mtime)

-d:同时修改atime与mtime

示例

[root@localhost tmp]# touch -d “2017-12-09 10:22” issue.out

[root@localhost tmp]# ls -l issue.out

-rw-r–r– 1 root root 23 Dec 9 10:22 issue.out

 

cp—— 文件复制 copy 复制文件是复制文件的数据

源文件source;目标文件dest;

单源复制:cp [OPTION]… [-T] SOURCE DEST

多源复制:cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

 

单源复制:cp [OPTION]… [-T] SOURCE DEST

如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中;

如果DEST存在:

如果DEST是非目录文件:则覆盖目标文件;

如果DEST是目录文件:则先在DEST目录下创建一个与源文件同名的文件,并复制其数据流;

 

多源复制:cp [OPTION]… SOURCE… DIRECTORY

cp [OPTION]… -t DIRECTORY SOURCE…

 

如果DEST不存在:错误;

如果DEST存在:

如果DEST是非目录文件:错误;

如果DEST是目录文件:分别复制每个文件至目标目录中,并保持原名;

-i:交互式复制,即覆盖之前提醒用户确认;

-f:强制覆盖目标文件;

-r, -R:递归复制目录;

-d:复制符号链接文件本身,而非其指向的源文件;

-a:-dR –preserve=all, archive,用于实现归档;

–preserv=

mode:权限

ownership:属主和属组

timestamps:时间戳

context:安全标签

xattr:扩展属性

links:符号链接

all:上述所有属性

示例

[root@localhost ~]# cp a /tmp

[root@localhost ~]# ls -l /tmp

total 92

-rw-r–r– 1 root root     0 Dec 10 06:27 a

 

[root@localhost ~]# cp a b c /tmp

[root@localhost ~]# ls -l /tmp

total 92

-rw-r–r– 1 root root     0 Dec 10 06:30 a

-rw-r–r– 1 root root     0 Dec 10 06:30 b

-rw-r–r– 1 root root     0 Dec 10 06:30 c

 

mv—— 移动并重命名文件    move

命令格式

mv [OPTION]… [-T] SOURCE DEST

mv [OPTION]… SOURCE… DIRECTORY

mv [OPTION]… -t DIRECTORY SOURCE..

-i:交互式

-f:force 强制覆盖

实例

将文件ex3改名为new1

mv ex3 new1

将目录/usr/men中的所有文件移到当前目录(用.表示)中:

mv /usr/men/* .

 

rm—— 删除文件  remove

命令格式——rm [OPTION]… FILE…

-i:interactive 交互式

-f:force 强制

-r:recursive 递归

删除目录:rm -rf /PATH/TO/DIR

危险操作:rm -rf /*

注意:所有不用的文件建议不要直接删除,而是移动至某个专用目录;(模拟回收站)

 

2.bash的工作特性之命令执行后状态返回值和命令展开所涉及的内容及其示例演示

命令执行的状态结果:

bash通过状态返回值来输出此结果:

成功:0

失败:1-255

 

命令执行完成之后,其状态返回值保存于bash的特殊变量$?中;

 

命令正常执行时,有的还回有命令返回值:

根据命令及其功能不同,结果各不相同;

 

[root@localhost ~]# ls /etc/sysconfig

anaconda   ebtables-config   irqbalance   network         selinux

authconfig firewalld         kdump       network-scripts sshd

cbq         grub             kernel     rdisc           wpa_supplicant

console     init             man-db     readonly-root

cpupower   ip6tables-config modules     rsyslog

crond       iptables-config   netconsole   run-parts

[root@localhost ~]# echo $?

0

[root@localhost ~]# ls /etc/sysconfigg

ls: cannot access /etc/sysconfigg: No such file or directory

[root@localhost ~]# echo $?

2

[root@localhost ~]# lss /etc/sysconfig

-bash: lss: command not found

[root@localhost ~]# echo $?

127

 

3.请命令行展开功能来完成以下练习:

(1)创建/tmp下 a_c a_d b_c b_d

[root@localhost ~]# mkdir -v /tmp{a,b}_{c,d}

mkdir: created directory ‘/tmpa_c’

mkdir: created directory ‘/tmpa_d’

mkdir: created directory ‘/tmpb_c’

mkdir: created directory ‘/tmpb_d’

 

(2)创建/tmp/mylinux目录下的:

untitled

[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’

 

4.文件元数据信息有哪些,分别表示什么含义,如何查看,如何修改文件的时间戳信息

文件:每一文件都有两类数据

元数据:metadata

数据: data

 

stat—— 显示文件和文件系统的状态 display file or file system statu

命令格式——stat FILE…

[root@localhost ~]# stat anaconda-ks.cfg

File: ‘anaconda-ks.cfg’      //文件名

Size: 1423             Blocks: 8         IO Block: 4096   regular file   //大小 块 IO块 普通文件

Device: 802h/2050d        Inode: 529006     Links: 1   //块设备号 Inode号 被硬链接的次数

Access: (0600/-rw——-)   Uid: (   0/   root)   Gid: (   0/   root)   //访问权限 UID GID

Access: 2017-12-09 17:59:19.374026733 +0800   //访问时间

Modify: 2017-12-09 17:59:19.380026733 +0800 //修改时间

Change: 2017-12-09 17:59:19.380026733 +0800 //更改时间

Birth: –

 

touch—— 修改文件的时间戳 change file timestamps

命令格式——touch [OPTION]… FILE…

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500             Blocks: 40         IO Block: 4096   regular file

Device: 802h/2050d        Inode: 786457       Links: 1

Access: (0644/-rw-r–r–) Uid: (   0/     root)   Gid: (   0/     root)

Access: 2017-12-05 19:20:34.088464502 +0800

Modify: 2017-12-05 19:20:34.088464502 +0800

Change: 2017-12-05 19:20:34.088464502 +0800

Birth: –

[root@localhost ~]# touch /tmp/functions

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500             Blocks: 40         IO Block: 4096   regular file

Device: 802h/2050d        Inode: 786457       Links: 1

Access: (0644/-rw-r–r–) Uid: (   0/     root)   Gid: (   0/     root)

Access: 2017-12-05 19:20:53.516463898 +0800

Modify: 2017-12-05 19:20:53.516463898 +0800

Change: 2017-12-05 19:20:53.516463898 +0800

Birth: –

-c: 指定的文件路径不存在时不予创建;

-a: 仅修改access time;

[root@localhost ~]# touch -a /tmp/func

[root@localhost ~]# stat /tmp/func

File: ‘/tmp/func’

Size: 0                 Blocks: 0         IO Block: 4096   regular empty file

Device: 802h/2050d        Inode: 786490       Links: 1

Access: (0644/-rw-r–r–) Uid: (   0/     root)   Gid: (   0/     root)

Access: 2017-12-09 17:41:17.468048427 +0800

Modify: 2017-12-09 17:41:17.467048427 +0800

Change: 2017-12-09 17:41:17.468048427 +0800

Birth: –

 

-m:仅修改modify time;

[root@localhost ~]# touch -m /tmp/func

[root@localhost ~]# stat /tmp/func

File: ‘/tmp/func’

Size: 0                 Blocks: 0         IO Block: 4096   regular empty file

Device: 802h/2050d        Inode: 786490       Links: 1

Access: (0644/-rw-r–r–) Uid: (   0/     root)   Gid: (   0/     root)

Access: 2017-12-09 17:41:17.468048427 +0800

Modify: 2017-12-09 17:42:08.951049234 +0800

Change: 2017-12-09 17:42:08.951049234 +0800

Birth: –

 

-t STAMP 指定某一时间

[[CC]YY]MMDDhhmm[.ss]年月日小时分钟秒

[root@localhost ~]# touch -m -t 200212011010.10 /tmp/functions

[root@localhost ~]# stat /tmp/functions

File: ‘/tmp/functions’

Size: 17500             Blocks: 40         IO Block: 4096   regular file

Device: 802h/2050d        Inode: 786457       Links: 1

Access: (0644/-rw-r–r–) Uid: (   0/     root)   Gid: (   0/     root)

Access: 2017-12-09 19:20:53.516463898 +0800

Modify: 2002-12-01 10:10:10.000000000 +0800

Change: 2017-12-09 19:27:08.544452236 +0800

Birth: –

 

5.如何定义一个命令别名,如何在引用另一个命令的执行结果

命令别名:

获取所有可用别名的定义:

[root@localhost ~]# alias

alias cp=’cp -i’

alias egrep=’egrep –color=auto’

alias fgrep=’fgrep –color=auto’

alias grep=’grep –color=auto’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias rm=’rm -i’

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde’

 

定义别名:

[root@localhost ~]# alias cls=clear

[root@localhost ~]# alias

alias cls=’clear’

alias cp=’cp -i’

alias egrep=’egrep –color=auto’

alias fgrep=’fgrep –color=auto’

alias grep=’grep –color=auto’

alias l.=’ls -d .* –color=auto’

alias ll=’ls -l –color=auto’

alias ls=’ls –color=auto’

alias mv=’mv -i’

alias rm=’rm -i’

alias which=’alias | /usr/bin/which –tty-only –read-alias –show-dot –show-tilde’

 

注意:定义别名使用小写,如果命令之间没有空格,不需要加单引号,但仅对当前shell进程有效

 

引用命令的执行结果:

$(COMMAND)

或`COMMAND`

[root@localhost ~]# date

Tue Dec 5 18:23:28 CST 2017

[root@localhost ~]# date +%T

18:23:46

[root@localhost ~]# date +%H-%M-%S

18-24-44

[root@localhost ~]# mkdir $(date +%H-%M-%S)

[root@localhost ~]# ls

18-25-29 anaconda-ks.cfg

[root@localhost ~]# mkdir $(date +%H-%M-%S)

[root@localhost ~]# ls

18-25-29 18-25-50   anaconda-ks.cfg

[root@localhost ~]# mkdir `date +%H-%M-%S`

[root@localhost ~]# ls

18-25-29 18-25-50   18-27-01 anaconda-ks.cfg

 

6.显示/var目录下以l开头,以一个小写字母结尾,且中间至少出现一位数字(可有其它字符)的文件或目录

[root@localhost ~]# ls -d /var/l*[[:digit:]]*[[:lower:]]

 

7.显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录

[root@localhost ~]# ls -d /etc/[0-9]*[^0-9]

 

8.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

[root@localhost ~]# ls /etc/[^a-z][a-z]*

 

9.在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22

[root@localhost ~]# mkdir -pv /tmp/tfile-$(date +”%F-%H-%M-%S”)

mkdir: created directory ‘/tmp/tfile-2017-12-10-18-22-43’

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43

[root@localhost ~]# touch /tmp/file-$(date +”%F-%H-%M-%S”)

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43

[root@localhost ~]# touch /tmp/tfile-$(date +”%F-%H-%M-%S”)

[root@localhost ~]# ls -d /tmp/tfile*[0-9]

/tmp/tfile-2017-12-10-18-22-43 /tmp/tfile-2017-12-10-18-26-16

 

10.复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

[root@localhost ~]# mkdir /tmp/mytest1

[root@localhost ~]# cp -a /etc/p*[^0-9] /tmp/mytest1/

[root@localhost ~]# ls -d /tmp/mytest1/*

/tmp/mytest1/pam.d     /tmp/mytest1/pm             /tmp/mytest1/printcap

/tmp/mytest1/passwd   /tmp/mytest1/popt.d         /tmp/mytest1/profile

/tmp/mytest1/passwd-  /tmp/mytest1/postfix         /tmp/mytest1/profile.d

/tmp/mytest1/pki       /tmp/mytest1/ppp             /tmp/mytest1/protocols

/tmp/mytest1/plymouth /tmp/mytest1/prelink.conf.d /tmp/mytest1/python

 

[root@localhost ~]# \cp -rf /etc/p*[^0-9] /tmp/mytest1/

[root@localhost ~]# ls -d /tmp/mytest1/*

/tmp/mytest1/pam.d     /tmp/mytest1/pm             /tmp/mytest1/printcap

/tmp/mytest1/passwd   /tmp/mytest1/popt.d         /tmp/mytest1/profile

/tmp/mytest1/passwd-  /tmp/mytest1/postfix         /tmp/mytest1/profile.d

/tmp/mytest1/pki       /tmp/mytest1/ppp             /tmp/mytest1/protocols

/tmp/mytest1/plymouth /tmp/mytest1/prelink.conf.d /tmp/mytest1/python

 

11.复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中

[root@localhost ~]# mkdir -v /tmp/mytest2

mkdir: created directory ‘/tmp/mytest2’

[root@localhost ~]# ls -d /etc/*.d

/etc/bash_completion.d   /etc/ld.so.conf.d   /etc/rc0.d     /etc/rwtab.d

/etc/binfmt.d           /etc/logrotate.d     /etc/rc1.d     /etc/statetab.d

/etc/chkconfig.d         /etc/modprobe.d     /etc/rc2.d     /etc/sudoers.d

/etc/cron.d             /etc/modules-load.d /etc/rc3.d     /etc/sysctl.d

/etc/depmod.d           /etc/my.cnf.d       /etc/rc4.d       /etc/tmpfiles.d

/etc/dracut.conf.d       /etc/pam.d           /etc/rc5.d     /etc/xinetd.d

/etc/grub.d             /etc/popt.d         /etc/rc6.d     /etc/yum.repos.d

/etc/init.d             /etc/prelink.conf.d /etc/rc.d

/etc/krb5.conf.d       /etc/profile.d       /etc/rsyslog.d

[root@localhost ~]# cp -a /etc/*.d /tmp/mytest2

[root@localhost ~]# ls -d /tmp/mytest2/*

/tmp/mytest2/bash_completion.d /tmp/mytest2/modules-load.d /tmp/mytest2/rc6.d

/tmp/mytest2/binfmt.d           /tmp/mytest2/my.cnf.d       /tmp/mytest2/rc.d

/tmp/mytest2/chkconfig.d       /tmp/mytest2/pam.d           /tmp/mytest2/rsyslog.d

/tmp/mytest2/cron.d             /tmp/mytest2/popt.d         /tmp/mytest2/rwtab.d

/tmp/mytest2/depmod.d           /tmp/mytest2/prelink.conf.d /tmp/mytest2/statetab.d

/tmp/mytest2/dracut.conf.d     /tmp/mytest2/profile.d       /tmp/mytest2/sudoers.d

/tmp/mytest2/grub.d             /tmp/mytest2/rc0.d           /tmp/mytest2/sysctl.d

/tmp/mytest2/init.d             /tmp/mytest2/rc1.d           /tmp/mytest2/tmpfiles.d

/tmp/mytest2/krb5.conf.d       /tmp/mytest2/rc2.d           /tmp/mytest2/xinetd.d

/tmp/mytest2/ld.so.conf.d       /tmp/mytest2/rc3.d           /tmp/mytest2/yum.repos.d

/tmp/mytest2/logrotate.d       /tmp/mytest2/rc4.d

/tmp/mytest2/modprobe.d         /tmp/mytest2/rc5.d

 

12.复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件或目录至/tmp/mytest3目录中

[root@localhost ~]# mkdir -v /tmp/mytest3

mkdir: created directory ‘/tmp/mytest3’

[root@localhost ~]# ls -d /etc/[lmn]*.conf

/etc/ld.so.conf     /etc/libuser.conf /etc/logrotate.conf /etc/mke2fs.conf

/etc/libaudit.conf /etc/locale.conf   /etc/man_db.conf     /etc/nsswitch.conf

[root@localhost ~]# cp -a /etc/[lmn]*.conf /tmp/mytest3

[root@localhost ~]# ls -d /tmp/mytest3/*

/tmp/mytest3/ld.so.conf     /tmp/mytest3/locale.conf     /tmp/mytest3/mke2fs.conf

/tmp/mytest3/libaudit.conf /tmp/mytest3/logrotate.conf /tmp/mytest3/nsswitch.conf

/tmp/mytest3/libuser.conf   /tmp/mytest3/man_db.conf

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/89813

(0)
上一篇 2017-12-10 21:05
下一篇 2017-12-10 22:05

相关推荐

  • 文本编辑工具vim

                             vim +#: 打开文件直接处在#行 +/PATTERN:打开文件直接处在第一次匹配到行 vim + file :打开文件直接处在最后行 vim –b file :以二进制打开文件…

    Linux干货 2016-08-12
  • 网络N22期-第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who | cut -d" " -f1 | uniq root 2、取出最后登录到当前系统的用户的相关信息。 [roo…

    Linux干货 2016-08-29
  • 马哥教育网络20期+第二周练习博客

    1、 Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 ls 使用方式:ls [OPTION]… [DIR]… 常用选项: -a:显示所有档案及目录;  -A:显示除影藏文件“.”和“..”以外的所有文件列表; -C:多列显示输出结果。这是默认选项;  -l:与“-C”选项功能相反,所有输出…

    Linux干货 2016-06-23
  • 学习宣言

    如果自己都不愿意动,没有人能帮助我成功!

    Linux干货 2016-12-26
  • Linux运维不想早死的方法 一

    Linux运维不想早死的方法 一 为了提高工作效率,减少工作时间,爱惜生命,远离辐射;务必做好以下几点: 1,一定用快捷键         这里简单的说下几个常用的快捷按键。 Ctrl + l     清屏,相当于clear命令。 Ctrl…

    Linux干货 2017-03-26
  • httpd

      httpd http服务器程序:   httpd (apache) nginx lighttpd httpd的特性:   高度模块化: core + modules DSO:dynamic shared object(模块的动态加载) MPM:Multipath processing Modules (多路处理模块) &nb…

    Linux干货 2017-06-04

评论列表(1条)

  • 马哥教育
    马哥教育 2017-12-16 12:28

    总结很详细的一篇。继续加油~