N27_第二周作业。

linux上的文件管理类命令都有哪些?其常用的使用方法及其相关示例演示。

答:

有cp、mv、rm

cp  复制命令

常用选项:

cp -i        #交互式

cp -r       #递归复制目录及其子目录的内容

cp -a      # 归档,相当于dR –preserve=all

cp -d     #表示不跟踪符号链接所指向的原文件 –no-dereference –preserve=links

cp -p     #保持原有的属性。c

cp -f      #强制覆盖

cp -v     #显示过程

示例:    ]# cp -i ping.pl /opt/
cp: overwrite `/opt/ping.pl’?

cp -r #递归复制目录及其子目录的内容

示例: TIM截图20171009153501

TIM截图20171009154351

cp -a #归档 相当于 -dR –preserve=all

示例:TIM截图20171009160029

 

-d:若源文件为连接文件杜属性,则复制连接文件属性而非文件本身。
例如:
1、
root@ubuntu:/tmp# cp -s bashrc bashrc_slink #建立一个快捷方式
root@ubuntu:/tmp# cp -l bashrc bashrc_hlink #建立一个硬连接
root@ubuntu:/tmp# ls -l bashrc*
-rw-r–r– 2 root root 3106 2012-01-16 14:31 bashrc
-rw-r–r– 2 root root 3106 2012-01-16 14:31 bashrc_hlink
lrwxrwxrwx 1 root root 6 2012-01-16 15:24 bashrc_slink -> bashrc
2、将上面生成的bashrc_slink 复制成为bashrc_slink_2
root@ubuntu:/tmp# cp bashrc_slink bashrc_slink_2
root@ubuntu:/tmp# ls -l bashrc_slink*
lrwxrwxrwx 1 root root 6 2012-01-16 15:24 bashrc_slink -> bashrc
-rw-r–r– 1 root root 3106 2012-01-16 15:35 bashrc_slink_2 #原本要复制的是连接文件,却将连接文件连接的实际文件复制过来了
3、若要复制连接文件而不是它指向的源文件,就要使用-d参数
root@ubuntu:/tmp# cp -d bashrc_slink bashrc_slink_2
root@ubuntu:/tmp# ls -l bashrc_slink*
lrwxrwxrwx 1 root root 6 2012-01-16 15:24 bashrc_slink -> bashrc
lrwxrwxrwx 1 root root 6 2012-01-16 15:36 bashrc_slink_2 -> bashrc

cp -p #保持原有的属性

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# cp -p ip.txt ip2.txt
[root@iZtd89lux3qk03uhzhj85mZ opt]# ll -ah ip*
-rw-r–r– 1 root root 1.2K Oct 9 14:47 ip2.txt
-rw-r–r– 1 root root 1.2K Oct 9 14:47 ip.txt

cp -v # 显示过程

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# cp -rv tmp/ tmp2/
`tmp/’ -> `tmp2/’
`tmp/mylinux’ -> `tmp2/mylinux’
`tmp/mylinux/lib’ -> `tmp2/mylinux/lib’
`tmp/mylinux/lib/modules’ -> `tmp2/mylinux/lib/modules’
`tmp/mylinux/sbin’ -> `tmp2/mylinux/sbin’
`tmp/mylinux/proc’ -> `tmp2/mylinux/proc’
`tmp/mylinux/dev’ -> `tmp2/mylinux/dev’
`tmp/mylinux/lib64′ -> `tmp2/mylinux/lib64′
`tmp/mylinux/usr’ -> `tmp2/mylinux/usr’
`tmp/mylinux/usr/local’ -> `tmp2/mylinux/usr/local’
`tmp/mylinux/usr/local/sbin’ -> `tmp2/mylinux/usr/local/sbin’
`tmp/mylinux/usr/local/bin’ -> `tmp2/mylinux/usr/local/bin’
`tmp/mylinux/etc’ -> `tmp2/mylinux/etc’
`tmp/mylinux/etc/sysconfig’ -> `tmp2/mylinux/etc/sysconfig’
`tmp/mylinux/etc/sysconfig/network-scripts’ -> `tmp2/mylinux/etc/sysconfig/network-scripts’
`tmp/mylinux/etc/rc.d’ -> `tmp2/mylinux/etc/rc.d’
`tmp/mylinux/etc/rc.d/init.d’ -> `tmp2/mylinux/etc/rc.d/init.d’
`tmp/mylinux/var’ -> `tmp2/mylinux/var’
`tmp/mylinux/var/log’ -> `tmp2/mylinux/var/log’
`tmp/mylinux/var/run’ -> `tmp2/mylinux/var/run’
`tmp/mylinux/var/lock’ -> `tmp2/mylinux/var/lock’
`tmp/mylinux/sys’ -> `tmp2/mylinux/sys’
`tmp/mylinux/tmp’ -> `tmp2/mylinux/tmp’
`tmp/mylinux/boot’ -> `tmp2/mylinux/boot’
`tmp/mylinux/boot/grub’ -> `tmp2/mylinux/boot/grub’
`tmp/mylinux/bin’ -> `tmp2/mylinux/bin’
[root@iZtd89lux3qk03uhzhj85mZ opt]# tree t
test/ test2/ tmp/ tmp2/
[root@iZtd89lux3qk03uhzhj85mZ opt]# tree tmp2/
tmp2/
└── 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

25 directories, 0 files

mv #移动或者重命名命令。

选项:

mv -i #交互式,会提醒是否移动

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# mv -i test/* test2/
mv: overwrite `test2/BingSiteAuth.xml’?

