This article will give you a detailed introduction to Linux commonly used instructions, demonstrations and some basic knowledge.
Table of contents
-
ls command -
file directive -
pwd command -
whoami command -
cd command -
Relative and absolute paths -
which command -
touch command -
mkdir command -
Add user trust relationship -
rmdir command -
rm command -
man command -
cp command -
mv command -
edit -
cat command -
echo command -
redirect -
more command -
less command -
head directive -
tail command -
Pipeline operation -
date directive -
cal instruction -
find command -
grep command -
zip command/unzip command -
tar command -
bc command -
uname directive -
shutdown command -
stat command -
Three times under Linux -
touch command supplement -
Hotkeys in Linux - -
List of instructions
ls command
grammar:ls [选项] [目录]
Function: ls has multiple options. For a directory, this command lists all subdirectories and files under the directory. For files, the filename will be listed along with other information
ls without options displays files in the current directory by default
Options:
-l
: List file details as a list
In the Linux system, the file type is not distinguished by the file suffix, but the file type is identified by the first character, whether it is the file suffix .c
or .txt
other suffixes, only the ones that start with - are ordinary files.
-
d: directory file, simply a folder, we can create a new file under the directory file -
-: Ordinary files, divided into text files, dynamic and static libraries, executable programs, etc. -
l: Soft link (similar to Windows shortcut) -
b: block device files (such as hard disks, optical drives, etc.) -
p: pipe file -
c: character device file (such as a serial device such as a screen) - -
s: socket file
We focus today on the first two types of documents
A single ordinary file can be divided into many kinds. In order to be compatible with Windows and to facilitate most of our Linux users to distinguish different files, we are still used to expressing different file types by suffixes. However, some files must be suffixed. For example, files written in C language
.c
need special processing. The suffix distinguishes the type.
-
-a: Display all files in the directory, including hidden files. The ones starting with a dot indicate hidden files. One dot indicates the current path, and two dots indicate the upper-level path. We will explain in the following cd instructions. and ..
-
-d : Display the directory like a file without displaying the files in the directory. The dot here means the current path
-
-R: List files in all subdirectories (recursively), all files in the directory will be listed
-
-n : Use numeric UID, GID instead of name
There are some other options that will not be demonstrated, you can try it out
-
-1 output only one file per line -
-r reverse sort directory -
-k Indicates the size of the file in k bytes. ls –alk specifies the file -
-i Output file's inode index information. Such as ls -ai to specify the file - -
-t sort by time
file directive
Function: View file type
grammar:file [文件]
[king@VM-12-11-centos ~]$ file cat.txt
cat.txt: UTF-8 Unicode text #类型是文本文件
[king@VM-12-11-centos ~]$ file ret #类型是目录文件
ret: directory
pwd command
grammar:pwd
Function: Display the current path
whoami command
Function: Display the current user
two ways of writing
cd command
Function: switch path
usage:cd [想要切换的路径]
Linux manages files in a multi-fork tree structure. The files and directories on the disk are organized into a directory tree, and each node is a directory or file. Root directory:/
Relative and absolute paths
When we need to find a file, we must know the path and file name of the file, such as finding text.c
a file, because each node has only one parent node, and the path from the root directory to the text.c
file is unique. The complete path describing the location of the file is the absolute path. The absolute path can uniquely identify a file. The pwd command above shows the absolute path.
The relative path is: its own path relative to the target location, which is not unique, we can find files from different locations
-
Absolute path: The path from the root directory to the specified file, which can uniquely identify a file
-
Relative path: the path from a file to the specified file, which cannot uniquely identify a file
The cd command can switch paths through relative paths and absolute paths.
You can switch to a specified directory through an absolute path
You can also switch through relative paths, dots indicate the upper-level path, cd ..
switch to the upper-level directory, and cd ../../
switch to the upper-level path
[king@VM-12-11-centos Linuxclass]$ pwd
/home/king/Linuxclass
[king@VM-12-11-centos Linuxclass]$ cd classcode
[king@VM-12-11-centos classcode]$ ls
a.out ret.txt test test.c
[king@VM-12-11-centos classcode]$ cd test
[king@VM-12-11-centos test]$ cd ../../p1 #切换到上上级目录的p1路径下
[king@VM-12-11-centos p1]$ pwd
/home/king/Linuxclass/p1
Some friends may have doubts, cd ..
they can switch to the previous path, so cd .
what's the use? We are already in the current directory and do not need to switch.
Here we use vim to write a
hello Linux
code, compile it through gcc, and generate the executable program of a.out, and we need the path and file name to run the executable program, and the path can be represented by a dot, compared to the following It is much more convenient to use absolute paths
cd has two more options
[king@VM-12-11-centos Linuxclass]$ pwd
/home/king/Linuxclass
[king@VM-12-11-centos Linuxclass]$ cd ~ #cd ~进入工作目录,就是用户的路径下
[king@VM-12-11-centos ~]$ pwd
/home/king
[king@VM-12-11-centos ~]$ cd - #cd -返回最近一次访问的路径
/home/king/Linuxclass
which command
grammar:which [指令]
Function: View the system path and related information of the command
[king@VM-12-11-centos Linuxclass]$ which ls
alias ls='ls --color=auto' #alias:给指令取别名
/usr/bin/ls #我们的指令都是系统简化过的
[king@VM-12-11-centos Linuxclass]$ which ll
alias ll='ls -l --color=auto' #ll可以代替ls -l
/usr/bin/ls
[king@VM-12-11-centos Linuxclass]$ which cd #同时我们发现指令都是在bin目录下的
/usr/bin/cd
Instructions are essentially programs. In Linux, instructions, commands, and tools are executable programs, and they are all stored in ordinary files. Under Linux, everything is a file, and our instructions are in the bin directory.
touch command
Function: Create a new file, change the date of the document or directory, including the access time and change time, we first understand the new file, we will introduce the date of changing the directory at the end of the article
It should be noted that touch creates ordinary files, not directory files, so you cannot cd into it.
[king@VM-12-11-centos test]$ touch file1 file2 file3 #touch可以连续创建多个文件
[king@VM-12-11-centos test]$ touch file4 #创建单个文件
[king@VM-12-11-centos test]$ ls
file1 file2 file3 file4
mkdir command
Function: create a new directory file
mkdir can also create multiple directory files at the same time, cd can enter, but cannot create multiple subdirectories under one directory file, here it shows that it cannot be created
Option -p: recursively build multiple subdirectories
Add user trust relationship
The tree command here is to display the file in a tree form, which needs to be downloaded with yum
yum intsall -y tree
If we are ordinary users, we need to add sudo in front. sudo can briefly elevate permissions and execute corresponding commands as root
Su alone means switching to the root user, you need to enter the root user's password
[ret@VM-12-11-centos ~]$ su
Password: #输入root用户密码
[root@VM-12-11-centos ret]# whoami #切换为root用户
root
[root@VM-12-11-centos ret]# su ret #root用户可以直接切换成指定用户,不需要输入用户密码
[ret@VM-12-11-centos ~]$ whoami #现在我们就切换到用户ret了
ret
If we sudo yum install -y tree
display an instruction similar to the following, it means that the current user has not added a trust relationship
We need to add the trust relationship to the user as root. We switch the identity to the root user, and execute the following command to add the trust relationship, and then we can sudo
echo 'xxx ALL=(ALL) ALL' >> /etc/sudoers (其中xxx代表用户名)
rmdir command
Function: delete empty directories, that is, directories that do not store other files
Here p1 is not empty, so it cannot be deleted. If you want to use the rm command
rm command
We generally use the rm command to delete files, rmdir is a bit tasteless
Function: delete directory or file
Options:
-
-f: force delete -
-r: Delete the directory and all files in the directory recursively.
The r option is required to delete a directory, and our p1/p2/p3 will be deleted at this time.
If we want to delete all files in the directory without deleting the directory, add wildcards directly*
Remember not to delete the root directory
rm -rf / #切记不要使用,递归强制删除根目录,可能会把系统干崩
man command
There are many instructions in linux, if we don't remember the use of a certain instruction, we can man and ask the man
Options:
1 is a normal command
2 is a system call, such as open, write and the like (through this, at least you can easily find out what header files need to be added to call this function)
3 is a library function, such as printf, fread4 is a special file, that is, various device files under /dev
5 refers to the format of the file, such as passwd, which will explain the meaning of each field in the file
6 is reserved for the game, defined by each game itself
7 is the attachment and some variables, such as global variables such as environ, which are explained here
8 is a command for system management, these commands can only be used by root, such as ifconfig
9 Kernel routines
Let's start with 1 and 3 for now
The same man needs us to download, ordinary users plus sudo
yum install -y man-pages
man does not add options, the default interpretation command
[king@VM-12-11-centos file11]$ man ls
q to exit the current interface
[king@VM-12-11-centos file11]$ man 3 printf #我们可以查看printf库函数
cp command
grammar:cp [选项] [源文件] [目标文件] [指定目录]
Function: Copy files or directories
Common options:
-
-f Force copies of files or directories, regardless of whether the destination file or directory already exists
-
-r Recursive processing, processing files and subdirectories in the specified directory together.
We copy the file file11 and name it file at the same time
By default, it will be copied to the current directory, or you can specify a directory at the end.
mv command
Function: move files or rename files
usage:mv [选项] [源文件] [目标文件]
Options:
-
-f : force means to force, if the target file already exists, it will be overwritten without asking
-
-i : If the destination file (destination) already exists, it will ask whether to overwrite!
Here we move the file file11 to the file directory
If mv is not followed by a file, but a filename, the file will be renamed
For example rename the file file11 to change
cat command
Function: View file content
Syntax: cat [options] [files]
Options:
-
-b : Number non-empty output lines
-
-n : Number all lines of output
-
-s : Do not output multiple blank lines
echo command
Function: Display the string to the standard output, which is the screen, echo wraps by default
redirect
So can we write the string to a file?
答案是可以的,通过重定向,字符串后面加 > 符号和文件名,就可以将本该显示到标准输出的字符串,写入文件中,如果文件不存在,就会自动创建文件,这就是输出重定向
将本该显示到屏幕上的字符串写入目标文件中,同时输出重定向会清空文件原始内容再重新写入
在此写入时,之前的hello Linux已近被清空了
想要保留之前的内容并写入就需要追加重定向 > 符号变成 >> 符号
cat指令后面不加文件,默认从键盘中读取内容并回显到显示器上,也就是屏幕上
之前cat显示文件内容就是通过输入重定向显示的
[king@VM-12-11-centos ~]$ cat < file #这个 < 符号可以省略
AAAAAAA
BBBBBBB
CCCCCCC
more指令
功能:和cat类似,一般用于查看日志,内容比较长的文件
选项:
-
-n :对行编号
-
q :退出more
比如这里我写一个内容从0-1000行的文件
count=0; while [ $count -le 1000 ]; do echo "hello ${count}"; let count++; done > cat.txt
more会先显示一部分内容,按回车就可以继续读取内容,按q退出,但more有一个弊端,只能往下翻,不能往上翻,所以我们一般不用more指令
less指令
功能:查看文件内容,less用法比more更具有弹性,可以使用 [pageup][pagedown]
(前后按键)等按键的功能来往前往后翻看文件,而且less在查看之前不会加载整个文件。
选项:
-
-i :忽略大小写
-
-N :显示行号
-
/字符串:向上搜索字符串的内容
-
?字符串:向下搜索字符串的内容
-
n :重复前一个搜索(与/ 或?有关)
-
N:返向重复前一个搜索(与/ 或?有关)
-
q:退出less
[king@VM-12-11-centos ~]$ less cat.txt
我们输入 /3向上搜索字符3,less就会帮我们标记字符3的内容
还有其它选项大家可以直接尝试,这里就不在演示了,我们q退出less界面
head 指令
功能:显示文件内容的开头到标准输出,也就是屏幕上,head不加参数默认显示前10行
选项:
-n<行数>
[king@VM-12-11-centos ~]$ head cat.txt #默认输出前10行
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[king@VM-12-11-centos ~]$ head -n5 cat.txt #指定输出前5行
hello 0
hello 1
hello 2
hello 3
hello 4
[king@VM-12-11-centos ~]$ head -5 cat.txt # n可以省略
hello 0
hello 1
hello 2
hello 3
hello 4
tail指令
功能:用于显示指定文件末尾内容,我们一般查看日志时肯定是查看最新的内容,也就是从尾部开始查看,tail就能很好的帮助我们查看最近的内容
选项:
-
-f :循环读取
-
-n<行号>
:显示行数
tail - f filename
会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容,非常适合查看日志
[king@VM-12-11-centos ~]$ tail cat.txt #tail不加行号默认显示尾10行
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
[king@VM-12-11-centos ~]$ tail -n5 cat.txt #指定显示尾5行
hello 996
hello 997
hello 998
hello 999
hello 1000
[king@VM-12-11-centos ~]$ tail -5 cat.txt #n可以省略
hello 996
hello 997
hello 998
hello 999
hello 1000
那我们可以显示文件的头和尾,如果要显示文件中间的内容怎么办呢?
比如我们要显示第100行到110行,有两种方法:
通过重定向将前110行写入一个tmp临时文件,然后在读取尾10行,不过这种方法需要重新创建文件,不进浪费空间效率也低
答案是有的,第二种方法借助管道操作
管道操作
使用管道时,默认隐式发生了重定向
# 符号 | 表示管道,通过管道将执行的结果传给下一条指令
[king@VM-12-11-centos ~]$ head -110 cat.txt | tail -10
hello 100
hello 101
hello 102
hello 103
hello 104
hello 105
hello 106
hello 107
hello 108
hello 109
date指令
格式
-
%H : 小时(00..23) -
%M : 分钟(00..59) -
%S : 秒(00..61) -
%X : 相当于 %H:%M:%S -
%d : 日 (01..31) -
%m : 月份 (01..12) -
%Y : 完整年份 (0000..9999) -
%F : 相当于 %Y-%m-%d
[king@VM-12-11-centos ~]$ date #date默认显示
Fri Jan 28 16:55:54 CST 2022
[king@VM-12-11-centos ~]$ date +%s #date +%s显示时间戳
1643360162
[king@VM-12-11-centos ~]$ date +%F%X #按年月日 时分秒的格式显示当前时间
2022-01-2804:56:08 PM
[king@VM-12-11-centos ~]$ date +%F%X@1643360162 # @时间戳,将时间戳转换成标准时间
2022-01-2804:56:27 PM@1643360162
我们可以加_以示区分,但不能是空格,这种写法是错误的
cal指令
功能:查看日历
选项:
-
-3 显示系统前一个月,当前月,下一个月的月历
-
-y 显示当前年份的年历
-
cal默认显示当前月的月历
[king@VM-12-11-centos ~]$ cal -y 2021 #也可以指定年份显示2021的年历
find指令
功能:在文件数中查找文件,并做出相应的处理(可能会访问磁盘),find指令会帮我们在当前目录以及所有该目录的所以子目录下查找相应的文件,当我们遍历一个大的文件系统时,可能会花费一点时间,然后显示相应的文件信息,但当我们下一次再用find查找时,就会快很多
find的选项非常多,这里列举一个,想了解更多可以man find
选项:-name :按照文件名查找
我们在根目录下查找文件名为text的文件,不过这里很多都是permission denied
,因为普通用户的权限不够,有很多文件都不能读取
我们在自己的目录下查找文件名为file的文件,就会把该目录下的所以子文件名为file的显示出来
指定目录
语法:find [起始目录位置] [-name][filename]
grep指令
功能:文本行过滤,搜索字符串
常用选项:
-
-i:忽略大小写,大小写视为相同
-
-n:输出行号
-
-v:反向选择
还是拿那个文件内容为0-1000行的文件cat.txt
举例
我们在显示文件内容时,将带'90'的字符都显示出来
我们利用输出重定向再往文件内写入两行,-i忽略大小写,就可以将这两行都搜索出来
同时grep指令也支持正则表达式,可以搜索 任意我们想要搜索的字符,大家下去可以自行尝试
我们先追加两行到cat.txt
中,用正则表达式就可以将这两行搜索出来
[king@VM-12-11-centos ~]$ echo "he9999" >> cat.txt
[king@VM-12-11-centos ~]$ echo "he9290" >> cat.txt
[king@VM-12-11-centos ~]$ cat cat.txt | grep 'he[0-9]*$'
he9290
he9999
zip指令/unzip指令
zip语法:zip [压缩后的文件名.zip] [目标文件]
功能:将文件压缩成.zip格式
常用选项:
-
-r :将指定目录下的所有文件和子目录一并处理,按递归方式
-
unzip 语法 :
unzip [压缩文件.zip]
-
-d:压缩到指定路径下
如果我们不加-r选项,直接压缩文件
我们以压缩test文件为例,其中test还有3个子目录file,file22和file33,压缩后的file.zip大小只有160
解压后发现test文件是个空目录,没帮我们将所以文件都压缩过来
所以我们需要带上选项-r,解压后我们发现cur.zip的大小为774,明显大于之前的160
加选项-d,将cur.zip解压到指定路径ret文件中
tar指令
和zip/unzip
类似,不过解压后的文件后缀为.tgz
功能:完成打包,压缩,解压
选项:
-
-c :建立一个压缩文件的参数指令(create 的意思) -
-x :解开一个压缩文件的参数指令 -
-t :查看 tarfile 里面的文件! -
-z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩? -
-j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩? -
-v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程! -
-f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数! -
-C :解压到指定目录
通常将-czf
和-xzf
配合使用,用来压缩和解压 ,这里我们同样将压缩后的文件cur.tgz
移动到test目录下进行解压
这里我们将压缩后的文件cur.tgz
通过-C
选项指定路径,将文件解压到ret目录中
前面的操作都是解压和压缩,而打包是将需要压缩的所以文件都打包在一起,可以理解将文件都整理起来,文件的大小没有发生变化
bc指令
功能:可以理解为计算器
直接将计算结果显示到屏幕上,Ctrl+c
退出
uname 指令
功能:获取电脑和操作系统相关信息
选项:
-
-a :输出详细信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类 型,硬件平台类型,操作系统名称
-
-r :输出内核版本号
uname默认输出内核名称,hostname默认输出主机名,实际上我们只需要-a选项就够了
内核版本号:3表示主版本号,10表示副版本号,其中副版本号为偶数:稳定版内核 为奇数:测试版内核,我们服务器的使用的内核版本一般都不是最新的,而是一些已经使用过多年的经典版本,对全新的版本,需要经过岁月的考验,证明内核是稳定,安全,高效的才可能使用
shutdown指令
选项:
-
-h:将系统的服务停掉后立即关机
-
-r:将系统服务停掉后重新启动
- -t second:-t [秒数]
,即过几秒后关机
对于服务器呢我们就不需要关机了
这里我们补充touch指令的更改时间
在介绍touch修改文件时间前我们先介绍一个指令
stat指令
功能:查看文件详细信息
用法 :stat [文件]
stat查看test文件详细信息
文件的相关信息:
-
File
:显示文件名 -
Size
:显示文件大小 -
Blocks
:文件使用的数据块总数 -
IO Block
:IO块大小 -
regular file
:文件类型(常规文件) -
Device
:设备编号 -
Inode
:Inode号 -
Links
:链接数 -
Access
:文件的权限 -
Gid、Uid
:文件所有权的Gid和Uid
Linux下的三个时间
-
Access Time
:简写为atime,表示文件最近一次被访问时间。 -
Modify Time
:简写为mtime,表示文件内容最近一次被修改时间 -
Change Time
:简写为ctime,表示文件属性最近一次被修改的时间
atime:当我们过一段时间cat查看文件后,stat再次查看就会发先atime时间发生变化,当然对于2.6之后的内核,对atime的刷新时间重设了规则,atime不会立即被更新,而是一定时间间隔后OS才会自动更新时间。因为相比与修改文件内容和属性,查看文件是最高频的,如果频繁刷新atime会导致效率降低
对mtime:我们写入hell后,它的三个时间都发生变化,mtime的改变不一定会影响atime,但是ctime可能会随之改变,因为修改文件内容时,有可能会修改文件的属性,比如写入数据时文件的大小属性会被修改
ctime:更改文件属性的最近时间,这一行表示的就是文件属性
chgmod指令可以更改文件的访问权限,也就更改了文件属性
touch指令补充
选项:
-
-a 改变atime和ctime -
-c 或 --no-create
不建立任何文档。 -
-d 使用指定的日期时间,而非现在的时间,改变atime和mtime -
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。 -
-m 改变ctime和mtime -
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。 -
-t 使用指定的日期时间,而非现在的时间
常用的就是-d,-a -m,而touch指令默认修改所有时间,这里的touch是对已经创建的文件操作
Linux中的热键
[Tab]
:命令补全,按一次或两次,会将以wh开头的指令显示到屏幕上
[c]
:终止当前程序,比如屏幕上一直输出字符,写了一个死循环代码,就可以Ctrl +c
终止
[Ctrl d]
:代替exit切换身份,多次Ctrl d
直接退出
[Ctrl r]
:根据关键字查找历史命令。我们输入r,自动转换换成历史命令
[page up page down]
:上和下,可以翻看历史命令,我们可以直接翻最近输入过的指令,非常方便
指令一览
-
安装和登录命令:
login、shutdown、halt、reboot、install、mount、umount、chsh、exit、last
-
文件处理命令:
file、mkdir、grep、dd、find、mv、ls、diff、cat、ln
-
系统管理相关命令:
df、top、free、quota、at、lp、adduser、groupadd、kill、crontab
-
网络操作命令:
ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup
-
系统安全相关命令:
passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who
-
其它命令:
tar、unzip、gunzip、unarj、mtools、man、unendcode、uudecode
来源:java知音
原文链接:https://blog.csdn.net/weixin_46016019
重磅!程序员交流群已成立
公众号运营至今,离不开小伙伴们的支持。
为了给小伙伴们提供一个互相交流的平台,特地开通了程序员交流群
There are many technical masters in the group, who will share some technical points from time to time, and some resource collectors will share some high-quality learning materials from time to time. (The group is completely free, no advertisements and no classes!)
Friends who need to join the group can long press to scan the QR code below.
▲Long press to scan the code