3.文件管理 实验手册

3.文件管理 实验手册

实验目的

掌握创建和查看文件、复制、转移和删除文件

前提准备

linux系统,centos6系统,centos7或ubuntu

实验步骤

  • -:表示普通文件
  • d:表示目录文件
  • c:表示字符设备文件
  • b:表示块设备文件
  • l:表示软链接文件
  • p:表示管道文件
  • s:表示套接字文件
【例1】查看文件类型
[root@centos7 ~]# ll
总用量 24
-rw-r--r--. 1 root root   6 7月  23 09:17 1.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 2.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 3.txt
drwxr-xr-x. 2 root root   6 7月  20 20:04 download
drwxr-xr-x. 2 root root  49 7月  21 19:06 Downloads
-rw-r--r--. 1 root root  22 7月  24 14:51 f1
-rw-r--r--. 1 root root  21 7月  24 14:52 f2
-rw-r--r--. 1 root root  13 7月  20 20:27 hello2.txt
-rw-r--r--. 1 root root  15 7月  22 11:20 hello.txt
-rw-r--r--. 1 root root   3 7月  22 17:59 win.txt
drwxr-xr-x. 2 root root 162 7月  24 15:36 模板

显示结果中,第一个位置的符号"-"就代表了文件类型为普通文件

1、pwd命令:显示当前shell的工作目录
【例2】显示当前shell的工作目录
[root@centos7 ~]# pwd
/root
2、basename命令:取路径基名
【例3】获取/etc/sysconfig/的基名
[root@centos7 ~]# basename /etc/sysconfig/
sysconfig
3、dirname命令:取路径名
【例4】取/etc/sysconfig路径名
[root@centos7 ~]# dirname /etc/sysconfig/
/etc
4、cd命令:切换目录
【例5】切换到用户家目录
[root@centos7 sysconfig]# cd
[root@centos7 ~]# pwd
/root
或:
[root@centos7 sysconfig]# cd ~
[root@centos7 ~]# pwd
/root
【例6】切换到父目录
[root@centos7 sysconfig]# cd ..
[root@centos7 etc]# 
【例7】切换到/etc/sysconfig目录下
[root@centos7 ~]# cd /etc/sysconfig/
[root@centos7 sysconfig]# 
【例8】切换到上一次所在的目录
[root@centos7 sysconfig]# cd -
/root
[root@centos7 ~]#
5、ls命令:列出目录的内容

选项:

​ -a:包含隐藏文件

​ -l:显示额外信息

​ -R:目录递归显示

​ -1:文件分行显示,一行显示一个

【例9】显示当前目录下所有文件
[root@centos7 ~]# ls -a
.   1.txt  3.txt          .bash_logout   .bashrc  .config  .dbus     Downloads  f1  hello2.txt  .history       .lesshst  .mozilla  .rnd  .tcshrc   win.txt
..  2.txt  .bash_history  .bash_profile  .cache   .cshrc   download  .esd_auth  f2  hello.txt   .ICEauthority  .local    .pki      .ssh  .viminfo  模板
【例10】显示目录内容外的额外信息
[root@centos7 ~]# ls -l
总用量 24
-rw-r--r--. 1 root root   6 7月  23 09:17 1.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 2.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 3.txt
drwxr-xr-x. 2 root root   6 7月  20 20:04 download
drwxr-xr-x. 2 root root  49 7月  21 19:06 Downloads
-rw-r--r--. 1 root root  22 7月  24 14:51 f1
-rw-r--r--. 1 root root  21 7月  24 14:52 f2
-rw-r--r--. 1 root root  13 7月  20 20:27 hello2.txt
-rw-r--r--. 1 root root  15 7月  22 11:20 hello.txt
-rw-r--r--. 1 root root   3 7月  22 17:59 win.txt
drwxr-xr-x. 2 root root 162 7月  24 15:36 模板
[root@centos7 ~]# ll
总用量 24
-rw-r--r--. 1 root root   6 7月  23 09:17 1.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 2.txt
-rw-r--r--. 1 root root   0 7月  20 20:04 3.txt
drwxr-xr-x. 2 root root   6 7月  20 20:04 download
drwxr-xr-x. 2 root root  49 7月  21 19:06 Downloads
-rw-r--r--. 1 root root  22 7月  24 14:51 f1
-rw-r--r--. 1 root root  21 7月  24 14:52 f2
-rw-r--r--. 1 root root  13 7月  20 20:27 hello2.txt
-rw-r--r--. 1 root root  15 7月  22 11:20 hello.txt
-rw-r--r--. 1 root root   3 7月  22 17:59 win.txt
drwxr-xr-x. 2 root root 162 7月  24 15:36 模板
【例11】递归显示目录内容
[root@centos7 ~]# ls -R
.:
1.txt  2.txt  3.txt  download  Downloads  f1  f2  hello2.txt  hello.txt  win.txt  模板

