文件与目录管理

查看文件目录

查看文件目录:ls [-aAdfFhilnRSt] 目录名称

ls [-aAdfFhilnRSt] 目录名称

ls [--color={never,auto,always}] 目录名称

ls [--full-time] 目录名称

参数:

-a:全部文件连同隐藏文件(开头为.的文件)一起列出来(常用) 
-A:列出全部文件包括隐藏文件但是不包括. 和..这两个目录
-d:仅列出目录本身,而不是列出目录内的文件数据(常用)
-f:直接列出结果而不进行排序,(ls默认会根据文件名进行排序)
-F:根据文件、目录等信息给予附加数据结构,如:
	*:代表可执行文件,/:代表socket文件,|:代表FIFO文件
-h:讲文件容量以人们比较易读的方式列出来(如GB,MB)
-i:列出inode号码
-l:列出长串数据,包含文件的属性、权限等
-n:列出UID,GID,而非用户和用户组的名称
-r:将结果反向输出,如:文件名由小到大,反向则是由大到小
-R:连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来
-S:以文件容量大小排列
-t:按照时间排列
—color=never:不要依据文件特性显示颜色,即不显示
—color=always:显示颜色
—color=auto:让系统自动判断是否显示颜色
--full-time:以完整的时间输出:年月日时分
--time={atime,ctime}:输出访问时间或者改变权限属性时间而非内容更改时间
演示:

