A complete collection of notes on commonly used Linux commands (recommended to be collected!!)


Insert image description here


1. ls command

Syntax: ls [options][directory or file]
Function: For directories, this command List all subdirectories and files in this directory. For files, the file name is listed along with other information. (Operation of file attributes)
Common options:

Options Function
-a List all files in a directory, including implicit files starting with .
-d Display the directory as a file, rather than displaying the files below it. For example: ls –d specifies the directory.
-i Outputs the index information of the i-node of the file. For example, ls –ai specifies the file.
-k Represents the size of the file in k bytes. ls –alk specifies the file.
-l List file details.
-n Replace names with numeric UIDs and GIDs. (Introducing UID, GID)
-F Attach a character after each file name to indicate the type of the file. "*" represents an executable ordinary file; "/" represents a directory; "@" represents a symbolic link; "
-r Sort the directory in reverse order.
-t Sort by time.
-s Output the size of the file after the l file name. (Size sorting, how to find the largest file in a directory)
-R List files in all subdirectories. (recursion)
-1 Only one file is output per line.

2. pwd command

Syntax: pwd
Function: Display the directory where the user is currently located

3. cd command

Syntax:cd directory name
Function: Change the working directory. Change the current working directory to the specified directory.
Common options:

Options Function
cd … Return to the upper level directory
cd /home/litao/linux/ absolute path
cd …/day02/ relative path
cd ~ Enter user home directory
cd - Return to recently visited directory

4. touch command

Syntax:touch [options]... File...
Function: The touch command parameters can change the document or The date and time of the directory, including access time and change time, or create a new file that does not exist.
Common options:

Options Function
-a or –time=atime or –time=access or –time=use Only change access time
-c or –no-create No documentation is created
-d Use the specified date and time instead of the current time
-f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
-m or –time=mtime or –time=modify Only change the change time
-r Set the date and time of the specified document or directory to be the same as the date and time of the reference document or directory
-t Use the specified date and time instead of the current time

5. mkdir command

Syntax: mkdir [options] dirname…
Function: Create a directory named Directory of "dirname"
Common options:

Options Function
-p, --parents Can be a path name. At this time, if some directories in the path do not exist yet, after adding this option, the system will automatically create those directories that do not yet exist, that is, multiple directories can be created at one time.

6. rmdir command && rm command

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.
Syntax: rmdir [-p][dirName]
Applicable objects: Have All users with operating permissions for the current directory
Function: Delete empty directories
Common options

Options Function
-p When the subdirectory is deleted, if the parent directory also becomes an empty directory, the parent directory will be deleted together.

The rm command can delete files or directories at the same time
Syntax: rm [-f-i-r-v][dirName/dir]
Applicable objects: All users
Function: Delete files or directories
Common options:

Options Function
-f Even if the file attribute is read-only (i.e. write-protected), it will be deleted directly.
-i Ask for confirmation one by one before deleting
-r Delete the directory and all files under it

7. man command

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is man(man command: yum install -y man-pages)
Syntax: man [option] command
Commonly used options:

Options Function
-k Search online help based on keywords
on one num is only found in chapter num
-a Display all chapters, such as man printf. By default, it starts searching from the first chapter and stops when it knows it. Use the a option. When pressing q to exit, it will continue to search until all chapters are searched.

8.cp command

Syntax: cp [options] Source file or directory Target file or directory
Function: Copy file or directory
Explanation: The cp command is used to copy files or directories, such as specifying two or more files or directories at the same time , and the final destination is an existing directory, it will copy all the files or directories specified previously to this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.
Common options:

Options Function
-f / --force Forcibly copy a file or directory, regardless of whether the destination file or directory already exists
-i / --interactive Ask user before overwriting files
-r Recursive processing, processing files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.
-R / --recursive Recursive processing, processing files and subdirectories in the specified directory together

9. mv command

The mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.
Syntax: mv [options] Source file or directory Target file or directory
Function:

  1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2. 当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。
  3. 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。

常用选项:

选项 功能
-f force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i 若目标文件 (destination) 已经存在时,就会询问是否覆盖

10. cat 、tac指令

语法:cat [选项][文件]
功能: 查看目标文件的内容(文本较小)
常用选项:

选项 功能
-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行
什么都不加 从键盘上读取数据,并回显到显示器上

tac指令:反向查看目标文件的内容

11. more指令

语法:more [选项][文件]
功能:more命令,功能类似 cat (文本较大,防止刷屏,回车翻看)
常用选项:

选项 功能
-n 对输出的所有行编号
q 退出more

12. less指令

