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 #递归复制目录及其子目录的内容
示例:
cp -a #归档 相当于 -dR –preserve=all
示例:
-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