[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  lrzsz-0.12.20  lrzsz-0.12.20.tar.gz  test1  test2  test3  test4
[root@localhost ~]# ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  install.log         lrzsz-0.12.20         .mysql_history  .tcshrc  test2  test4
..  .bash_history    .bash_profile  .cshrc   install.log.syslog  lrzsz-0.12.20.tar.gz  .mysql_secret   test1    test3
[root@localhost ~]# ls -al
总用量 372
dr-xr-x---.  5 root root   4096 5月   3 16:41 .
dr-xr-xr-x. 25 root root   4096 5月   5 10:43 ..
-rw-------.  1 root root   1489 5月   3 12:54 anaconda-ks.cfg
-rw-------.  1 root root   1223 5月   5 09:57 .bash_history
-rw-r--r--.  1 root root     18 5月  20 2009 .bash_logout
-rw-r--r--.  1 root root    176 5月  20 2009 .bash_profile
-rw-r--r--.  1 root root    176 9月  23 2004 .bashrc
-rw-r--r--.  1 root root    100 9月  23 2004 .cshrc
-rw-r--r--.  1 root root  27338 5月   3 12:54 install.log
-rw-r--r--.  1 root root   7572 5月   3 12:53 install.log.syslog
drwxrwxrwx.  9  406 disk   4096 5月   3 13:36 lrzsz-0.12.20
-rwxrwxr-x.  1 root root 280938 12月 31 1998 lrzsz-0.12.20.tar.gz
-rw-------.  1 root root     47 5月   3 14:01 .mysql_history
-rw-------.  1 root root    104 5月   3 13:54 .mysql_secret
-rw-r--r--.  1 root root    129 12月  4 2004 .tcshrc
-rw-r--r--.  1 root root      0 5月   3 16:39 test1
drwxr-xr-x.  2 root root   4096 5月   3 16:39 test2
-rw-rw-r--.  1 root root      0 5月   3 16:41 test3
drwxrwxr-x.  2 root root   4096 5月   3 16:41 test4
[root@localhost ~]# ls -al --color=never 
总用量 372
dr-xr-x---.  5 root root   4096 5月   3 16:41 .
dr-xr-xr-x. 25 root root   4096 5月   5 10:43 ..
-rw-------.  1 root root   1489 5月   3 12:54 anaconda-ks.cfg
-rw-------.  1 root root   1223 5月   5 09:57 .bash_history
-rw-r--r--.  1 root root     18 5月  20 2009 .bash_logout
-rw-r--r--.  1 root root    176 5月  20 2009 .bash_profile
-rw-r--r--.  1 root root    176 9月  23 2004 .bashrc
-rw-r--r--.  1 root root    100 9月  23 2004 .cshrc
-rw-r--r--.  1 root root  27338 5月   3 12:54 install.log
-rw-r--r--.  1 root root   7572 5月   3 12:53 install.log.syslog
drwxrwxrwx.  9  406 disk   4096 5月   3 13:36 lrzsz-0.12.20
-rwxrwxr-x.  1 root root 280938 12月 31 1998 lrzsz-0.12.20.tar.gz
-rw-------.  1 root root     47 5月   3 14:01 .mysql_history
-rw-------.  1 root root    104 5月   3 13:54 .mysql_secret
-rw-r--r--.  1 root root    129 12月  4 2004 .tcshrc
-rw-r--r--.  1 root root      0 5月   3 16:39 test1
drwxr-xr-x.  2 root root   4096 5月   3 16:39 test2
-rw-rw-r--.  1 root root      0 5月   3 16:41 test3
drwxrwxr-x.  2 root root   4096 5月   3 16:41 test4
[root@localhost ~]# ls -al --full-time
总用量 372
dr-xr-x---.  5 root root   4096 2017-05-03 16:41:30.047068296 +0800 .
dr-xr-xr-x. 25 root root   4096 2017-05-05 10:43:22.152533350 +0800 ..
-rw-------.  1 root root   1489 2017-05-03 12:54:26.939999743 +0800 anaconda-ks.cfg
-rw-------.  1 root root   1223 2017-05-05 09:57:43.396850991 +0800 .bash_history
-rw-r--r--.  1 root root     18 2009-05-20 18:45:02.000000000 +0800 .bash_logout
-rw-r--r--.  1 root root    176 2009-05-20 18:45:02.000000000 +0800 .bash_profile
-rw-r--r--.  1 root root    176 2004-09-23 11:59:52.000000000 +0800 .bashrc
-rw-r--r--.  1 root root    100 2004-09-23 11:59:52.000000000 +0800 .cshrc
-rw-r--r--.  1 root root  27338 2017-05-03 12:54:21.802999746 +0800 install.log
-rw-r--r--.  1 root root   7572 2017-05-03 12:53:30.199999783 +0800 install.log.syslog
drwxrwxrwx.  9  406 disk   4096 2017-05-03 13:36:43.869330332 +0800 lrzsz-0.12.20
-rwxrwxr-x.  1 root root 280938 1998-12-31 00:31:54.000000000 +0800 lrzsz-0.12.20.tar.gz
-rw-------.  1 root root     47 2017-05-03 14:01:24.277496526 +0800 .mysql_history
-rw-------.  1 root root    104 2017-05-03 13:54:02.315496233 +0800 .mysql_secret
-rw-r--r--.  1 root root    129 2004-12-04 05:42:06.000000000 +0800 .tcshrc
-rw-r--r--.  1 root root      0 2017-05-03 16:39:24.335068758 +0800 test1
drwxr-xr-x.  2 root root   4096 2017-05-03 16:39:35.277068462 +0800 test2
-rw-rw-r--.  1 root root      0 2017-05-03 16:41:25.828067718 +0800 test3
drwxrwxr-x.  2 root root   4096 2017-05-03 16:41:30.047068296 +0800 test4

复制、删除、移动:cp,rm,mv

cp:复制文件或者目录

cp [-adfilorsu] source destination

参数:

-a:相当于pdr
-d:如果文件为链接文件则复制链接文件属性而非文件本身
-f:即force,强制,若目标文件已经存在且无法开启,则删除后再尝试一次
-i:若目标文件已经存在,则覆盖时会先询问操作的进行
-l:进行应连接的文件的创建,而非复制文件本身
-p:联通文件的属性一同复制过去,而非使用默认属性
-r:递归持续复制,用于目录的复制行为
-s:复制成为符号链接,即快捷方式
-u:若目标文件比源文件旧才更新目标文件
最后注意:若源文件为两个,则最后一个目的文件一定要是目录才行
演示:
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# ls
711  attrtest  bashrc  test2  testtouch  yum.log
[root@localhost tmp]# rm bashrc 
rm:是否删除普通文件 "bashrc"?y
[root@localhost tmp]# cd 
[root@localhost ~]# cp ~/.bashrc /tmp/bashrc
[root@localhost ~]# cp -i  ~/.bashrc /tmp/bashrc
cp:是否覆盖"/tmp/bashrc"? y
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# cp /var/log/wtmp .
[root@localhost tmp]# ls -l /var/lo
local/ lock/  log/   
[root@localhost tmp]# ls -l /var/log/wtmp 
-rw-rw-r--. 1 root utmp 24576 5月   5 10:44 /var/log/wtmp
[root@localhost tmp]# ls -l /var/log/wtmp wtmp 
-rw-rw-r--. 1 root utmp 24576 5月   5 10:44 /var/log/wtmp
-rw-r--r--. 1 root root 24576 5月   5 14:46 wtmp
[root@localhost tmp]# cp -a /var/log/wtmp wtmp_2
[root@localhost tmp]# ls -l /var/log/wtmp wtmp_2 
-rw-rw-r--. 1 root utmp 24576 5月   5 10:44 /var/log/wtmp
-rw-rw-r--. 1 root utmp 24576 5月   5 10:44 wtmp_2
[root@localhost tmp]# cp /etc/ /tmp/
cp: 略过目录"/etc/" ==》无法直接复制 
[root@localhost tmp]# cp -r /etc/ /tmp/
[root@localhost tmp]# ls /tmp
711  attrtest  bashrc  etc  test2  testtouch  wtmp  wtmp_2  yum.log
#范例四: 
[root@localhost tmp]# cp -s bashrc bashrc_slink
[root@localhost tmp]# cp -l bashrc bashrc_hlink
[root@localhost tmp]# ls -l bashrc*
-rw-r--r--. 2 root root 176 5月   5 14:45 bashrc
-rw-r--r--. 2 root root 176 5月   5 14:45 bashrc_hlink
lrwxrwxrwx. 1 root root   6 5月   5 15:19 bashrc_slink -> bashrc

rm:移除文件或目录

rm [-fir] 文件或目录

参数

-f:force:忽略不存在的文件,不会出现警告信息
-i:互动模式,在删除前询问是否操作
-r:递归删除,最长用在目录的删除-------------这是非常危险的参数

演示

范例一:讲cp到tmp中的bashrc删除掉
[root@localhost ~]# cd /tmp/  ----进入tmp

[root@localhost tmp]# ls  -----查看tmp中的文件,其中有bashrc
711  attrtest  bashrc  bashrc_hlink  bashrc_slink  bashrc_slink_1  bashrc_slink_2  etc  test2  testtouch  wtmp  wtmp_2  yum.log

[root@localhost tmp]# rm -i bashrc  ------删除,删除前询问是否操作
rm:是否删除普通文件 "bashrc"?y  ------y:是,n:否

[root@localhost tmp]# ls  ------再次查看tmp中的文件,其中已经没bashrc文件了
711  attrtest  bashrc_hlink  bashrc_slink  bashrc_slink_1  bashrc_slink_2  etc  test2  testtouch  wtmp  wtmp_2  yum.log

范例二:使用通配符,将tmp下面的所有以bashrc开头的文件都删除
[root@localhost tmp]# rm -i bashrc*
rm:是否删除普通文件 "bashrc_hlink"?y
rm:是否删除符号链接 "bashrc_slink"?y
rm:是否删除普通文件 "bashrc_slink_1"?y
rm:是否删除普通文件 "bashrc_slink_2"?y

[root@localhost tmp]# ls  再次查看tmp中的文件,其中已经没bashrc开头的文件了
711  attrtest  etc  test2  testtouch  wtmp  wtmp_2  yum.log

范例三:将tmp/etc/这个目录删掉
[root@localhost tmp]# rmdir etc/
rmdir: 删除 "etc/" 失败: 目录非空

[root@localhost tmp]# rm -r etc/
rm:是否进入目录"etc"? y
rm:是否删除普通文件 "etc/asound.conf"?y
………..    ------------需要一直按y删除,因为是root身份,默认加上了参数-i

范例四:删除一个带有“-”开头的文件
[root@localhost tmp]# touch ./-aaa~   ----------创建一个-aaa~文件
[root@localhost tmp]# ls
711  -aaa~  attrtest  etc  test2  testtouch  wtmp  wtmp_2  yum.log
[root@localhost tmp]# rm -aaa~      -----直接删除,发现无法删除:因为”-”是参数,所以系统给误判了
rm:无效选项 -- a
尝试使用"rm ./-aaa~"删除文件"-aaa~"。
请尝试执行"rm --help"来获取更多信息。
[root@localhost tmp]# rm ./-aaa~
rm:是否删除普通空文件 "./-aaa~"?y
[root@localhost tmp]# touch ./-aaa~
[root@localhost tmp]# rm -- -aaa~   -----也可以通过这种方式删除文件
rm:是否删除普通空文件 "-aaa~"?y

mv:移动文件、目录或更名
mv [-fiu] source destination

mv [options] source1 source2 source3 .... directory 

参数:

empty

演示:

范例一:复制一个文件,创建一个目录,将这个文件放到这个目录中
[root@localhost tmp]# cp ~/.bashrc bashrc
[root@localhost tmp]# mkdir mvtest
[root@localhost tmp]# mv bashrc mvtest/
[root@localhost tmp]# cd mvtest/
[root@localhost mvtest]# ls
Bashrc
范例二:给刚才的文件命名为mvtest2
[root@localhost mvtest]# cd ../
[root@localhost tmp]# mv mvtest/ mvtest2
[root@localhost tmp]# ls
711  attrtest  etc  mvtest2  test2  testtouch  wtmp  wtmp_2  yum.log
范例三:再创建两个文件全部移动到/tmp/mvtest2中
[root@localhost tmp]# cp ~/.bashrc bashrc1
[root@localhost tmp]# cp ~/.bashrc bashrc2
[root@localhost tmp]# mv bashrc1 bashrc2 mvtest2
[root@localhost tmp]# cd mvtest2/
[root@localhost mvtest2]# ls
bashrc  bashrc1  bashrc2

获取路径的文件名和目录名称

[root@localhost mvtest2]# basename /etc/sysconfig/network
Network   -------即获取最后的文件名
[root@localhost mvtest2]# dirname /etc/sysconfig/network
/etc/sysconfig   -------获取目录名



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值