mv -f  # 强制

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# mv -f ip2.txt ip.txt ping.pl test2/
[root@iZtd89lux3qk03uhzhj85mZ opt]# ll test2/*
-rw-r–r– 1 root root 1191 Oct 9 14:47 test2/ip2.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 test2/ip.txt
-rw-r–r– 1 root root 756 Oct 9 14:56 test2/ping.pl

 

mv  文件或者目录  新的文件名或者新的目录名

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# ll
total 28
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip2.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip.txt
-rw-r–r– 1 root root 756 Oct 9 14:56 ping.pl
drwxr-xr-x 7 root root 4096 Oct 9 17:00 test
drwxr-xr-x 2 root root 4096 Oct 9 17:05 test2
drwxr-xr-x 3 root root 4096 Oct 8 15:47 tmp
drwxr-xr-x 3 root root 4096 Oct 9 16:45 tmp2
[root@iZtd89lux3qk03uhzhj85mZ opt]# mv ip2.txt ip1.txt
[root@iZtd89lux3qk03uhzhj85mZ opt]# ll
total 28
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip1.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip.txt
-rw-r–r– 1 root root 756 Oct 9 14:56 ping.pl
drwxr-xr-x 7 root root 4096 Oct 9 17:00 test
drwxr-xr-x 2 root root 4096 Oct 9 17:05 test2
drwxr-xr-x 3 root root 4096 Oct 8 15:47 tmp
drwxr-xr-x 3 root root 4096 Oct 9 16:45 tmp2

rm #删除命令

rm -i         #交互式,提示是否删除

rm -f         #强制删除。

rm -r       #递归删除

 

