Linux 学习笔记02 - 文件操作
Linux文件系统将所有的磁盘都并入一个虚拟目录下,在使用新的存储媒体之前,需要将它放在虚拟目录下,这项工作称为 挂载(mounting)
常用文件操作
touch filename创建文件,如果存在则更新其修改时间mkdir directory创建目录mv source destination重命名某个文件source 也就是 移动某个文件cp source destination复制某个文件source到destination位置rm -ir [directory | filename]询问是否删除文件或者文件夹rm -rf directory强制删除文件夹,危险操作cat filename查看文件的所有内容,不适用大文件more filename分页查看文件内容,适用于大文件tail -10 filename查看文件的最后10行文字tail -f filename时时查看文件的最后输出内容,适用于程序打印日志的查看vi filename编辑文件,如果不存在则先创建再编辑
挂载
mount 命令
默认会输出当前系统上挂载的设备列表.
比如输出如下
/dev/disk1 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
输出的格式:
- 媒体的设备文件名称
- 媒体的挂载到虚拟目录的挂载点
- 文件系统类型
- 已挂载媒体的访问状态
要手动挂载媒体设备,需要以root用户身份登录,或是以root用户身份运行sudo命令.
mount -t type device directory
type参数指定了磁盘被格式化的文件系统类型
通常为:
- vfat : Windows长文件系统
- ntfs : Windows NT 、XP、Visa以及Windows 7 中广泛使用的高级文件系统
- iso9660 : 标准的CD-ROM文件系统
比如手动将U盘/dev/sdb1挂载到/media/disk下面:
mount -t vfat /dev/sdb1 /media/disk
umount 命令
卸载设备
umount [directory | device]
umount 命令支持通过设备名或者是挂载点来指定卸载设备. 不过要注意的是 如果有任何程序正在使用该设备,则会提示设备繁忙,卸载不成功, 需要退出该设备后,再进行卸载尝试.
lsof 命令 查看哪个进程在使用该设备
lsof /path/to/device/node 或者 lsof /path/to/mount/point
df
查看磁盘的空间
df 命令
输出如下:
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk1 234573824 201213432 32848392 86% 2389298 4292577981 0% /
devfs 380 380 0 100% 658 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home
输出的格式为:
- 设备的设备文件位置
- 能容纳
512-blocks多少个大小为512字节的块 - 已使用
512-blocks大小的块 - 还有多少
512-blocks可以使用 - 已用空间所占用的比例
- 设备挂载到了哪个挂载点上,如
/home主目录
du
du -ch 命令
查看具体某个目录的磁盘使用情况
8 ./SweetLove/SweetLove/Classes/Business/Base/Controller
72 ./SweetLove/SweetLove/Classes/Business/Base/Hub
24 ./SweetLove/SweetLove/Classes/Business/Base/Layer
40 ./SweetLove/SweetLove/Classes/Business/Base/TableView
全部命令:
usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m | -g] [-x] [-I mask] [file ...]
- -c : 显示所有已列出文件总的大小
- -h : 按照用户易读的格式输出
- -s : 显示每个输出参数的总计
sort
Usage: sort [OPTION]... [FILE]...
Write sorted concatenation of all FILE(s) to standard output.
Mandatory arguments to long options are mandatory for short options too.
Ordering options:
-b, --ignore-leading-blanks ignore leading blanks
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < `JAN' < ... < `DEC'
-n, --numeric-sort compare according to string numerical value
-r, --reverse reverse the result of comparisons
常用的几个
- -M 用三字符月份名 按照月份排序
- -n 按照字符串数值排序 (并不转化为浮点数)
- -k 排序从POS1位置开始,如果指定了POS2的话,则到POS2位置结束
常用的场景
1 查看文本并按照某个归责进行排序
比如按照','逗号分割, 第3个位置的数值升序排序
sort -t ',' -k 3 -n filename
2 查看目录中文件占用磁盘情况,并排序
当前目录的文件占用,并倒序排序,查找占用最大的文件
du -sh * | sort -nr
grep
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
grep可以跟 参数, 匹配字符,文件路径
比如查找server.log中匹配 error信息的输出
grep error server.log
默认用unix的正则匹配风格
压缩数据
Linux工具
bzip2压缩为 .bz2后缀的文件compress压缩为 .Zgzip压缩为 .gzzip压缩为 .zip
最流行的 gzip
gzip 压缩
gunzip 解压
gzcat 查看压缩过的文件内容
可以后面跟 正则匹配进行压缩
比如 压缩当前目录下的脚本文件
gzip *.sh
归档文件
tar(bsdtar): manipulate archive files
First option must be a mode specifier:
-c Create -r Add/Replace -t List -u Update -x Extract
Common Options:
-b # Use # 512-byte records per I/O block
-f <filename> Location of archive
-v Verbose
-w Interactive
Create: tar -c [options] [<file> | <dir> | @<archive> | -C <dir> ]
<file>, <dir> add these items to archive
-z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma
--format {ustar|pax|cpio|shar} Select archive format
--exclude <pattern> Skip files that match pattern
-C <dir> Change to <dir> before processing remaining files
@<archive> Add entries from <archive> to output
List: tar -t [options] [<patterns>]
<patterns> If specified, list only entries that match
Extract: tar -x [options] [<patterns>]
<patterns> If specified, extract only entries that match
-k Keep (don't overwrite) existing files
-m Don't restore modification times
-O Write entries to stdout, don't restore to disk
-p Restore permissions (including ACLs, owner, file flags)
归解档文件的常用命令
- -c 创建一个归档文件
- -v 在处理文件时显示文件
- -f 输出结果到文件或者设备file
- -t 列出已有的归档文件
- -x 从已有的归档文件中提出文件
常用的命令
创建一个 test.tar的归档文件, 将test/和test2/目录归档
tar -cvf test.tar test/ test2/
查看归档文件
tar -tf test.tar
解压提取文件
tar -xvf test.tar
注意 在下载开源软件时,经常遇到以.tgz结尾的文件, 这些是用gzip压缩后的tar文件,可以用 tar -zxvf filename.tgz来解压
Discussion