1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
文件管理类命令: 复制:CP ,移动:MV 删除:RM
cp
命令功能:将一个或多个源文件或目录复制到指定的目标文件或目录
命令格式:
cp [OPTION]... [-T]SOURCE DEST //cp [选项]…[-T]源目的
cp [OPTION]...SOURCE... DIRECTORY // cp [选项]…源…目录
cp [OPTION]... -tDIRECTORY SOURCE... // cp [选项]…-t 目录 源…
常用选项:
-i:交互式复制,即覆盖之前提醒用户确认
-f:强制覆盖目标文件
-r:递归复制目录(大写R也是这个功能)
-d:--no-dereference --preserv=links 复制符号链接文件本身,而非其指向的源文件
-a:归档,相当于-dR --preserve=all,archive,用于实现归档;
--preserve[=ATTR_LIST]
mode:权限 #默认
ownership:属主和属组 #默认
timestamps:时间戳 #默认
context:安全标签
xattr:扩展属性
links:符号链接
all:上述所有属性
-p:--preserv=mode,ownership,timestamp
-v:--verbose
命令示例:
(1)复制和改名
[root@hiall2016 ~]# ls
anaconda-ks.cfg conf.n cshrc cshrcvc h opt
[root@hiall2016 ~]# cp anaconda-ks.cfg /tmp
[root@hiall2016 ~]# ls /tmp
anaconda-ks.cfg gates.lod hsperfdata_root moni.lod
[root@hiall2016 ~]# cp anaconda-ks.cfg /tmp
cp: overwrite ‘/tmp/anaconda-ks.cfg’? y
[root@hiall2016 ~]# 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@hiall2016 ~]# cp anaconda-ks.cfg /tmp/ana.cfg
[root@hiall2016 ~]# ls /tmp
ana.cfg anaconda-ks.cfg gates.lod hsperfdata_root moni.lod
(2)使用-r参数,复制目录
[root@hiall2016 ~]# cp test /tmp
cp: omitting directory ‘test’
[root@hiall2016 ~]# cp -r test /tmp
[root@hiall2016 ~]# ls /tmp/test/
test.txt
(3)使用-a参数,把权限等都复制过来
[root@hiall2016 tmp]# ls -l test
total 4
-rw-r--r--. 1 root root 3 Aug 14 10:43test.txt
[root@hiall2016 tmp]# ls /root
anaconda-ks.cfg conf.n cshrc cshrcvc h opt test
[root@hiall2016 tmp]# cp /root/conf.n .
[root@hiall2016 tmp]# ls -l
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:52 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
[root@hiall2016 tmp]# rm conf.n
rm: remove regular file ‘conf.n’? y
[root@hiall2016 tmp]# cp -a /root/conf.n .
[root@hiall2016 tmp]# ls -l
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
mv
命令功能:为文件或目录改名,或将文件或目录移动到其它位置
命令格式:
mv [OPTION]...[-T] SOURCE DEST
mv [OPTION]...SOURCE... DIRECTORY
mv [OPTION]...-t DIRECTORY SOURCE...
常用选项:
-i:交互式
-f:force强制覆盖,不提示
-v:显示移动过程
-u:若目标文件已经存在,且 source 比较新,才会更新(update)
-b:若需覆盖文件,则覆盖前先行备份
-t:即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后
(1)移动文件
[root@hiall2016 tmp]# ll
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
drw-------. 2 root root 6 Aug 14 10:48 test2
[root@hiall2016 tmp]# mv test2 /root
[root@hiall2016 tmp]# ll
total 20
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rw-r--r--. 1 root root 73 Aug 14 10:19 conf.n
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 21 Aug 14 10:43 test
[root@hiall2016 tmp]# mv conf.n test
[root@hiall2016 tmp]# ls /test
ls: cannot access /test: No such file ordirectory
[root@hiall2016 tmp]# ls /tmp/test/
conf.n test.txt
(2)使用-f参数,强制移动不提示
[root@hiall2016 tmp]# ll
total 16
-rw-------. 1 root root 1651 Aug 14 10:39ana.cfg
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 3 root root 46 Aug 14 11:02 test
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
[root@hiall2016 tmp]# cp ana.cfg /test2
[root@hiall2016 tmp]# ls
ana.cfg anaconda-ks.cfg gates.lod hsperfdata_root moni.lod test test2
[root@hiall2016 tmp]# mv ana.cfg /test2
mv: overwrite ‘/test2’? n
[root@hiall2016 tmp]# mv -f ana.cfg /test2
rm
命令功能:删除一个目录中的一个或多个文件或目录
命令格式:rm[OPTION]... FILE...
命令选项:
-i:interactive交互式
-f:force强制删除
-r:recursive递归删除
删除目录:rm -rf/PATH/TO/DIR
危险操作:rm -rf /*(6和7有提示,5没有)
命令示例:
[root@hiall2016tmp]# ls
anaconda-ks.cfg gates.lod hsperfdata_root moni.lod test test2
[root@hiall2016 tmp]# cd test
[root@hiall2016 test]# ls
conf.n test2 test.txt
[root@hiall2016 test]# cd ..
[root@hiall2016 tmp]# rm test
rm: cannot remove ‘test’: Is a directory
[root@hiall2016 tmp]# rm -f test
rm: cannot remove ‘test’: Is a directory
[root@hiall2016 tmp]# rm -rf test
[root@hiall2016 tmp]# ll
total 12
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
2、bash的工作特性之命令执行状态返回值和命令行展开涉及的内容及其示例演示。
答:bash命令执行完成后会有一个返回值,保存在$?中,如果正常执行,返回0,错误则返回值为1-255之间的数字。当执行命令后,执行echo$?查看。
命令示例:
[root@hiall2016 tmp]# echo $?
0
[root@hiall2016 tmp]# lss -l
-bash: lss: command not found
[root@hiall2016 tmp]# ls -l
total 12
-rw-------. 1 root root 1651 Aug 14 10:37anaconda-ks.cfg
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 gates.lod
drwxr-xr-x. 2 root root 30 Aug 11 08:24 hsperfdata_root
-rwxr-xr-x. 1 root root 5 Aug 12 14:09 moni.lod
drwxr-xr-x. 2 root root 6 Aug 14 11:00 test2
[root@hiall2016 tmp]# echo $?
0
[root@hiall2016 tmp]# lssss -l
-bash: lssss: command not found
[root@hiall2016 tmp]# echo $?
127
[root@hiall2016 tmp]#
3、请使用命令行展开功能来完成以下练习:
(1)、创建/tmp目录下的:a_c, a_d,b_c, 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
1
[root@hiall2016 tmp]# mkdir -v {a,b}_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
2
root@cat ~]# 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}
4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
答:
三个时间戳:
Accesstime: 访问时间,简写为atime,读取文件内容
Modifytime: 修改时间,mtime,改变文件内容(数据)
Changetime: 改动时间,ctime,元数据发生改变
使用stat命令查看时间戳信息
命令事例:
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:14:33.271718977 -0400
Modify: 2016-08-14 11:14:27.771718824 -0400
Change: 2016-08-14 11:14:27.771718824 -0400
Birth: -
[root@hiall2016 mylinux]# ls
bin boot dev etc lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls
bin boot dev etc lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# touchu bin
-bash: touchu: command not found
[root@hiall2016 mylinux]# touch bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:20:54.901729622 -0400
Modify: 2016-08-14 11:20:54.901729622 -0400
Change: 2016-08-14 11:20:54.901729622 -0400
Birth: -
[root@hiall2016 mylinux]# touchu -a bin
-bash: touchu: command not found
[root@hiall2016 mylinux]# touch -a bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:21:33.325730694 -0400
Modify: 2016-08-14 11:20:54.901729622 -0400
Change: 2016-08-14 11:21:33.325730694 -0400
Birth: -
[root@hiall2016 mylinux]#
(2)使用-m -t参数改修改时间
[root@hiall2016 mylinux]# touch -m -t201508142324.35 bin
[root@hiall2016 mylinux]# stat bin
File: ‘bin’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 8429698 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context:unconfined_u:object_r:user_tmp_t:s0
Access: 2016-08-14 11:21:33.325730694 -0400
Modify: 2015-08-14 23:24:35.000000000 -0400
Change: 2016-08-14 11:24:39.415735884 -0400
Birth: -
5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
答:
(1)定义命令别名通过alias命令实现:
alias NAME=‘VALUE‘
[root@hiall2016 mylinux]# aliasipconfiginfo=‘cat /etc/sysconfig/network-scripts/ifcfg-em1‘
[root@hiall2016 mylinux]# ipconfiginfo |tail -3
IPADDR=192.168.1.252
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
6、显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。
[root@hiall2016 mylinux]# touch Iabc2.a
[root@hiall2016 mylinux]# ls
bin boot dev etc Iabc2.a lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls -dl*[[:digit:]]*[[:lower:]]
ls: cannot accessl*[[:digit:]]*[[:lower:]]: No such file or directory
[root@hiall2016 mylinux]# ls -d I*[[:digit:]]*[[:lower:]]
Iabc2.a
7、显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录。
[root@hiall2016 mylinux]# mkdir a333 3aaa
[root@hiall2016 mylinux]# ls -d/tmp/mylinux/[0-9]*[^0-9]
/tmp/mylinux/233aaaa23233a /tmp/mylinux/3aaa
[root@hiall2016 mylinux]#
8、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录。
[root@hiall2016 mylinux]# ls
233aaaa23233a 23fdfds2 3aaa a333 bin boot dev etc Iabc2.a lbcd@..z lib lib64 lock log proc run sbin sys tmp usr var
[root@hiall2016 mylinux]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
ls: cannot access/etc/[^[:alpha:]][[:alpha:]]*: No such file or directory
[root@hiall2016 mylinux]# ls -d/tmp/mylinux/[^[:alpha:]][[:alpha:]]*
/tmp/mylinux/3aaa
9、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-08-06-09-32-22。
[root@hiall2016 mylinux]# touch /tmp/tfile-`date+"%Y-%m-%d-%H-%M-%S"`
[root@hiall2016 mylinux]# ls -d /tmp/tf*[0-9]
/tmp/tfile-2016-08-14-11-45-51
[root@hiall2016 mylinux]#
10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
[root@hiall2016 tmp]# ls
a_c a_d anaconda-ks.cfg b_c b_d gates.lod hsperfdata_root moni.lod mylinux mytest1 test2 tfile-2016-08-14-11-45-51
[root@hiall2016 tmp]# ls -d /etc/p*[^[:digit:]]
/etc/pam.d /etc/passwd- /etc/plymouth /etc/popt.d /etc/ppp /etc/printcap /etc/profile.d /etc/protocols
/etc/passwd /etc/pki /etc/pm /etc/postfix /etc/prelink.conf.d /etc/profile /etc/profile.ori /etc/python
[root@hiall2016 tmp]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/
[root@hiall2016 tmp]# ls -d/tmp/mytest1/p*[^[:digit:]]
/tmp/mytest1/pam.d /tmp/mytest1/passwd- /tmp/mytest1/plymouth /tmp/mytest1/popt.d /tmp/mytest1/ppp /tmp/mytest1/printcap /tmp/mytest1/profile.d /tmp/mytest1/protocols
/tmp/mytest1/passwd /tmp/mytest1/pki /tmp/mytest1/pm /tmp/mytest1/postfix /tmp/mytest1/prelink.conf.d /tmp/mytest1/profile /tmp/mytest1/profile.ori /tmp/mytest1/python
[root@hiall2016 tmp]#
11、复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中。、
[root@hiall2016tmp]# mkdir /tmp/mytest2
[root@hiall2016 tmp]# ls -d /etc/*.d
/etc/bash_completion.d /etc/depmod.d /etc/init.d /etc/modules-load.d /etc/prelink.conf.d /etc/rc2.d /etc/rc6.d /etc/statetab.d /etc/xinetd.d
/etc/binfmt.d /etc/dnsmasq.d /etc/ld.so.conf.d /etc/my.cnf.d /etc/profile.d /etc/rc3.d /etc/rc.d /etc/sudoers.d /etc/yum.repos.d
/etc/chkconfig.d /etc/dracut.conf.d /etc/logrotate.d /etc/pam.d /etc/rc0.d /etc/rc4.d /etc/rsyslog.d /etc/sysctl.d
/etc/cron.d /etc/grub.d /etc/modprobe.d /etc/popt.d /etc/rc1.d /etc/rc5.d /etc/rwtab.d /etc/tmpfiles.d
[root@hiall2016 tmp]# cp -r /etc/*.d /tmp/mytest2
[root@hiall2016 tmp]# ls -d /tmp/mytest2/*.d
/tmp/mytest2/bash_completion.d /tmp/mytest2/dnsmasq.d /tmp/mytest2/logrotate.d /tmp/mytest2/popt.d /tmp/mytest2/rc2.d /tmp/mytest2/rc.d /tmp/mytest2/sysctl.d
/tmp/mytest2/binfmt.d /tmp/mytest2/dracut.conf.d /tmp/mytest2/modprobe.d /tmp/mytest2/prelink.conf.d /tmp/mytest2/rc3.d /tmp/mytest2/rsyslog.d /tmp/mytest2/tmpfiles.d
/tmp/mytest2/chkconfig.d /tmp/mytest2/grub.d /tmp/mytest2/modules-load.d /tmp/mytest2/profile.d /tmp/mytest2/rc4.d /tmp/mytest2/rwtab.d /tmp/mytest2/xinetd.d
/tmp/mytest2/cron.d /tmp/mytest2/init.d /tmp/mytest2/my.cnf.d /tmp/mytest2/rc0.d /tmp/mytest2/rc5.d /tmp/mytest2/statetab.d /tmp/mytest2/yum.repos.d
/tmp/mytest2/depmod.d /tmp/mytest2/ld.so.conf.d /tmp/mytest2/pam.d /tmp/mytest2/rc1.d /tmp/mytest2/rc6.d /tmp/mytest2/sudoers.d
[root@hiall2016 tmp]#
12、复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中。
[root@hiall2016 tmp]# mkdir/tmp/mytest3
[root@hiall2016 tmp]# ls -d /etc/[1,m,n]*.conf
/etc/man_db.conf /etc/mke2fs.conf /etc/nsswitch.conf
[root@hiall2016 tmp]# cp -r/etc/[1,m,n]*.conf /tmp/mytest3/
[root@hiall2016 tmp]# ls -d/tmp/mytest3/[1,m,n]*.conf
/tmp/mytest3/man_db.conf /tmp/mytest3/mke2fs.conf /tmp/mytest3/nsswitch.conf
[root@hiall2016 tmp]#
本文出自 “9099360” 博客,请务必保留此出处http://9109360.blog.51cto.com/9099360/1837931
原文地址:http://9109360.blog.51cto.com/9099360/1837931