rm -i  #交互式,提示是否删除。

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# rm -i ip2.txt
rm: remove regular file `ip2.txt’?

rm -r    #递归删除

[root@iZtd89lux3qk03uhzhj85mZ opt]# ll
total 28
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip1.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip.txt
-rw-r–r– 1 root root 756 Oct 9 14:56 ping.pl
drwxr-xr-x 7 root root 4096 Oct 9 17:00 test
drwxr-xr-x 2 root root 4096 Oct 9 17:13 test2
drwxr-xr-x 3 root root 4096 Oct 8 15:47 tmp
drwxr-xr-x 3 root root 4096 Oct 9 16:45 tmp2
[root@iZtd89lux3qk03uhzhj85mZ opt]# ll test2/
total 16
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip2.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip3.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip4.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip5.txt
[root@iZtd89lux3qk03uhzhj85mZ opt]# rm -r test2/*
rm: remove regular file `test2/ip2.txt’? y
rm: remove regular file `test2/ip3.txt’? y
rm: remove regular file `test2/ip4.txt’? y
rm: remove regular file `test2/ip5.txt’? y
[root@iZtd89lux3qk03uhzhj85mZ opt]# ll test2/
total 0
[root@iZtd89lux3qk03uhzhj85mZ opt]#

rm -f       #强制删除

2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容

bash使用特殊变量$?保存最近一条命令的执行状态结果 :echo $?

0:成功   1-255:失败

示例:

root@iZtd89lux3qk03uhzhj85mZ opt]# ll
total 24
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip1.txt
-rw-r–r– 1 root root 1191 Oct 9 14:47 ip.txt
drwxr-xr-x 7 root root 4096 Oct 9 17:00 test
drwxr-xr-x 2 root root 4096 Oct 9 17:14 test2
drwxr-xr-x 3 root root 4096 Oct 8 15:47 tmp
drwxr-xr-x 3 root root 4096 Oct 9 16:45 tmp2
[root@iZtd89lux3qk03uhzhj85mZ opt]# echo $?
0
[root@iZtd89lux3qk03uhzhj85mZ opt]# mv ip1.txt ip2.txt -R
mv: invalid option — ‘R’
Try `mv –help’ for more information.
[root@iZtd89lux3qk03uhzhj85mZ opt]# echo $?
1

PS:正确执行的时候显示0 ,错误执行的时候显示1-255

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

(1)、创建/tmp目录下的:a_c,a-d,b_c,b_d

示例:

[root@iZtd89lux3qk03uhzhj85mZ opt]# mkdir -pv /tmp/{a,b}_{c,d}
mkdir: created directory `/tmp/a_c’
mkdir: created directory `/tmp/b_c’
mkdir: created directory `/tmp/b_d’

(2)、创建、/tmp/mylinux下的

示例:

[root@iZtd89lux3qk03uhzhj85mZ tmp]# 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@iZtd89lux3qk03uhzhj85mZ tmp]# 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

24 directories, 0 files

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

(0)
zhao8486814zhao8486814
上一篇 2017-10-09 15:30
下一篇 2017-10-09 19:21

相关推荐

  • 文本及文件查找练习

    1、显示当前系统上root、fedora或user1用户的默认shell;     ~]# cat /etc/passwd | grep "^\(root\|fedora\|user1\)\>" | cut -d: -f1,7    root:/bin/…

    Linux干货 2016-10-24
  • N22-第一周作业

    马哥教育网络班22期第1周课程练习 第一部分,计算机构成和功能简述 一、计算机体系 从最初的计算机ENIAC到当今时代的计算机都采用冯●诺依曼体系结构,该结构宗旨为:数字计算机的数制采用二进制;计算机应该按照程序顺序执行。 二、计算机硬件组成 计算机基本硬件由以下组件构成: CPU:       &…

    Linux干货 2016-08-22
  • man和history及bashi的快捷键

    man:帮助命令     命令格式:man# cmd;使用时可以先使用whatis 查询所对应的man的章节,然后用对应的man章节来查询帮助;     info:本地帮助文档     官方文档,在线查询等等,如果是付费版系统的话可以使用S…

    Linux干货 2016-07-29
  • 第三周

    第三周blog 第三周blog 1    列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。    # who|cut -d’ ‘ -f 1 |sort -u 2    取出最后登录到当前系统的用户的相关信息。  …

    Linux干货 2016-12-19
  • linux基础第二周

    1. 用chattr命令防止系统中某个关键文件被修改:# chattr +i /etc/resolv.conf 然后用mv /etc/resolv.conf等命令操作于该文件,都是得到Operation not permitted 的结果。vim编辑该文件时会提示W10: Warning: Changing a readonly file错误。要想修改此文件…

    2017-09-09
  • 马哥教育网络20期+第五周练习博客

    1、  显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@localhost ~]# grep "^[[:space:]]\+.*" /boot/grub/grub.conf          root (hd0…

    Linux干货 2016-08-02