文章目录
一、Linux基础命令
ls ---->搜寻可执行程序或代码,搜寻的路径是?
shell程序帮助搜寻可执行程序的路径定义
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
命令帮助
command --help (这里的command填具体命令)
man手册 -----更加详细的帮助手册
man command
命令帮助的使用
- help命令
- man手册(man命令可以加options,例如man --help)
- 信息页(info,例如info ls)
- 程序本身的帮助文件(README)
- 程序的官网文档
- 发行版本的官网文档
目录管理类命令
1.创建目录
mkdir:
-p:–parents no error if existing, make parent directories as needed
递归创建:例如mkdir a/b可能失败,因为没有文件夹a,这时候可以用mkdir -p a/b
-v:–verbose print a message for each created directory
输出创建过程信息:
[root@localhost ~]# mkdir -pv a/b
mkdir: 已创建目录 "a"
mkdir: 已创建目录 "a/b"
-m:–mode=MODE set file mode (as in chmod), not a=rwx - umask
可以执行创建目录的权限,如果没有指定,默认是rwx - umask
2.删除目录
rmdir:
-p:递归删除
-v:显示删除过程
[root@localhost ~]# rmdir -pv a/b
rmdir: 正在删除目录 "a/b"
rmdir: 正在删除目录 "a"
3.修改目录名
mv 命令
例如: mv a/ b 将文件(夹)a修改为文件(夹)b
tree命令
可以展示目录结构
[root@localhost ~]# tree
.
├── a
│ ├── b
│ └── c
├── anaconda-ks.cfg
├── CentOS-7-x86_64-Minimal-1810.iso
├── Dockerfile
└── mc
文件查看命令
cat + 文件名,查看文件信息
tac + 文件名,与cat命令相反的顺序显示
more / less 翻页的形式查看文件信息(在查看大文件时使用)
head ,默认输出文件的前十行
[root@localhost ~]# head --help|more
用法:head [选项]... [文件]...
Print the first 10 lines of each FILE to standard output.
-c, --bytes=[-]K print the first K bytes of each file;#显示前K字节
-n, --lines=[-]K print the first K lines instead of the first 10;#显示前K行
tail,与head相对应,默认显示后十行。
tail -f + 文件名 / tailf + 文件名 :实时追踪文件内容,例如实时查看日志文件。
文件时间戳
stat命令
查看文件元数据(包含数据类型名称等)
[root@localhost ~]# stat anaconda-ks.cfg
文件:"anaconda-ks.cfg"
大小:1241 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:33574979 硬链接:1
权限:(0600/-rw-------) Uid:( 0/ root) Gid:( 0/ root)
环境:system_u:object_r:admin_home_t:s0
最近访问:2019-12-03 13:27:35.914833741 +0800
最近更改:2019-09-17 12:38:52.489655119 +0800
最近改动:2019-09-17 12:38:52.489655119 +0800
创建时间:-
最近访问:2019-12-03 13:27:35.914833741 +0800 (访问文件数据)
最近更改:2019-09-17 12:38:52.489655119 +0800 (改变文件内容)
最近改动:2019-09-17 12:38:52.489655119 +0800 (元数据改变)
就是文件时间戳
touch命令
创建文件命令touch,此外也可以用来
修改文件时间戳(牛逼一点的黑客再改动文件时修改时间戳,以防被发现做过手脚)
[root@localhost ~]# touch --help
用法:touch [选项]... 文件...
-a 只更改访问时间
-m 只更改修改时间
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
-t用于指定修改的时间格式,[[CC]YY]MMDDhhmm[.ss]
例如:
touch -a -t 10040400 1.txt 文件1.txt访问时间改为10月4日4时0分
touch -m -t 10040400 1.txt 文件1.txt修改时间改为10月4日4时0分
文件管理类命令
- cp命令,拷贝文件
[root@localhost ~]# cp --help
用法:cp [选项]... [-T] 源文件 目标文件
或:cp [选项]... 源文件... 目录
或:cp [选项]... -t 目录 源文件...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
SRC(源文件)如果是单个文件:
DEST(目的文件)不存在时,创建DEST并将SRC写入其中
DEST存在时,将SRC文件内容覆盖至DEST中
如果是DIRECTORY(目录),在目录中创建文件并写入
SRC如果是多个文件时:
只能用 cp [选项]… 源文件… 目录 的形式
SRC如果是目录:
使用-r参数
DEST必须也是为目录:
DEST存在,拷贝一致性文件进去
DEST不存在,创建目录,再拷贝一致性文件
常用选项:
-i:交互式
-r/-R:递归复制文件或目录
-a:归档
-p:连同属性复制,例如可以将相同的时间戳复制
-
mv命令,剪切/重命名
类比cp命令 -
rm命令,删除文件
-i:交互式,alias rm = ‘‘rm -i’’,因为rm是别名,所以rm时会自动交互
-f:强制删除,全部yes
-r:递归(可以删除目录)
二、文件链接
查看链接文件类型
stat或ll
链接方式:
- 硬链接:为inode分配多个文件名,通过文件找到inode,从而读取文件信息
- 软连接:类似于Windows上的快捷方式
由之前的学习知道文件是由 metadata和data组成,inode可以理解为一个指向文件存储位置的指针。metadata中包含inode值和文件名等信息。
元数据信息
[root@localhost ~]# stat 1.txt
文件:“1.txt”
大小:6 块:8 IO 块:4096 普通文件
设备:fd00h/64768d Inode:33726306 硬链接:1
权限 用户ID 组ID
权限:(0644/-rw-r–r--) Uid:( 0/ root) Gid:( 0/ root)
selinux权限
环境:unconfined_u:object_r:admin_home_t:s0
时间戳
最近访问:2020-02-09 16:44:54.554852331 +0800
最近更改:2020-02-09 16:44:40.723766733 +0800
最近改动:2020-02-09 16:44:40.723766733 +0800
创建时间:-
如图所示,硬链接与原始文件共用一个inode,但inode是不跨文件系统的(Ext3、Ext4),每个文件系统都有自己的inode列表。因此,硬链接是没办法跨文件系统的。
而软链接不同,软链接相当于重新创建了一个文件。这个文件也有独立的inode,只不过开打这个文件看里面内容的时候,内容指向另外一个文件。
可以跨文件系统,甚至目标文件被删除了,链接文件还是在的,只不过指向的文件找不到了而已(Window中的快捷方式)
引用原文地址:https://www.cnblogs.com/yeni/p/11570549.html
ln命令:创建链接文件
Usage: ln [OPTION]… [-T] TARGET LINK_NAME (1st form)
常见选项:
-s:建立软链接文件,默认情况是硬链接文件。
-f:强制创建
[root@localhost c]# ln 1.txt 1_hard.txt
[root@localhost c]# ln -s 1.txt 1_soft.txt
[root@localhost c]# ll -i
总用量 8
17672355 -rw-r--r--. 2 root root 6 2月 9 16:47 1_hard.txt
17672359 lrwxrwxrwx. 1 root root 5 2月 9 20:07 1_soft.txt -> 1.txt
17672355 -rw-r--r--. 2 root root 6 2月 9 16:47 1.txt
注意:
1.当我们创建硬链接文件的时候,inode不会发生改变,链接数会加1,同时文件内容相同
2.当我们创建软链接文件的时候,inode会重新分配,链接数保持为1,同时文件内容相同
3.链接目的:在节省存储空间的情况下,共享文件
4. 当源文件内容改变时,软硬链接文件内容都会改变
5.当删除源文件时,软链接失效,硬链接正常,硬链接和源文件在表现上可以说是独立的。
硬链接特点
- 无论修改源文件还是硬链接文件,另一个文件都会改变。
- 无论删除源文件还是硬链接文件,只要存在一个文件(link),这个文件依然可用。
- 硬链接不会新建inode,也不会更改
- 硬链接不能跨文件系统,因为在不同的文件系统中inode号是可以重新进行分配的
- 硬链接不能够链接目录
软链接特点
- 无论修改源文件还是软链接文件,另一个文件都会改变
- 删除软链接文件,源文件不受影响;反之,软链接文件不正常
- 软链接会新建inode号和block,block中不存储实际文件数据,只存源文件的inode和绝对路径
- 软链接是可以链接目录的
- 软连接是可以跨分区的
问题:那只删除文件名的话,文件占用的空间会释放吗?
只删除文件名会将link数减1; 当link为0时,文件占用的空间将释放掉
三、重定向及管道
程序 = 指令 + 数据
数据: 输入/输出数据
每打开一个文件都会有一个文件描述符fd
输入输出
标准输入
标准输出 1
标准错误输出 2
重定向
输出重定向
> 覆盖重定向
>> 追加重定向
2> 覆盖重定向错误输出流
2>> 追加重定向错误输出流
[root@test tmp]# echo "hello eagles"
hello eagles
[root@test tmp]# echo "hello eagles" > 2.txt
[root@test tmp]# cat 2.txt
hello eagles
[root@test tmp]# echo "hello eagles" >> 2.txt
[root@test tmp]# cat 2.txt
hello eagles
hello eagles
[root@test tmp]# lnd
-bash: lnd: command not found
[root@test tmp]# lnd 2> output_error
[root@test tmp]# cat output_error
-bash: lnd: command not found
[root@test tmp]# lnd 2>> output_error
[root@test tmp]# cat output_error
-bash: lnd: command not found
-bash: lnd: command not found
标准输出和错误输出到各自不同的位置
[root@test tmp]# ll 1> file.out 2> file_error.out
[root@test tmp]# cat file.out
total 8
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 0 Feb 6 07:53 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:53 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error
[root@test tmp]# lld 1> file.out 2> file_error.out
[root@test tmp]# cat file_error.out
-bash: lld: command not found
垃圾桶: /dev/null
合并标准输出和错误输出为同一数据流进行重定向
&> 覆盖重定向
&>> 追加重定向
2>&1 将错误流重定向到标准输出文件中 (>>)
1>&2 将正确流重定向到错误输出文件中 (>>)
[root@test tmp]# lld &> all.out
[root@test tmp]# ll &>> all.out
[root@test tmp]# cat all.out
-bash: lld: command not found
total 16
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 30 Feb 6 07:56 all.out
-rw-r--r--. 1 root root 30 Feb 6 07:53 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:53 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error
[root@test tmp]# lld 1> file.out 2> file_error.out 2>&1
[root@test tmp]# cat file.out
-bash: lld: command not found
[root@test tmp]# ll 1> file.out 2> file_error.out 1>&2
[root@test tmp]# cat file_error.out
total 16
-rw-r--r--. 1 root root 30 Feb 6 07:57 1.out
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 396 Feb 6 07:56 all.out
-rw-r--r--. 1 root root 0 Feb 6 07:59 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:59 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error
标准输入
覆盖:
cat > /path/somefile <<EOF
…
EOF
追加:
cat >> /path/somefile <<EOF
…
EOF
cat > test.txt <<EOF
hello eagles
hello Linux
EOF
cat >> test.txt <<EOF
hello eagles
hello Linux
EOF
管道
command1 | command2 | command3 …
最后一个命令会在当前shell进程的子shell进程中执行;前一个命令的执行结果
会由当前命令继续使用
tr命令
Usage: tr [OPTION]… SET1 [SET2]
常用选项:
-d: 删除
案例1:将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中
[root@node1 ~]# head ‐n 5 /etc/passwd | tr 'a‐z' 'A‐Z' > /tmp/passwd.output
案例2:将登陆至当前系统上用户信息中的最后1行的信息转换为大写后保存至/tmp/who.out文件中
[root@node1 ~]# who | tail ‐n 1 | tr 'a‐z' 'A‐Z' >/tmp/who.out
案例3:将文件中的:删除之后重定向到新文件中
[root@test tmp]# cat output_error | tr -d ":" > output_error_new
wc命令:统计
Usage: wc [OPTION]… [FILE]…
[root@test tmp]# wc test.txt
4 8 50 test.txt
依次显示行数、单词数和字节数
常用参数:
-l : 只显示行数
-w : 只显示单词数
-c :只显示字符数
[root@test tmp]# wc -l test.txt
4 test.txt
[root@test tmp]# wc -w test.txt
8 test.txt
[root@test tmp]# wc -c test.txt
50 test.txt
cut命令:
[root@test tmp]# cut --help
Usage: cut OPTION… [FILE]…
Print selected parts of lines from each FILE to standard output.
常用参数:
-d: 指定分隔符
-f: 指定字段
实例: 以:为分隔符显示第7列内容
[root@test tmp]# cut -d: -f7 passwd.output
sort命令
常用参数:
-f: 忽略大小写
-r:逆序
-t:字段分隔符
-k #: 以指定字段为标准排序
-n: 以数字进行排序(默认情况下,如果没有-n参数,会以ASCII进行排序)
-u: 排序后进行去重
实例: 以:为分隔符,指定第3字段,以数字进行逆序
[root@test tmp]# sort -t: -k3 -r -n passwd.output
uniq命令:去重
常用参数:
-c : 显示每行出现的次数
-d :仅显示重复过的行
-u :仅显示不曾重复过的行
实例:
[root@test tmp]# uniq -c test.txt
2 hello eagles
2 hello Linux
1 fjkldlaj
1 fdkajjf;
[root@test tmp]# uniq -d test.txt
hello eagles
hello Linux
[root@test tmp]# uniq -u test.txt
fjkldlaj
fdkajjf;
测试文本:
[root@test tmp]# cat test.txt
hello eagles
hello eagles
hello Linux
hello Linux
fjkldlaj
fdkajjf;
综合案例:以:为分割,取出/etc/passwd文件的第6列至第10列,并将这些信息按照第3个字段的数值大小进行排序,最后仅显示一个字段
[root@node1 ~]# cut ‐d: ‐f6‐10 /etc/passwd |cut ‐f3 | sort ‐n |uniq -c
[root@test tmp]# cut -d: -f6-10 /etc/passwd | sort -t: -k3 -n | uniq -c