第三章 ⽂件管理

135 阅读6分钟

@[TOC](第三章 ⽂件管理)

实验⼀:⽂件管理相关命令使⽤

实验⽬的

掌握创建和查看⽂件、复制、转移和删除⽂件、软和硬链接的区别等命令使⽤。

前提准备

linux系统,centos6、centos7或ubuntu。

实验步骤

先介绍下linux⽂件系统上的⽂件类型如下:

-:表示普通文件 d:表示目录文件 b:表示块设备文件 c:表示字符设备文件 l:表示软链接文件 p:表示管道文件 s:表示套接字文件

【例1】查看⽂件类型

[root@Magedu ~]# ll 
total 8
-rw-------. 1 root root 1865 Apr 25 14:33 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 May 8 15:23 Desktop
drwxr-xr-x 2 root root 6 May 8 15:23 Documents
drwxr-xr-x 2 root root 6 May 8 15:23 Downloads
-rw-r--r--. 1 root root 1913 Apr 25 14:35 initial-setup-ks.cfg
drwxr-xr-x 2 root root 6 May 8 15:23 Music
drwxr-xr-x 2 root root 6 May 8 15:23 Pictures
drwxr-xr-x 2 root root 6 May 8 15:23 Public
drwxr-xr-x 2 root root 6 May 8 15:23 Templates
drwxr-xr-x 2 root root 6 May 8 15:23 Videos

显⽰结果中,第⼀个位置的符号“-”就代表了⽂件类型为普通⽂件。

  1. pwd命令:显⽰当前shell的⼯作⽬录

【例2】显⽰当前⼯作⽬录

[root@Magedu network-scripts]# pwd
/etc/sysconfig/network-scripts
  1. basename命令:取路径基名

【例3】获取/etc/sysconfig/的基名

[root@Magedu sysconfig]# basename /etc/sysconfig/
sysconfig
  1. dirname命令:取路径名

【例4】取/etc/sysconfig/的路径名

[root@Magedu sysconfig]# dirname /etc/sysconfig/
/etc
  1. cd命令:切换⽬录

【例5】切换到⽤户家⽬录

[root@Magedu network-scripts]# cd
[root@Magedu ~]#
或:
[root@Magedu network-scripts]# cd ~
[root@Magedu ~]#

【例6】切换到⽗⽬录

[root@Magedu network-scripts]# cd ..
[root@Magedu sysconfig]# 

【例7】切换到/etc/sysconfig⽬录下

[root@Magedu ~]# cd /etc/sysconfig/
[root@Magedu sysconfig]# 

【例8】切换到上⼀次所在的⽬录

[root@Magedu sysconfig]# cd -
/root
[root@Magedu ~]#
  1. ls命令:列出⽬录的内容

选项:
-a:包含隐藏文件; -l:显示额外信息; -R:目录递归通过;
-1:文件分行显示;

【例9】显⽰当前⽬录下所有⽂件

[root@Magedu testdir]# ls -a
. .. test1.txt test.txt win.txt

【例10】显⽰⽬录内容的额外信息

[root@Magedu testdir]# ls -l
total 252
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt 6. 23
或:
[root@Magedu testdir]# ll
total 252
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt

【例11】递归显⽰⽬录内容

[root@Magedu ~]# ls -R
.:
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
./Desktop:
./Documents:
./Downloads:
./Music:
./Pictures:
./Public:
./Templates:
./Videos:

【例12】组合应⽤

[root@Magedu testdir]# ll -aR
.:
total 256
drwxr-xr-x 2 root root 54 May 23 09:00 .
dr-xr-x---. 10 root root 4096 May 28 07:07 ..
-rw-r--r-- 1 root root 0 May 23 09:00 test1.txt
-rw-r--r-- 1 root root 251734 May 23 04:15 test.txt
-rw-r--r-- 1 root root 9 May 23 04:06 win.txt
  1. stat命令:查看⽂件状态

【例13】查看test.txt⽂件的状态,注意三个时间戳

