linux 命令记录
linux 查看目录文件列表,学习超好用的 awk 命令
文件大小相关
- ll 命令(ls -l 的别名)
ll
显示字节大小ll -h
以 KB、MB 等为单位进行显示ll -S
按文件大小从大到小的顺序显示ll -t
sort by modification time 按修改时间排序(最新的在最前面)ll -rS
按文件大小倒序显示- 搭配
head
或者tail
命令ll -hS | head
获取当前目录文件大小最大的10个文件ll -hrS | head
获取当前目录文件大小最小的10个文件
如何使用 grep 搜索带空格的字符串
使用 \ 来转义就好啦。比如使用 ifconfig 命令来查看 ipv4 地址 ifconfig | grep inet\
。
超有用的 awk 命令
https://www.geeksforgeeks.org/awk-command-unixlinux-examples/
Syntax:
1awk options 'selection _criteria {action }' input-file > output-file
Options:
1-f program-file : Reads the AWK program source from the file
2 program-file, instead of from the
3 first command line argument.
4-F fs : Use fs for the input field separator
Example:
1ajay manager account 45000
2sunil clerk account 25000
3varun manager sales 50000
4amit manager account 47000
5tarun peon sales 15000
6deepak clerk sales 23000
7sunil peon sales 13000
8satvik director purchase 80000
- Print the lines which match the given pattern.
output:
1awk '/manager/ {print}' employee.txt
1ajay manager account 45000 2varun manager sales 50000 3amit manager account 47000
- Splitting a Line Into Fields: For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.
output:
1awk '{print $1,$4}' employee.txt
1ajay 45000 2sunil 25000 3varun 50000 4amit 47000 5tarun 15000 6deepak 23000 7sunil 13000 8satvik 80000
- macOS 获取某个网段的 ip 地址
output:
1ifconfig | grep 172.16.16 | awk '{print $2}'
1172.16.16.xxx
Built-In Variables In Awk
Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields.
- NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.
- NF: NF command keeps a count of the number of fields within the current input record.
- FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
- RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.
- OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.
- ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.
Use of NR built-in variables (Display Line Number)
1awk '{print NR,$0}' employee.txt
Output:
11 ajay manager account 45000
22 sunil clerk account 25000
33 varun manager sales 50000
44 amit manager account 47000
55 tarun peon sales 15000
66 deepak clerk sales 23000
77 sunil peon sales 13000
88 satvik director purchase 80000
Use of NF built-in variables (Display Last Field)
1awk '{print $1,$NF}' employee.txt
Output:
1ajay 45000
2sunil 25000
3varun 50000
4amit 47000
5tarun 15000
6deepak 23000
7sunil 13000
8satvik 80000
Another use of NR built-in variables (Display Line From 3 to 6)
1awk 'NR==3, NR==6 {print NR,$0}' employee.txt
Output:
13 varun manager sales 50000
24 amit manager account 47000
35 tarun peon sales 15000
46 deepak clerk sales 23000
linux 下查看某一端口被哪个进程占用
方法1: lsof
命令, 即 ls open files
1lsof -i:端口号
方法2: netstat
命令
1netstat -tunpl | grep 端口号
mkdir -p
mkdir -p
是一个在创建目录时使用的命令选项,用于创建所需的父目录。-p
选项会确保在创建指定目录时,如果该目录的任何父目录不存在,将自动创建这些父目录。
例如,如果您要创建一个名为/usr/app/assets/yt-dlp
的目录,但/usr/app/assets
尚不存在,那么使用mkdir -p /usr/app/assets/yt-dlp
命令将同时创建/usr/app/assets
和/usr/app/assets/yt-dlp
目录。
这在自动化脚本中尤其有用,因为您可能不确定所有父目录是否已存在。使用-p
选项可以确保目录结构按预期创建,而无需担心目录是否已存在。
linux 删除用户
在 Linux 中,您可以使用 userdel
命令删除用户。要删除用户,请以 root 用户身份运行以下命令:
1userdel username
将 username
替换为您要删除的用户的用户名。
注意:在删除用户之前,请确保您已经删除了与该用户相关的所有文件和目录,或者将它们分配给其他用户。否则,这些文件和目录可能会变成"无主",在以后可能导致问题。
如果您想在删除用户的同时删除该用户的主目录,请添加 --remove
选项:
1userdel --remove username
这将同时删除用户及其主目录。在执行此命令时要小心,因为删除的文件和目录将无法恢复。
linux 查看所有用户
在 Linux 中,可以通过查看 /etc/passwd
文件来查看所有用户。/etc/passwd
文件包含了系统上所有用户的信息,每行表示一个用户。要查看所有用户,请在终端运行以下命令:
1cat /etc/passwd
这将输出 /etc/passwd
文件的内容。每行的格式为:
1username:x:UID:GID:User Description:home_directory:shell
如果你只需要用户列表,可以使用 awk
或 cut
命令来提取用户名:
1# 使用 awk
2awk -F: '{print $1}' /etc/passwd
3
4# 使用 cut
5cut -d: -f1 /etc/passwd
这两个命令将输出 /etc/passwd
文件中的所有用户名,每个用户名占一行。
请注意,这将列出所有系统用户,包括系统服务和守护进程所使用的用户。这些用户通常不是用于登录和日常工作的普通用户。
linux 获取时间
在 Linux 中获取时间有多种方式,以下是几个常用的方法:
- 使用 date 命令
在终端中输入 date 命令,即可获取当前系统时间:
1date
该命令会输出当前的日期和时间,例如:
1Sun May 9 10:18:45 CST 2023
- 查看 /etc/timezone 文件
在 Linux 中,系统的时区信息通常保存在 /etc/timezone 文件中。可以使用 cat 命令查看该文件的内容,以获取当前系统所在的时区:
1cat /etc/timezone
该命令会输出当前系统所在的时区,例如:
Asia/Shanghai
- 使用 timedatectl 命令
timedatectl 命令可以显示当前系统的时间和时区信息,以及设置系统时间和时区。在终端中输入 timedatectl 命令即可获取当前系统时间:
timedatectl
该命令会输出当前系统的时间和时区信息,例如:
1 Local time: Sun 2023-05-09 10:22:55 CST
2 Universal time: Sun 2023-05-09 02:22:55 UTC
3 Timezone: Asia/Shanghai (CST, +0800)
4 NTP enabled: yes
5NTP synchronized: yes
6 RTC in local TZ: no
7 DST active: n/a
以上是获取 Linux 系统时间的几种常用方法,您可以根据自己的需求选择合适的方法。
which
和 command -v
的区别
which
和 command -v
都是用来查找命令的执行路径的工具,但它们之间存在一些关键的差异。
-
标准化与可移植性:
command
是一个 shell 内建命令,在几乎所有的 POSIX 兼容的 shell(例如bash
,dash
,zsh
等)中都可以使用。因此,它在不同的 shell 和系统之间更为可移植。which
是一个独立的可执行程序,并不是所有的系统上都预装有这个命令。
-
处理别名和函数:
command -v
会返回 shell 别名、函数、关键字或外部命令。如果你查询的命令是一个别名或函数,command -v
会返回该别名或函数的定义。which
通常只关注外部命令。它不会告诉你命令是否是一个别名或函数。
-
搜索路径:
command -v
使用当前 shell 的$PATH
变量。which
在某些系统上可能不会使用当前 shell 的$PATH
。这意味着如果你修改了$PATH
并在同一个 shell 会话中使用which
,你可能不会得到预期的结果。
-
输出:
- 当查询的命令不存在时,
which
通常会输出一个错误消息到标准错误。 command -v
通常会静默地失败。
- 当查询的命令不存在时,
因为这些差异,当编写跨平台的 shell 脚本或查找命令时,通常推荐使用 command -v
而不是 which
,因为前者更为可移植且不受外部命令影响。
curl 使用代理
使用 -x
或 --proxy
选项
HTTP 代理
1curl -x http://proxyserver:port http://example.com
SOCKS 代理
curl
支持 SOCKS4 和 SOCKS5 代理。你可以使用下列方式之一:
1# SOCKS4
2curl --proxy socks4://proxyserver:port http://example.com
3
4# SOCKS5
5curl --proxy socks5://proxyserver:port http://example.com
6
7# SOCKS5 with hostname resolution via proxy
8curl --proxy socks5h://proxyserver:port http://example.com