./download:

./Downloads:
wps-office-11.1.0.8722-1.x86_64.rpm

./模板:
DOCX 文档.docx  DOC 文档.doc  PPTX 演示文稿.pptx  PPT 演示文稿.ppt  XLSX 工作表.xlsx  XLS 工作表.xls
【例12】组合应用
[root@centos7 data]# ls -al
总用量 7358376
drwxr-xr-x.   6 root root       4096 7月  24 15:53 .
dr-xr-xr-x.  20 root root        262 7月  22 17:37 ..
-rw-r--r--.   1 root root         38 7月  24 11:31 1.txt~
drwxr-xr-x. 144 root root       8192 7月  23 10:07 2019-07-23
-rw-r--r--.   1 root root          0 7月  22 15:26 7.txtx
-rw-r--r--.   1 root root          8 7月  20 19:51 a
-rw-r--r--.   1 root root         11 7月  24 14:23 all.log
-rw-r--r--.   1 root root         16 7月  24 15:51 a.log
-rw-r--r--.   1 root root          0 7月  24 14:02 f1.txt
-rw-r--r--.   1 root root          0 7月  24 14:04 fd
-rw-r--r--.   1 root root         21 7月  20 17:05 fi.log
-rw-r--r--.   1 root root         79 7月  24 16:05 issue
-rw-r--r--.   1 root root         79 7月  24 09:15 issue2
-rw-r--r--.   1 root root         50 7月  24 14:09 log.log
-rw-r--r--.   1 root root          0 7月  24 14:06 ls.log
-rw-r--r--.   1 root root          0 7月  24 15:12 motd
-rw-r--r--.   1 root root         51 7月  24 15:53 my.cnf
-rw-r--r--.   1 root root       2426 7月  24 16:36 passwd
-rw-r--r--.   1 root root 7534911488 7月  24 12:00 .passwd.swp
dr-xr-x---.  12 root root       4096 7月  23 10:12 rootdir
-rw-r--r--.   1 root root          0 7月  24 14:13 stderr.log
-rw-r--r--.   1 root root          0 7月  24 14:13 stdout.log
drwxr-xr-x.   7 root root       4096 7月  22 14:12 sysconfig-bak
drwxr-xr-x.   2 root root          6 7月  22 20:49 .trash
6、stat命令:查看文件状态
【例13】查看test.txt文件的状态,注意三个时间戳
[root@centos7 ~]# stat 1.txt 
  文件:"1.txt"
  大小:6          块:8          IO 块:4096   普通文件
设备:802h/2050d   Inode:201518774   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2019-07-24 13:59:17.238016239 +0800
最近更改:2019-07-23 09:17:50.724956696 +0800
最近改动:2019-07-23 09:17:50.728956695 +0800
创建时间:-
7、touch命令:创建空文件和刷新时间

touch一个已经存在的文件,不会改变文件内容,但会改变文件的时间,比如mtime等

