Linux 基础 - 文件管理
Linux 文件管理
文件和目录管理
每个目录都会有两个特殊目录 .
和 ..
分别代表当前目录和上级目录
查看当前目录:pwd
[root@localhost ~]# pwd
/root
进入目录:cd
[root@localhost ~]# cd /
[root@localhost /]# pwd
/
[root@localhost /]# cd /home
[root@localhost home]# pwd
/home
[root@localhost home]# cd ../usr/local
[root@localhost local]# pwd
/usr/local
列出目录内容:ls
[root@localhost ~]# ls
anaconda-ks.cfg
列出指定目录内容
[root@localhost ~]# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
-l
显示详细信息
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1228 9月 25 08:33 anaconda-ks.cfg
[root@localhost ~]# ls -l /
总用量 16
lrwxrwxrwx. 1 root root 7 9月 25 08:28 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 9月 25 08:34 boot
drwxr-xr-x. 19 root root 3100 9月 25 08:34 dev
drwxr-xr-x. 74 root root 8192 9月 25 08:34 etc
......(略去内容)......
创建目录:mkdir
[root@localhost ~]# mkdir dir1
[root@localhost ~]# ls
anaconda-ks.cfg dir1
-p
多级目录
[root@localhost ~]# mkdir -p dir2/dir3/dir4
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2
[root@localhost ~]# ls dir2
dir3
[root@localhost ~]# ls dir2/dir3
dir4
删除空目录:rmdir
[root@localhost ~]# ls dir2
dir3 file.txt
[root@localhost ~]# rmdir dir2
rmdir: 删除 "dir2" 失败: 目录非空
[root@localhost ~]# ls dir2/dir3
dir4
[root@localhost ~]# ls dir2/dir3/dir4
[root@localhost ~]# rmdir dir2/dir3/dir4
[root@localhost ~]# ls dir2/dir3
[root@localhost ~]#
复制文件和目录:cp
[root@localhost ~]# ls dir2
dir3 file.txt
[root@localhost ~]# ls dir2/dir3
[root@localhost ~]# cp dir2/file.txt dir2/dir3/file.txt
[root@localhost ~]# ls dir2
dir3 file.txt
[root@localhost ~]# ls dir2/dir3
file.txt
-r
复制目录
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2
[root@localhost ~]# ls dir1
[root@localhost ~]# cp -r dir2 dir1/dir2
[root@localhost ~]# ls
anaconda-ks.cfg dir1 dir2
[root@localhost ~]# ls dir1
dir2
创建文件:touch
[root@localhost dir1]# touch file.txt
[root@localhost dir1]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 25 14:29 file.txt
删除文件:rm
[root@localhost dir1]# ls
file1.txt file.txt
[root@localhost dir1]# rm file1.txt
rm:是否删除普通空文件 "file1.txt"?y
[root@localhost dir1]# ls
file.txt
-r
删除目录
移动或重命名文件:mv
[root@localhost dir1]# ls
file.txt
[root@localhost dir1]# ls ../dir2
dir3
[root@localhost dir1]# mv file.txt ../dir2/file.txt
[root@localhost dir1]# ls
[root@localhost dir1]# ls ../dir2
dir3 file.txt
显示文件内容:cat
[root@localhost ~]# cat anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
......(略去内容)......
查看文件头:head
[root@localhost ~]# head -n 5 anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
查看前 5 行的内容
查看文件尾:tail
[root@localhost ~]# tail -n 5 anaconda-ks.cfg
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
查看文件后 5 行
文件和目录的权限
查看目录或文件的权限:ls -al
[root@localhost ~]# ls -al
总用量 32
dr-xr-x---. 4 root root 175 9月 25 10:55 .
dr-xr-xr-x. 17 root root 224 9月 25 08:32 ..
-rw-------. 1 root root 1228 9月 25 08:33 anaconda-ks.cfg
-rw-------. 1 root root 150 9月 25 08:48 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwxr-xr-x. 3 root root 18 9月 25 14:48 dir1
drwxr-xr-x. 3 root root 34 9月 25 14:36 dir2
-rw-------. 1 root root 41 9月 25 09:47 .lesshst
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
- 第一列:文件的类别和权限
- 第二列:连接数文件的连接数是 1,目录的连接数是该目录中包含其他目录的总个数+2
- 第三列:所有人
- 第四列:所有组
- 第五列:文件大小
- 第六列:创建或修改时间
- 第七列:文件名
第一个字符 | 含义 |
---|---|
d | 目录 |
- | 普通文件 |
l | 链接文件 |
b | 块文件 |
c | 字符文件 |
s | socket 文件 |
p | 管道文件 |
改变文件权限:chmod
作用 | 命令 |
---|---|
添加用户读权限 | chmod u+r file |
删除用户读权限 | chmod u-r file |
添加用户写权限 | chmod u+w file |
删除用户写权限 | chmod u-w file |
添加用户执行权限 | chmod u+x file |
删除用户执行权限 | chmod u-x file |
添加用户读写执行权限 | chmod u+rwx file |
删除用户读写执行权限 | chmod u-rwx file |
设置用户读写执行权限 | chmod u=rwx file |
如果要给用户组或者其他人添加或删除权限只需要将 u
改成 g
或者 o
即可
也可以用数组表示 r=4 w=2 x=1 权限 rwx 就是 7
chmod 754 file
用户(u):rwx,用户组(g):r-x,其他人(o):r–
-R
递归子文件
改变文件拥有者:chown
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 root root 0 9月 25 14:29 file.txt
[root@localhost dir2]# chown onlychina file.txt
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 onlychina root 0 9月 25 14:29 file.txt
:
改变拥有组
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 onlychina root 0 9月 25 14:29 file.txt
[root@localhost dir2]# chown :onlychina file.txt
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 onlychina onlychina 0 9月 25 14:29 file.txt
-R
递归子文件
改变文件拥有组:chgrp
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 onlychina onlychina 0 9月 25 14:29 file.txt
[root@localhost dir2]# chgrp onlychina5555 file.txt
[root@localhost dir2]# ls -l file.txt
-rw-r--r--. 1 onlychina onlychina5555 0 9月 25 14:29 file.txt
查看文件类型:file
[root@localhost dir2]# ll
总用量 0
drwxr-xr-x. 2 root root 22 9月 25 14:47 dir3
-rw-r--r--. 1 onlychina onlychina5555 0 9月 25 14:29 file.txt
[root@localhost dir2]# file file.txt
file.txt: empty
[root@localhost dir2]# file dir3
dir3: directory
查找文件
一般查找:find
[root@localhost dir2]# find / -name dir
/sys/fs/selinux/class/dir
/usr/bin/dir
/usr/share/info/dir
[root@localhost dir2]# find / -name dir*
/root/dir1/dir2/dir3
/root/dir2/dir3
数据库查找:locate
Linux 系统每天都会检索系统中所有的文件并保存在数据库中,所以 locate 比 find 更快,一般执行 locate 之前要执行 updatedb,检索并更新数据库信息
要是没有 locate 需要安装一下
[root@localhost dir2]# yum -y install mlocate
[root@localhost dir2]# updatedb
[root@localhost dir2]# locate dir*
/root/dir1/dir2/dir3
/root/dir1/dir2/dir3/file.txt
/root/dir2/dir3
/root/dir2/dir3/file.txt
查找执行文件:which/whereis
[root@localhost ~]# which passwd
/usr/bin/passwd
[root@localhost ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
whereis 除了找出可执行文件的绝对路径 ,同时也找出了其二进制文件 以及相关 man 文件
文件压缩和打包
gzip/gunzip
[root@localhost dir2]# ls
dir3 file.txt
[root@localhost dir2]# gzip file.txt
[root@localhost dir2]# ls
dir3 file.txt.gz
[root@localhost dir2]# gunzip file.txt.gz
[root@localhost dir2]# ls
dir3 file.txt
tar
[root@localhost dir2]# ls
dir3 file.txt
[root@localhost dir2]# tar -zcvf dir.tgz dir3
dir3/
dir3/file.txt
[root@localhost dir2]# ls
dir3 dir.tgz file.txt
[root@localhost dir2]# tar -zxvf dir.tgz
dir3/
dir3/file.txt
[root@localhost dir2]# ls
dir3 dir.tgz file.txt
[root@localhost dir2]# mkdir tmp
[root@localhost dir2]# tar -zxvf dir.tgz -C tmp
dir3/
dir3/file.txt
[root@localhost dir2]# ls
dir3 dir.tgz file.txt tmp
[root@localhost dir2]# ls tmp
dir3
-z
使用 gzip-c
创建压缩文件-v
显示文件-f
使用文件名-x
解压-C
指定解压目录