#
## 1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示
### A.cp(copy,复制)
基本格式:cp 需要复制的文件路径 复制目的目录路径
~~~
[root@iZ23olnit2eZ ~]# ls
jdk-8u91-linux-x64.tar.gz
[root@iZ23olnit2eZ ~]# pwd
/root
[root@iZ23olnit2eZ ~]# cp /root/jdk-8u91-linux-x64.tar.gz /tmp
[root@iZ23olnit2eZ ~]# ls /tmp
jdk-8u91-linux-x64.tar.gz
~~~
### B.mv(move,移动)
基本格式:mv 需要移动的文件路径 复制移动文件路径(可改原文件名)
~~~
[root@iZ23olnit2eZ ~]# mv /root/jdk-8u91-linux-x64.tar.gz /tmp/myname
[root@iZ23olnit2eZ ~]# ls /tmp
myname
~~~
### C.rm(remove,删除)
基本格式:rm 需删除文件(不需确认则加-f参数,涉及文件夹需要递归删除时加上-rf选项)
~~~
[root@iZ23olnit2eZ ~]# rm /tmp/myname
rm: remove regular file `/tmp/myname'? y
[root@iZ23olnit2eZ ~]# ls /tmp
~~~
## 2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示
### A.查看命令执行状态
echo $? 查看命令执行返回值,为0表示执行成功,非0表示执行失败
~~~
[root@iZ23olnit2eZ ~]# ls
jdk-8u91-linux-x64.tar.gz
[root@iZ23olnit2eZ ~]# echo $?
0
[root@iZ23olnit2eZ ~]# ls nothing
ls: cannot access nothing: No such file or directory
[root@iZ23olnit2eZ ~]# echo $?
2
~~~
### B.命令行展开
~为当前系统用户家目录
~~~
[root@iZ23olnit2eZ ~]# cd /tmp
[root@iZ23olnit2eZ tmp]# cd ~
[root@iZ23olnit2eZ ~]# pwd
/root
~~~
-为上一次使用目录
~~~
[root@iZ23olnit2eZ ~]# cd –
/tmp
~~~
{}在大括号中加上带逗号的列表,可展开为相应目录
~~~
[root@iZ23olnit2eZ tmp]# mkdir {a_{a,b},{c,d}_e}
[root@iZ23olnit2eZ tmp]# ls
a_a a_b c_e d_e
~~~
## 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
~~~
### A.创建/tmp目录下的:a_c, a_d, b_c, b_d
~~~
[root@iZ23olnit2eZ tmp]# ls
[root@iZ23olnit2eZ tmp]# mkdir {a_{c,d},b_{c,d}}
[root@iZ23olnit2eZ tmp]# ls
a_c a_d b_c b_d
~~~
### B.按图示创建/tmp/mylinux目录下的目录
~~~
[root@iZ23olnit2eZ 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@iZ23olnit2eZ 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
~~~
## 4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
### A.查看文件元数据命令 stat (大致信息可通过ls -l(或者ll)查询)
~~~
[root@iZ23olnit2eZ tmp]# stat a_c
File: `a_c' #文件名
Size: 4096 #文件大小 Blocks: 8 #占用块个数 IO Block: 4096 #占用块大小 directory #文件类型
Device: ca01h/51713d #存储设备 Inode: 538986 #索引节点 Links: 2 #链接数
Access: (0755/drwxr-xr-x) #权限 Uid: ( 0/ root) #属主 Gid: ( 0/ root) #属组
Access: 2016-07-17 18:26:43.827784089 +0800 #访问时间,读取文件时间,atime
Modify: 2016-07-17 18:26:43.827784089 +0800 #修改时间,内容(数据)改变,mtime
Change: 2016-07-17 18:26:43.827784089 +0800 #改变时间,元数据改变,ctime
~~~
### B.修改文件时间戳 touch
~~~
touch -a #修改atime
touch -m #修改mtime
修改atime或者mtime都会导致ctime的改变
~~~
## 5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
### A.定义别名 alias
~~~
[root@iZ23olnit2eZ tmp]# alias cls='clear'
[root@iZ23olnit2eZ tmp]# type cls
cls is aliased to `clear'
~~~
### B.引用另一命令执行结果 |(管道)
~~~
[root@iZ23olnit2eZ tmp]# b=2|echo $b-1
-1
~~~
## 6、显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。
~~~
find /var -name "1*[0-9]?*[[:lower:]]"
~~~
## 7、显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录
~~~
find /var -name "[0-9]?*[^0-9]"
~~~
## 8、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录
~~~
find /var -name "[^[:alpha:]][[:alpha:]]*"
~~~
##9、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22
~~~
[root@iZ23olnit2eZ ~]# touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`
[root@iZ23olnit2eZ ~]# ls /tmp
tfile-2016-07-17-19-16-24
~~~
##10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中
~~~
[root@iZ23olnit2eZ ~]# mkdir /tmp/mytest{1,2,3}
[root@iZ23olnit2eZ ~]# ls /tmp/mytest{1,2,3}
/tmp/mytest1:
/tmp/mytest2:
/tmp/mytest3:
[root@iZ23olnit2eZ ~]# cp -r `find /etc -name "p*[^0-9]"` /tmp/mytest1/
cp: cannot overwrite non-directory `/tmp/mytest1/profile' with directory `/etc/lvm/profile'
cp: overwrite `/tmp/mytest1/passwd'? y
cp: will not overwrite just-created `/tmp/mytest1/passwd' with `/etc/pam.d/passwd'
cp: overwrite `/tmp/mytest1/poweroff'? y
cp: will not overwrite just-created `/tmp/mytest1/poweroff' with `/etc/pam.d/poweroff'
cp: overwrite `/tmp/mytest1/prelink'? y
cp: will not overwrite just-created `/tmp/mytest1/prelink' with `/etc/cron.daily/prelink'
cp: overwrite `/tmp/mytest1/psacct'? y
cp: will not overwrite just-created `/tmp/mytest1/psacct' with `/etc/rc.d/init.d/psacct'
cp: cannot overwrite non-directory `/tmp/mytest1/postfix' with directory `/etc/postfix'
[root@iZ23olnit2eZ ~]# ls /tmp/mytest{1,2,3}
/tmp/mytest1:
pads.pp pem policy.kern preprobe
pam.d permissivedomains.pp policykit.pp printcap
pam_env.conf phar.ini popt.d print_event.conf
pango php.conf portmap.pp private
pango.modules php.d portreserve.pp privoxy.pp
pangox.aliases php.ini postfix procmail.pp
passenger.pp php.ini.zabbixbak postfix.pp profile
passwd pinforc postgresql.pp profile.d
passwd- pingd.pp postgrey.pp profiles
password piranha.pp post_report.xml protected.d
password-auth pkcs11.txt power.conf protocols
password-auth-ac pkcsslotd.pp power.d psacct
path pki poweroff psad.pp
pcmcia pluginconf.d power.sh ptchown.pp
pcp.pp plugins ppp publicfile.pp
pcscd.pp plugins.d ppp.pp pulseaudio.pp
pdo.ini plymouth prefdm puppet.pp
pdo_mysql.ini plymouthd.conf prefdm.conf pw
pdo_odbc.ini plymouthd.pp prefixes python.conf
pdo_sqlite.ini plymouth-shutdown.conf prelink python_event.conf
pear pm prelink.cache pyzor.pp
pear.conf pm-utils-hd-apm-restore.conf prelink.conf
peers podsleuth.pp prelink.conf.d
pegasus.pp policy prelude.pp
/tmp/mytest2:
/tmp/mytest3:
~~~
##11、复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中
~~~
[root@iZ23olnit2eZ ~]# yes|cp -r /etc/*\.d /tmp/mytest2
[root@iZ23olnit2eZ ~]# ls /tmp/mytest2
bash_completion.d init.d monit.d rc0.d rc6.d sudoers.d
chkconfig.d ld.so.conf.d pam.d rc1.d rc.d xinetd.d
cron.d logrotate.d php.d rc2.d rsyslog.d yum.repos.d
depmod.d lsb-release.d popt.d rc3.d rwtab.d
dracut.conf.d makedev.d prelink.conf.d rc4.d setuptool.d
event.d modprobe.d profile.d rc5.d statetab.d
~~~
##12、复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录中。
~~~
[root@iZ23olnit2eZ ~]# yes|cp -r /etc/[l,m,n]*\.conf /tmp/mytest3
[root@iZ23olnit2eZ ~]# ls /tmp/mytest3
ld.so.conf libuser.conf mke2fs.conf nscd.conf ntp.conf
libaudit.conf logrotate.conf monit.conf nsswitch.conf
~~~
原创文章,作者:N21_郁蓝,如若转载,请注明出处:http://www.178linux.com/19964
评论列表(1条)
写的很好,排版还可以在改进一下,第9个有没有更简单的命令,加油