[root@Magedu ~]# stat initial-setup-ks.cfg 
File: ‘initial-setup-ks.cfg’
Size: 1913 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 201326698 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-25 14:35:53.224010813 +0800
Modify: 2019-04-25 14:35:53.225010813 +0800
Change: 2019-04-25 14:35:53.225010813 +0800
Birth: -
  1. touch命令:创建空⽂件和刷新时间

【例14】创建空⽂件test.sh

[root@Magedu ~]# touch test.sh
[root@Magedu ~]# ll test.sh 
-rw-r--r-- 1 root root 0 May 29 01:55 test.sh
  1. cp命令:复制⽂件和⽬录

【例15】把/etc/httpd/conf/httpd.conf⽂件和/etc/my.cnf⽂件拷贝到当前⽬录

[root@Magedu dir1]# cp /etc/httpd/conf/httpd.conf /etc/my.cnf ./
[root@Magedu dir1]# ll
total 16
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf

【例16】把/etc/nginx⽬录及其下⾯所有⽂件和⼦⽬录拷贝到当前⽬录

[root@Magedu dir1]# cp -R /etc/nginx/ ./
[root@Magedu dir1]# ll
total 20
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx

【例17】复制httpd.conf⽂件并重命名为

httpd.conf.bak
[root@Magedu dir1]# cp httpd.conf httpd.conf.bak
[root@Magedu dir1]# ll
total 32
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx

【例18】复制/etc⽬录下所有⽂件及其⼦⽬录到当前⽬录,并重命名为etc_bak

[root@Magedu dir1]# cp -R /etc ./etc_bak
[root@Magedu dir1]# ll
total 44
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx
  1. mv命令:移动⽂件或⽬录。注意:移动⽬录时,⽆需添加-R递归选项,要与cp命令区别。

【例19】把当前⽬录下nginx命令重命名为nginx_bak

[root@Magedu dir1]# mv nginx/ nginx_bak
[root@Magedu dir1]# ll
total 44
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:13 httpd.conf
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx_bak

【例20】把httpd.conf⽂件移动到/tmp⽬录下

[root@Magedu dir1]# mv httpd.conf /tmp
[root@Magedu dir1]# ll
total 32
drwxr-xr-x 143 root root 8192 May 29 02:32 etc_bak
-rw-r--r-- 1 root root 11766 May 29 02:19 httpd.conf.bak
-rw-r--r-- 1 root root 570 May 29 02:13 my.cnf
drwxr-xr-x 4 root root 4096 May 29 02:16 nginx_bak
  1. rm命令:删除⽂件或⽬录

【例21】删除当前⽬录下所有⽂件

[root@Magedu dir1]# rm -rf *
[root@Magedu dir1]# ll
total 0
  1. mkdir命令:创建⽬录

【例22】创建⽬录a,其下包含b和c两⽬录,且b和c⽬录下都有⼀个⽬录d

[root@Magedu ~]# mkdir -p a/{b,c}/d
  1. tree命令:显⽰⽬录树

【例23】显⽰a⽬录的⽬录树

[root@Magedu ~]# tree a
a
├── b
│ └── d
└── c
 └── d
4 directories, 0 files

【例24】查看/usr/local⽬录树,但仅查看2级的⽬录深度

[root@Magedu ~]# tree -L 2 /usr/local
/usr/local
├── bin
├── etc
├── games
├── include
├── lib
├── lib64
├── libexec
├── sbin
├── share
│ ├── applications
│ ├── info
│ └── man
└── src
13 directories, 0 files
  1. ln命令:创建链接⽂件

【例25】把 /usr/sbin/apachectl⽂件在当前⽬录下创建软连接⽂件为apachectl

[root@Magedu dir1]# ln -s /usr/sbin/apachectl apachectl
[root@Magedu dir1]# ll
total 0
lrwxrwxrwx 1 root root 19 May 29 02:57 apachectl -> /usr/sbin/apachectl