前言
首先,必须明确的一点概念是:Linux系统一切皆文件。常规意义上的文件是文件,目录是文件,显示器是文件,网卡是文件,总之在Linux中,所有东西都是文件。
当然,这个概念也是为了方便理解Linux的文件管理权限等等,实际使用过程中,影响不会很大。
本篇主要讲述的是最简单的文件操作,如:创建文件(文件夹)、复制、移动、删除等等,也是属于基础命令,关于文件的定义,文件的属性之类的会有专门的文章来介绍。
mkdir
作用:创建目录 (make directories)
用法:mkdir [OPTION]... DIRECTORY...
选项:
- -p 递归,连续创建目录
- -v 显示创建目录的结果或过程
演示:
[root@gaoqichen ~]# mkdir /tmp/test/gao/qi/chen/ -p
[root@gaoqichen ~]# tree /tmp/test
/tmp/test
├── gao
│ └── qi
│ └── chen
└── hosts
3 directories, 1 file
PS: tree命令为显示目录树结构的命令,CentOS默认没有安装,使用yum -y install tree
命令安装。
touch
作用:创建文件或更新时间戳 (change file timestamps)
用法:touch [OPTION]... FILE...
PS: 仅创建文件不需要输入选项!如果文件不存在则创建文件,如果文件存在,则更新时间戳。更新时间戳不是特别常用,了解即可。
选项(仅修改时间戳时使用):
- -a 只改变访问时间
- -m 只改变修改时间
演示:
[root@gaoqichen ~]# touch hello.txt
[root@gaoqichen ~]# ls -lh --time-style=long-iso
total 4.0K
......
-rw-r--r--. 1 root root 0 2018-10-28 00:51 hello.txt
file
作用:查看文件类型(determine file type),同时也可以查看软链接的目标文件
用法:file FILE...
选项:
- -b 不输出文件名称,只显示文件格式以及编码
- –i 输出mime类型的字符串
文件类型:
- ASCII text 文本
- directory 目录
- dynamically linked (uses shared libs) 二进制文件
- shell script text executable 可执行脚本
- character special 字符设备
- block special 块设备文件
- symbolic link 连接文件
演示:
[root@gaoqichen ~]# file /etc/hosts
/etc/hosts: ASCII text
[root@gaoqichen ~]# file -b /etc/hosts
ASCII text
[root@gaoqichen ~]# file -i /etc/hosts
/etc/hosts: text/plain; charset=us-ascii
[root@gaoqichen ~]# file /etc/rc.d
/etc/rc.d: directory
[root@gaoqichen ~]# file /etc/rc.d/rc.local
/etc/rc.d/rc.local: Bourne-Again shell script, ASCII text executable
[root@gaoqichen ~]# file /bin/sh
/bin/sh: symbolic link to 'bash'
[root@gaoqichen ~]# which cp
alias cp='cp -i'
/usr/bin/cp
# which 命令是查询该命令的真实物理路径
[root@gaoqichen ~]# file /usr/bin/cp
/usr/bin/cp: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=47c2259e084c64fb00ec01bda8a57c005e3516d5, stripped
stat
作用:查看文件属性(display file or file system status)
用法:stat [OPTION]... FILE...
选项:
-c 转换格式输出显示文件权限
- %a 以八进制显示权限
- %A 以-rwx显示权限
PS: -c选项仅输出文件权限,文件权限后面会详细写文章。
演示:
[root@gaoqichen ~]# stat /etc/hosts
File: ‘/etc/hosts’
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 4790360 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:net_conf_t:s0
Access: 2018-10-27 23:38:59.535818919 +0800
Modify: 2018-10-27 23:38:50.296139124 +0800
Change: 2018-10-27 23:38:50.296139124 +0800
Birth: -
[root@gaoqichen ~]# stat -c %a /etc/hosts
644
[root@gaoqichen ~]# stat -c %A /etc/hosts
-rw-r--r--
cp
作用:复制文件或目录到指定目录 (copy files and directories)
用法:cp [OPTION]... SOURCE... DIRECTORY
选项:
- -a 递归复制并保留所有属性,包括链接,
-a
实际上等于-dpr
- -r 递归,复制目录需要加
- -f 强行复制文件或目录,不论目标文件或目录是否已存在
- -i 覆盖既有文件之前先询问用户,默认属性
- -v 详细显示命令执行的操作
-----以下了解即可----- - -d 复制链接属性
- -p 保留源文件或目录的属性
- -u 只有源文件比目标文件更新或目标文件不存在才复制
- -b 覆盖已存在的文件目标前将目标文件备份
演示:
复制hosts文件到/tmp文件夹
[root@gaoqichen ~]# cp /etc/hosts /tmp
[root@gaoqichen ~]# ls -lh /tmp
total 8.0K
-rw-r--r--. 1 root root 158 Oct 17 19:17 hosts
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
将hosts文件复制到/tmp目录下并改名为hosts1
[root@gaoqichen ~]# cp /etc/hosts /tmp/hosts1
[root@gaoqichen ~]# ls -lh /tmp
total 12K
-rw-r--r--. 1 root root 158 Oct 17 19:17 hosts
-rw-r--r--. 1 root root 158 Oct 17 19:19 hosts1
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
备份hosts文件,起名为hosts.bak
[root@gaoqichen ~]# cp /etc/hosts{,.bak}
[root@gaoqichen ~]# ls -lh /etc/hosts /etc/hosts.bak
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
-rw-r--r--. 1 root root 158 Oct 17 19:22 /etc/hosts.bak
mv
作用:剪切或重命名 (move (rename) files)
用法:mv [OPTION]... SOURCE... DIRECTORY
选项有很多,但是基本没有常用的。
演示:
将/tmp/hosts移动到/tmp/test/中
[root@gaoqichen ~]# mkdir /tmp/test
[root@gaoqichen ~]# mv /tmp/hosts /tmp/test/
[root@gaoqichen ~]# ls -lh /tmp/test
total 4.0K
-rw-r--r--. 1 root root 158 Oct 17 19:17 hosts
将/tmp/hosts1改名为hosts
[root@gaoqichen ~]# mv /tmp/hosts1 /tmp/hosts
[root@gaoqichen ~]# ls -lh /tmp
total 8.0K
-rw-r--r--. 1 root root 158 Oct 17 19:19 hosts
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
drwxr-xr-x. 2 root root 19 Oct 17 19:32 test
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
rm
作用:删除文件或目录 (remove files or directories)
rm删除命令无法撤销,请区别windows的删除,Linux没有回收站……
强烈建议在生产环境下不要使用该命令!可以将要删除的文件使用mv命令移动到/tmp目录下以替代删除。如果非要删除,建议cd进入当前目录,并配合find命令查找删除。
用法:rm [OPTION]... FILE...
选项:
- -f 强制删除
- -r 删除目录
- -i 删除前提示
PS: 删除目录同样可以使用rmdir命令实现,但记一个命令,还是记一个已知命令的一个选项,不难选吧!所以rmdir这个命令就不介绍了。
演示:
[root@gaoqichen ~]# cd /tmp
[root@gaoqichen tmp]# ls -lh
total 8.0K
-rw-r--r--. 1 root root 158 Oct 17 19:19 hosts
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
drwxr-xr-x. 3 root root 30 Oct 17 19:36 test
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
[root@gaoqichen tmp]# rm -f hosts
[root@gaoqichen tmp]# ls -lh
total 4.0K
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
drwxr-xr-x. 3 root root 30 Oct 17 19:36 test
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
[root@gaoqichen tmp]# rm -fr test
[root@gaoqichen tmp]# ls -lh
total 4.0K
-rwx------. 1 root root 836 Oct 16 20:32 ks-script-SRC9oI
-rw-------. 1 root root 0 Oct 16 20:26 yum.log
cat
作用:输出显示文件内容 (concatenate files and print on the standard output)
用法:cat [OPTION]... [FILE]...
选项:
- -n 对输出行编号
- -b 对输出非空行编号
- -s 连续空白行替换成一行空白行
- -E 在每一行结尾处显示$
- -T 用"^I"显示tab锁进
演示:
输出显示测试文本
[root@gaoqichen ~]# cat /tmp/test.txt
Welcome to my blog. http://gaoqichen.com
My name is gaoqichen
If you like my blog, plz support me.
Bye! boys and girls.
-n
显示行号
[root@gaoqichen ~]# cat -n /tmp/test.txt
1 Welcome to my blog. http://gaoqichen.com
2 My name is gaoqichen
3
4 If you like my blog, plz support me.
5
6
7
8 Bye! boys and girls.
-b
只给非空行加行号
[root@gaoqichen ~]# cat -b /tmp/test.txt
1 Welcome to my blog. http://gaoqichen.com
2 My name is gaoqichen
3 If you like my blog, plz support me.
4 Bye! boys and girls.
-s
将连续的空行合并为一行
[root@gaoqichen ~]# cat -s /tmp/test.txt
Welcome to my blog. http://gaoqichen.com
My name is gaoqichen
If you like my blog, plz support me.
Bye! boys and girls.
-E
在每一行结尾处显示$
[root@gaoqichen ~]# cat -E /tmp/test.txt
Welcome to my blog. http://gaoqichen.com$
My name is gaoqichen$
$
If you like my blog, plz support me.$
$
$
$
Bye! boys and girls.$
-T
用“^I”显示tab锁进
[root@gaoqichen ~]# cat -T /tmp/test.txt
Welcome to my blog. http://gaoqichen.com
My name is^Igaoqichen
If you like my blog, plz support me.
Bye! boys and girls.
vi/vim
作用:文本编辑器 (Vi IMproved, a programmers text editor),修改编辑文件的。
vi和vim为两个编辑器,可以理解为vim为vi的升级版,主要体现在Highlight(高亮)上。
最小化安装CentOS默认没有安装vim,使用yum -y install vim
进行安装。
用法:vim [OPTION] [FILE]...
PS: 如果文件不存在,使用vi/vim命令则在保存后创建该文件。
其他用法:
- vi FILE
打开或新建文件,光标位于第一行 - vi FILE +数字
打开文件,光标位于第数字行 - vi FILE +
打开文件,光标位于最后一行 - vi FILE +/关键词
打开文件,光标位于关键字出现的第一次所在行
说实话vi编辑器真心好难用,但毕竟自带的编辑器,一定要掌握啊……
vi的常见模式:
- 普通模式(进入vi后的默认模式,也可以叫阅读模式,只能看不能编辑。移动光标也仅对其他模式提供帮助。)
特点是最后一行显示"filename" xL xC(文件名、几行、多少字符)或最后一行为空 - 块选择模式(移动光标对文本进行选择,并删除、复制粘贴等动作。按v进入,按ESC退出)
特点是最后一行显示-- VISUAL -- - 编辑模式(键盘输入,按下字符只表示输入字符本身,按a、i或o进入,ESC退出)
特点是最后一行显示-- INSERT -- - 替换模式(输入字符替换掉光标位置的字符,并向后移动光标。按R进入,ESC退出)
特点就是最后一行显示-- REPLACE -- - 命令模式(可以理解为对整个文件及编辑器的操作,按英文半角:进入,ESC退出)
特点就是最后一行显示:
快捷键:
另外一篇文章将详细介绍vi/vim的快捷键,传送门:Linux中vi/vim快捷键整理
结语
Linux一切皆文件!也因为一切皆文件,所以对文件进行的操作非常重要。废话不多说了,记熟!用熟!