入门 Linux 首先就要了解其文件系统和文件操作,Linux 的文件系统有其自身的结构特点,同时也有丰富的命令操作,下面就这两方面作简短总结。


文件操作

ls

format

1
ls [OPTION]... [FILE]...

option

-a

表示显示所有文件信息,包括

  • . : 表示当前工作目录
  • .. : 表示上级目录
  • .filename : 表示隐藏文件

    -i

    显示文件索引号,计算机识别文件的唯一标识是inode节点号,即文件索引号

    -l

    表示以长格式显示文件信息
    1
    2
    3
    4
    5
    [root@localhost test]# ls -l
    总计 16
    -rwxr-xr-x 1 root root 4729 11-29 20:18 a.out
    -rw-r--r-- 1 root root 6 11-29 20:13 a.txt
    -rw-r--r-- 1 root root 78 11-29 20:18 hello.c
    其中文件信息的第一块表示文件类型和权限,共10位

    第一位表示文件类型
  • - : 表示普通文件
    d : 表示目录
    c : 表示字符设备类文家
    b : 表示块设备文件
    l : 表示链接文件
    p : 表示管道文件
    s : 表示套接子文件

    第二到第十位表示权限,三位为一组,共三组,分别表示属主权限–u、表示属组权限–g,其他用户权限–o

    r : 表示只读
    w : 表示只写
    x : 表示可执行
    - : 表示无权限

    第二块表示文件硬连接数
    硬链接
    创建 </br>
    `ln destfile srcfile` </br>
    特性 </br>
    1、 给文件做一个备份 </br>
    2、 硬链接文件inode节点号一致 </br>
    3、 不可以对目录备份 </br>
    4、 不可以跨分区 </br>
    软连接
    创建 </br>
    `ln -s destfile srcfile` </br>
    特性 </br>
    1、 相当于创建一个快捷方式 </br>
    2、 文件inode节点号不一致 </br>
    3、 可以对目录设置 </br>
    4、 不可以跨分区 </br>

第三块表示文件属主,即文件拥有者

第四块表示文件属组,即文件拥有组

第五块表示文件大小,以字节计

第六块表示修改文件内容时间

  • atime:访问时间 (cat tail head more less …)
  • ctime:表示修改文件属性时间(chmod chown)
  • mtime:表示内容时间 同时触发atime 和 ctime时间

第七块表示文件名

-h

表示易读方式显示文件大小

bit => byte => K => M => G => T => P => ….

chmod

指定权限命令

原有基础上添加权限

案例

1
2
3
4
5
6
7
[root@localhost test]# ll
总计 0
-rw-r--r-- 1 root root 0 12-04 20:34 aa
[root@localhost test]# chmod u+x aa
[root@localhost test]# ll
总计 0
-rwxr--r-- 1 root root 0 12-04 20:34 aa

原有基础上取消权限

案例

1
2
3
4
5
6
7
[root@localhost test]# ll
总计 0
-rwxr--r-- 1 root root 0 12-04 20:34 aa
[root@localhost test]# chmod u-w aa
[root@localhost test]# ll
总计 0
-r-xr--r-- 1 root root 0 12-04 20:34 aa

直接指定文件的权限

案例

1
2
3
4
[root@localhost test]# chmod  u=rwx aa
[root@localhost test]# ll
总计 0
-rwxr--r-- 1 root root 0 12-04 20:34 aa

一次性指定权限

案例

1
2
3
4
5
6
7
[root@localhost test]# ll
总计 0
-rwxr--r-- 1 root root 0 12-04 20:34 aa
[root@localhost test]# chmod u=rx,g+w,o-r aa
[root@localhost test]# ll
总计 0
-r-xrw---- 1 root root 0 12-04 20:34 aa

修改文件属主或者属组

1
chown username:groupname filename

案例

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost test]# chown tom aa
[root@localhost test]# ll
总计 4
---------- 1 tom root 19 12-04 20:48 aa
[root@localhost test]# chown :jim aa
[root@localhost test]# ll
总计 4
---------- 1 tom jim 19 12-04 20:48 aa
[root@localhost test]# chown jim:tom aa
[root@localhost test]# ll
总计 4
---------- 1 jim tom 19 12-04 20:48 aa

cd

作用是切换工作目录

绝对路径

从根开始路径就是绝对路径

相对路径

从当前工作目录开始就是相对路径

特殊目录

符号 表示
. 表示当前工作目录
.. 表示上级目录
~ 表示当前用户家目录

用户

区分用户是通过用户的uid来区分的

[root@localhost bb]# id root

uid=0(root) gid=0(root)

groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

[root@localhost bb]# id tom

uid=500(tom) gid=500(tom) groups=500(tom)

  • 管理员用户root

    /root

    普通用户:

    /home/</br>

    cd : 表示回到当前用户家目录下

    - : 表示返回上次所处的目录环境

    pwd查看当前工作目录绝对路径
    /test

touch

创建文件

format

touch filename

touch a{1..100} //创建了100个文件

cp

实现拷贝,复制功能

format

`cp option srcfile destfile`

option

  • -r : 表示拷贝目录

案例

拷贝文件时候如果目标文件存在,则无需交互式确认

1
2
/bin/cp srcfilename destfilename
\cp srcfilename destfilename

mv

实现移动、改名功能

案例

1
2
3
4
5
[root@localhost test]# ls
a.txt
[root@localhost test]# mv a.txt b.txt 表示改名
[root@localhost test]# ls
b.txt
1
2
3
4
5
6
7
8
9
10
11
[root@localhost test]# ls
aa b.txt
[root@localhost test]# mv b.txt aa 表示移动
[root@localhost test]# ls
aa
[root@localhost test]# tree
.
`-- aa
`-- b.txt

1 directory, 1 file

移动并改名

1
2
3
4
5
6
7
8
9
10
11
[root@localhost test]# mv aa/b.txt /tmp/c.txt
[root@localhost test]# tree
.
`-- aa

1 directory, 0 files
[root@localhost test]# tree /tmp/
/tmp/
|-- a.txt
|-- b.txt
`-- c.txt

rm

删除

rm option filename

option

  • -f : 表示无序确认删除
    -r : 表示删除目录

    通配符

    *: 匹配任意多个字符
    ?: 匹配任意一个字符

mkdir

表示创建目录

[root@localhost test]# mkdir aa/bb/cc/dd

mkdir: 无法创建目录 “aa/bb/cc/dd”: 没有那个文件或目录

[root@localhost test]# mkdir -p aa/bb/cc/dd

cat

查看文件内容

tree

树状结构显示目录

rmdir

删除目录,不能删除非空目录

grep

查看匹配关键字的行

案例

1
2
3
4
5
6
7
8
grep root ./passwd  //表示查看匹配字符串
grep -w root a.txt //表示查看匹配关键字
grep -v root a.txt //表示查看不匹配字符串行
grep -i root a.txt //表示不区分大小写查看
grep ^root a.txt //表示查看以root开头行
grep root$ a.txt //表示查找root结尾行
grep -n root$ a.txt //表示查看第几行
grep -v ^$ ./a.txt //表示去除空行

alias

给命令定义别名

案例

临时有效地定义一个命令别名

1
2
3
4
5
6
[root@localhost test]# llll
bash: llll: command not found
[root@localhost test]# alias llll='ls -l'
[root@localhost test]# llll
总计 4
drwxr-xr-x 2 root root 4096 12-13 19:33 aa

永久有效地定义命令别名

1
vim ~/.bashrc 

修改内容为

1
alias llll='ls -l'

生效

1
source ~/.bashrc






  • end