【例14】创建空文件test.sh
[root@centos7 ~]# touch test.sh
[root@centos7 ~]# ll test.sh
-rw-r--r--. 1 root root 0 7月  24 20:04 test.sh
8、cp命令
【例15】把/etc/httpd/conf/httpd.conf和/etc/my.cnf两个文件拷贝到当前目录
[root@centos7 ~]# cp /etc/httpd/conf/httpd.conf /etc/my.cnf .
[root@centos7 ~]# ls -l !*
ls -l /etc/httpd/conf/httpd.conf /etc/my.cnf .
-rw-r--r--.  1 root root 11753 4月  24 21:44 /etc/httpd/conf/httpd.conf
-rw-r--r--.  1 root root   570 8月  16 2018 /etc/my.cnf
【例16】把/etc/nginx目录及其下面所有文件和子目录拷贝到当前目录
[root@centos7 ~]# cp /etc/nginx . -R
[root@centos7 ~]# ll -d nginx
drwxr-xr-x. 3 root root 177 7月  24 20:17 nginx
【例17】复制httpd.conf文件并重命名为httpd.conf.bak
[root@centos7 ~]# cp /etc/httpd/conf/httpd.conf{,.bak}
[root@centos7 ~]# ll /etc/httpd/conf
总用量 40
-rw-r--r--. 1 root root 11753 4月  24 21:44 httpd.conf
-rw-r--r--. 1 root root 11753 7月  24 20:20 httpd.conf.bak
-rw-r--r--. 1 root root 13077 4月  24 21:46 magic
【例18】复制/etc目录下面所有文件及其子目录并重命名为/etc_bak
[root@centos7 ~]# cp /etc ./etc_bak -a
[root@centos7 ~]# ll -d etc_bak/
drwxr-xr-x. 145 root root 8192 7月  24 20:16 etc_bak/
9、mv命令:移动文件或目录。注意:移动目录时,无需添加-R递归选项,要与cp命令区别
【例19】把当前目录下nginx目录重命名为nginx_bak
[root@centos7 ~]# mv nginx/ nginx_bak
[root@centos7 ~]# ll nginx_bak/ -d
drwxr-xr-x. 3 root root 177 7月  24 20:17 nginx_bak/
【例20】把httpd.conf文件移动到/tmp目录下面
[root@centos7 ~]# mv httpd.conf /tmp
[root@centos7 ~]# ll /tmp/httpd.conf 
-rw-r--r--. 1 root root 11753 7月  24 20:12 /tmp/httpd.conf
10、rm命令:删除文件或目录
【例21】删除当前目录下所有文件
[root@centos7 tmp]# rm -rf ./*
[root@centos7 tmp]# ll
总用量 0
11、mkdir命令
【例22】创建目录a,其下包含b和c两目录,且b和c目录下面都有一个目录d
[root@centos7 tmp]# mkdir a/{b,c}/d -p
[root@centos7 tmp]# tree -C a
a
|-- b
|   `-- d
`-- c
    `-- d

4 directories, 0 files
12、tree命令:显示目录树
【例23】显示a目录的目录树
[root@centos7 tmp]# mkdir a/{b,c}/d -p
[root@centos7 tmp]# tree -C a
a
|-- b
|   `-- d
`-- c
    `-- d

4 directories, 0 files
【例24】查看/usr/local目录树,但仅查看2级的目录深度
[root@centos7 tmp]# tree /usr/local/ -L 2
/usr/local/
|-- bin
|-- etc
|-- games
|-- include
|-- lib
|-- lib64
|-- libexec
|-- sbin
|-- share
|   |-- applications
|   |-- info
|   `-- man
`-- src

13 directories, 0 files
13、ln命令:创建链接文件
【例25】把/usr/sbin/apachectl文件在当前目录下创建软链接为apachectl
[root@centos7 tmp]# ll apachectl 
lrwxrwxrwx. 1 root root 15 7月  24 20:34 apachectl -> /sbin/apachectl

猜你喜欢

转载自blog.51cto.com/14012942/2423255