目录
一.关机、开机命令
shutdown//一分钟后关机
shutdown -h now//立马关机
shutdown -h 30//30分钟后关机
reboot//重启系统
shutdown -r now//立马重启系统
shutdown -r 30//30分钟后重启系统
shutdown -c//取消关机或重启
二.路径表示方法
pwd 查看当前工作路径
cd 新路径 更改工作路径,切换路径(默认切换到当前用户家目录)
cd ../ 切换到当前路径的上一目录
cd ~ 切换到当前用户家目录
cd 切换到当前用户家目录
cd - 切换到上一次工作目录
destiny-yyds@destinyyyds-virtual-machine:/$ cd /home 切换到/home目录下
destiny-yyds@destinyyyds-virtual-machine:/home$ pwd 打印当前工作路径
/home
destiny-yyds@destinyyyds-virtual-machine:/home$ cd ../ 切换到当前路径的上一目录
destiny-yyds@destinyyyds-virtual-machine:/$ pwd 打印当前工作路径
/
destiny-yyds@destinyyyds-virtual-machine:/$ cd ~ 切换到当前用户家目录
destiny-yyds@destinyyyds-virtual-machine:~$ cd - 切换到上一次工作目录
/
destiny-yyds@destinyyyds-virtual-machine:/$ cd 切换到当前用户家目录
destiny-yyds@destinyyyds-virtual-machine:~$ pwd
/home/destiny-yyds
三.文件操作管理
1.判断文件类型(file)
常见文件类型:
文件类型 | 描述 |
字符设备(c) dev/tty | 所有输入输出的设备,如:键盘、鼠标、显示器、打印机、终端(pts文件中) |
块设备(b) dev目录下 | 所有存储设备称之为块设备文件,如:软盘、磁盘、光盘、U盘、磁带、光驱等 |
软连接文件(l) | 类似于Windows下的快捷方式 |
目录文件(d) | 相当于Windows下的文件夹 |
普通文件(f或-) | 类似Windows下记事本、word等,可以使用相关命令进行编辑、查看文件内容 |
管道文件(p) | 简单理解为程序或进程之间通讯的一种方式 |
套接字文件(s) | 简单理解为程序或进程之间通讯的一种方式 |
//目录文件
$ file dir1
dir1: directory
//块设备
$ file /dev/sda
/dev/sda: block special (8/0)
//字符设备
file /dev/tty1
/dev/tty1: character special (4/1)
//链接文件
$ file /bin/sh
/bin/sh: symbolic link to dash
2.列出目录内容(ls)
常见选项
-a all,查看目录下的所有文件,包括隐藏文件
-l 长列表显示
-h human,以人性化方式显示出来
-d 只列出目录名,不列出其他内容
-t 按修改时间排序
-r 逆序排列
-S 按文件的大小排序
-i 显示文件的inode号(索引号)
root@ubuntu:/# ls -a /root
root@ubuntu:/# ls -l /root
root@ubuntu:/# ls -lh /root
3.创建目录(mkdir)
root@ubuntu:/# mkdir /test //在根目录下创建test
root@ubuntu:/# mkdir ./test //在当前目录下创建test
root@ubuntu:/# mkdir -p /test/dir/project
//如果创建的目录的上一级目录不存在,就需要加-p参数;-p在前面和后面都可以
4.创建文件(touch)
(1)创建一个新的空文件(目标文件不存在)
root@ubuntu:/test# touch file1 在当前目录下创建file1文件
root@ubuntu:/# touch /test/file2 在test目录下创建file2文件
(2)修改文件时间(目标文件存在)
查看文件状态信息(stat):
root@ubuntu:/test# stat file1
File: file1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 4194308 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-17 23:02:14.719250765 -0700
Modify: 2022-05-17 23:02:14.719250765 -0700
Change: 2022-05-17 23:02:14.719250765 -0700
Birth: -
Access:文件的查看访问时间
Modify:文件修改时间
Change:文件的属性时间,文件的大小、权限等信息发生改变时,该时间会变
修改文件时间:
root@ubuntu:/test# touch file1
root@ubuntu:/test# stat file1
File: file1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 4194308 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-05-17 23:19:38.977341110 -0700
Modify: 2022-05-17 23:19:38.977341110 -0700
Change: 2022-05-17 23:19:38.977341110 -0700
Birth: -
-a:访问时间 access
-m:修改时间 modify
-t:时间类型格式
root@ubuntu:/test# touch -a file1 -t 201501010000 修改文件访问时间
root@ubuntu:/test# touch -m file1 -t 201601010000 修改文件修改时间
File: file1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 4194308 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-01-01 00:00:00.000000000 -0800
Modify: 2016-01-01 00:00:00.000000000 -0800
Change: 2022-05-17 23:23:45.023366838 -0700
Birth: -
只需要掌握touch -d的用法即可
root@ubuntu:/test# touch -d 20110808 file1 修改文件日期
root@ubuntu:/test# touch -d 1212 file1 修改文件时间
root@ubuntu:/test# touch -d "20101012 11:11 :11" file1 修改文件的时间和日期
root@ubuntu:/test# stat file1 查看文件状态
File: file1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 4194308 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2010-10-12 11:11:11.000000000 -0700
Modify: 2010-10-12 11:11:11.000000000 -0700
Change: 2022-05-17 23:29:26.878501881 -0700
Birth: -
5.查看文件内容
cat 命令:一般查看小文件,从第一行到最后一行列出来常见选项:
-n:显示行号
-A:显示控制字符,如换行符,制表符等( linux $ 和 Windows ^M$ )
tac 命令:一般查看小文件,从最后一行到第一行列出来
more和less 命令:一般查看大文件,q退出查看,可以搜索,建议less命令
head 命令:默认查看文件前10行, head -n 15或head -15 表示查看前15行
tail 命令:默认查看文件后10行, tail -n 15或tail -15 表示查看后15行; -f 表示动态查看
ldd 命令:一般用来查看二进制的命令文件
root@ubuntu:/test# cat /etc/passwd 查看etc/passwd内容
root@ubuntu:/test# cat -n /etc/passwd 查看etc/passwd内容,并打印行号
root@ubuntu:/test# tac /etc/passwd 查看etc/passwd内容
root@ubuntu:/test# head -5 /etc/passwd 查看前5行
root@ubuntu:/test# tail -5 /etc/passwd 查看后5行
root@ubuntu:/test# less /etc/passwd
6.拷贝文件(cp)
注意:本地文件拷贝
常用选项:
-a 递归拷贝文件,包括目录及文件属性信息
-r 拷贝目录
-p 拷贝文件包含文件的属性信息
-v 显示拷贝过程信息
用法:
cp 选项 需要拷贝的文件 拷贝到哪里去
root@ubuntu:/# cp /test/file1 /test/dir 把file1文件复制到dir目录中
root@ubuntu:/# cp /test/file1 /test/dir/file2 把file1文件复制到dir目录中,并重命名为file2
root@ubuntu:/# cp -r /test/dir /test/dir1 复制目录dir到dir1中
-a和-p有什么区别?
答:相同点都是需要拷贝文件的属性信息,比如拥有者(谁创建的等);不同点在于,-p只能拷贝文件,-a既可以拷贝文件 也可以拷贝目录。
7.移动或重命名文件(mv)
(1)移动文件用法(不同路径下)
# mv 需要移动的文件 移动到新的路径下注意:文件的路径不一样
(2)重命名用法(相同路径下)
# mv 原来文件的名字 新文件的名字注意:老文件和新文件的路径一样
root@ubuntu:/tmp# mv file is 将file移动到is中
root@ubuntu:/tmp/is# mv file file1 将file改为file1
8.删除文件(rm)
常用选项
-r 递归删除,一般用于删除目录
-f 直接删除,不提示
root@ubuntu:/tmp/is# rm -rf ./* 强制删除当前目录下所有文件,无提示
root@ubuntu:/tmp/is# rm -r dir 删除目录dir,有提示
root@ubuntu:/tmp/is# rm file 删除文件file,有提示
四.Linux下如何获取帮助
1.简约求帮助(help)
help命令:知道该命令的含义,相关参数不知道可以使用help
内部命令求帮助:help 命令
外部命令求帮助:命令 --help
cp --help
help cd
2.判断命令是外部还是内部(type)
内部命令:shell内置的命令,bash
外部命令:第三方程序,软件带来的命令
root@ubuntu:/test# type cd
cd is a shell builtin 内部命令
root@ubuntu:/test# type cp
cp is /bin/cp 外部命令
3.详细求帮助(man)
# man man
1 Executable programs or shell commands 所有用户使用命令
2 System calls (functions provided by the kernel) 系统调用
3 Library calls (functions within program libraries) 函数库
4 Special files (usually found in /dev) 设备与特殊文件
5 File formats and conventions eg /etc/passwd 文档格式说明
6 Games 游戏
7 Miscellaneous (including macro packages and conventions), e.g. man(7),
groff(7) 杂项
8 System administration commands (usually only for root) 系统管理员与程序用户相关
五.bash的标准输入输出
1.名词解释
标准输入(stdin):键盘上的输入 文件描述符—>0
标准输出(stdout):屏幕上正确的输出 文件描述符—>1
标准错误(stderr):屏幕上错误的输出 文件描述符—>2
2.相关符号
> :标准输出重定向,覆盖重定向,1>或> 标准输出重定向, 2> 标准错误重定向。
>> :重定向追加,1>> 标准输出追加,2>> 标准错误追加
< :标准输入
&> :标准输出标准错误重定向
3.举例说明
(1)环境准备
1.创建1.sh脚本文件
root@ubuntu:/tmp# echo -e 'date\nuuu' >1.sh
root@ubuntu:/tmp# cat 1.sh
date
uuu
2.给1.sh脚本文件增加可执行权限
root@ubuntu:/tmp# chmod +x 1.sh
3.执行1.sh脚本
root@ubuntu:/tmp# bash 1.sh
4.输出
2022年 05月 19日 星期四 00:28:22 PDT
1.sh: line 2: uuu: command not found
(2)将标准输出(屏幕上的正确结果)重定向到1.log文件中
root@ubuntu:/tmp# bash 1.sh >1.log
1.sh: line 2: uuu: command not found 标准错误依然在屏幕,正确结果到文件中
root@ubuntu:/tmp# cat 1.log
2022年 05月 19日 星期四 00:31:38 PDT 文件中是标准输出的结果
(3)将标准错误(屏幕上的错误结果)重定向到2.log文件中
root@ubuntu:/tmp# bash 1.sh 2>2.log
2022年 05月 19日 星期四 00:34:08 PDT 标准输出依然在屏幕,标准错误重定向到了文件
root@ubuntu:/tmp# cat 2.log
1.sh: line 2: uuu: command not found 文件中是标准错误结果
(4)将标准输出和标准错误一起重定向到3.log里
root@ubuntu:/tmp# bash 1.sh &>3.log
root@ubuntu:/tmp# cat 3.log
2022年 05月 19日 星期四 00:36:29 PDT
1.sh: line 2: uuu: command not found
./1.sh >/dev/null 2>&1 将标准输出和标准错误扔掉(放到空设备)
等于
./1.sh &>/dev/null
文件/dev/null是Linux系统下特殊的设备,空设备,类似于黑洞。
4.echo命令
echo会将输入的字符串送往标准输出,并在最后加上换行符。 可以理解为打印字符串。
常见选项:
-n :不输出最后的换行符“\n”
-e:解释转义字符(字符串中出现\n、\t等特殊字符,则特别加以处理,而不会将它当成一般文字输出)
root@ubuntu:/tmp# echo hello world 打印hello world
hello world
root@ubuntu:/tmp# echo hello world >file1 将hello重定向到file1文件
root@ubuntu:/tmp# cat file1
hello world
总结:
- echo表示打印字符串,默认将字符串送往标准输出;默认会打印一个换行符
- echo可以结合>或者>>符号来使用,进行文件的创建或内容追加。
常见控制字符:
\t 表示制表符
\n 表示换行符
root@ubuntu:/tmp# echo -e "date\nhello" >file1 -e把\n转译为换行符
root@ubuntu:/tmp# cat file1
date
hello
六.Linux下文件查找命令
1.命令查找(which、whereis)
(1)which 命令 :找出命令的绝对路径
(2)whereis 命令 :找出命令的路径以及文档手册信息(man)
root@ubuntu:/tmp# which mkdir
/bin/mkdir
root@ubuntu:/tmp# whereis mkdir
mkdir: /bin/mkdir /usr/share/man/man1/mkdir.1.gz /usr/share/man/man2/mkdir.2.gz
2.文件查找(find)
find 命令:精确查找,磁盘搜索,IO读写,cpu开销大
用法1:找出来输出到屏幕
根据需求查找出来直接输出到屏幕
常见选项 | 含义 | 备注 |
-name | 按照文件名查找文件 | |
-iname | 按照文件名忽略大小写查找 | |
-size | 按照文件大小来查找 | +1M 大于1M; -1M 小于1M; 1M 等于1M |
-type | 按照文件类型来查找 | |
-mtime | 按文件修改时间来查找文件 | -n指n天以内,+n指n天以前 |
-atime | 按文件访问时间来查 | |
-ctime | 按文件创建时间来查找文件 | |
-perm | 按照文件权限来查找文件 |
-mtime:假设今天是20号。
-mtime -2:找18号之前文件(不包括18号,<18号);
-mtime +2:找18-20号,两天内文件(包括18号,>=18);
-mtime 2:找18号当天文件;
-daystart:未满一天
1.根据文件名查找
root@ubuntu:/# find /tmp -name "file1"
/tmp/is/file1
/tmp/file1
root@ubuntu:/# find /tmp -iname "file1"
/tmp/is/file1
/tmp/file1
/tmp/FILE1
root@ubuntu:/# find /tmp -name "*.log"
/tmp/3.log
/tmp/1.log
/tmp/2.log
2.根据文件类型查找
root@ubuntu:/# find /tmp/is -type f
/tmp/is/.X1024-lock
/tmp/is/file1
3.根据文件大小查找
[root@heima test]# find . -type f -size +1M
[root@heima test]# find . -type f -size -1M
[root@heima test]# find . -type f -size -1024k
[root@heima test]# find . -type f -size 9M
4.根据文件属性(权限,创建者和所属组)
[root@heima test]# find . -user heima -group heima -type f
[root@heima test]# find . -type f -perm 644
5.mtime
[root@heima test]# find ./ -type f -mtime +2
[root@heima test]# find ./ -type f -mtime -2
[root@heima test]# find ./ -type f -mtime 2
用法2:找出来执行命令
根据需求查找出来后执行某个动作(命令)
find 路径 选项 关键字 动作
常见动作 | 说明 |
-exec | 对查找到的文件直接执行该参数后的shell命令 |
-ok | 对查找到的文件询问式执行该参数后的shell命令 |
-delete | 删除查找到的文件 |
-ls | 列出查找到的文件,详细信息 |
| 打印出查找到的文件(默认选项) |
语法结构:
注意:
- -exec或者-ok后面写完命令必须以空格反斜杠\;结尾( \;)
- {}表示find命令所找出来的内容
root@ubuntu:/tmp# find . -type f -exec cp {} ./is \;
找到当前目录下文件,然后拷贝到目录is下