less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看但若使用了 less 时,就可以使用 [pageup][pagedown] 等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜。
语法: less [参数] 文件
功能:less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载整个文件。
常用选项:

选项 功能
-i 忽略搜索时的大小写
-N 显示每行的行号
/字符串 向下搜索“字符串”的功能
?字符串 向上搜索“字符串”的功能
n 重复前一个搜索(与 / 或 ? 有关)
N 反向重复前一个搜索(与 / 或 ? 有关)
q quit

13. head指令

语法: head [参数]… [文件]…
功能:head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。
常用选项:

选项 功能
-n<行数> 显示的行数

14. tail指令

语法:ail[必要参数][选择参数][文件]
功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。
常用选项:

选项 功能
-f 循环读取
-n <行数> 显示行数

15. 时间相关的指令

date显示
date 指定格式显示时间: date +%Y:%m:%d
date 用法:date [OPTION]… [+FORMAT]

1. 在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记

常用选项:

选项 功能
%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

2. 在设定时间方面

常用选项:

选项 功能
date -s 设置当前时间,只有root权限才能设置,其他只能查看。
date -s 20080523 设置成20080523,这样会把具体时间设置成空00:00:00
date -s 01:01:01 设置具体时间,不会对日期做更改
date -s “01:01:01 2008-05-23″ 可以设置全部时间
date -s “01:01:01 20080523″ 可以设置全部时间
date -s “2008-05-23 01:01:01″ 可以设置全部时间
date -s “20080523 01:01:01″ 可以设置全部时间

3. 时间戳

时间->时间戳:date +%s
时间戳->时间:date -d@1508749502
Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

16. Cal指令

命令格式: cal [参数][月份][年份]
功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常用选项:

选项 功能
-3 显示系统前一个月,当前月,下一个月的月历
-j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
-y 显示当前年份的日历

17. find指令

语法: find pathname -options
功能: 用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)
常用选项:

选项 功能
-name 按照文件名查找文件

小tips:

  1. Linux下find命令在目录结构中搜索文件,并执行指定的操作。
  2. Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。
  3. 即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。
  4. 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)

18. grep指令

语法: grep [选项] 搜寻字符串 文件
功能: 在文件中搜索字符串,将找到的行打印出来
常用选项:

选项 功能
-i 忽略大小写的不同,所以大小写视为相同
-n 顺便输出行号
-v 反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行

19. zip/unzip指令

Syntax: zip compressed file.zip directory or file
Function: Compress the directory or file into zip format
Common options:

Options Function
-r Recursive processing, processing all files and subdirectories in the specified directory together

20. tar command: pack/unpack, don’t open it, just read the content

Common selection:

Options Function
-c Parameter command to create a compressed file (meaning create);
-x Unpack the parameter command of a compressed file!
-t Check out the files inside the tarfile!
-With Does it also have the attributes of gzip? That is, do I need to use gzip compression?
-j Does it also have attributes of bzip2? That is, do I need to use bzip2 compression?
-in Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
-f When using a file name, please note that the file name must be followed immediately after f! Don't add any more parameters!
-C Unzip to the specified directory

20.bc instruction

The bc command can easily perform floating point operations

21. uname -r command

Syntax: uname [option]
Function: uname is used to obtain information about the computer and operating system Information (uname can display the version of the operating system used by the Linux host, the name of the hardware and other basic information)
Common options:

Options Function
-a or –all Output all information in detail, in order: kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name

22. Several important hotkeys [Tab], [ctrl]-c, [ctrl]-d

Options Function
[Tab] "Command completion" and "File completion" functions
[Ctrl]-c Let the current program "stop"
[Ctrl]-d It usually means: "End Of File, EOF or End OfInput"; in addition, it can also be used to replace exit

23. Shut down

Syntax: shutdown [options]

Options Function
-h After stopping the system services, shut down immediately.
-r After stopping the system services, restart them
-t sec Add the number of seconds after -t, which means "shut down after a few seconds"

24. Extended commands

  1. Installation and login commands: login, shutdown, halt, reboot, install, mount, umount, chsh, exit, last.
  2. File processing commands: file, mkdir, grep, dd, find, mv, ls, diff, cat, ln.
  3. System management related commands: df, top, free, quota, at, lp, adduser, groupadd, kill, crontab.
  4. Network operation commands: ifconfig, ip, ping, netstat, telnet, ftp, route, rlogin, rcp, finger, mail, nslookup.
  5. System security related commands: passwd, su, umask, chgrp, chmod, chown, chattr, sudo ps, who.
  6. its instructions: tar, unzip, gunzip, unarj, mtools, man, unendcode, uudecode.

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/133716919
Recommended