Linux常用command笔记

Linux是目前应用最广泛的服务器操作系统,基于Unix,开源免费,由于系统的稳定性和安全性,市场占有率很高,几乎成为程序代码运行的最佳系统环境。linux不仅可以长时间的运行我们编写的程序代码,还可以安装在各种计算机硬件设备中,如手机Android、路由器OpenWRT等,最底层就是运行在linux系统上的。
我安装的Linux发行版本是Ubuntu 16.04和Ubuntu 18.04
下面记录一些使用过程中遇到的Linux命令:

linux的目录结构

/ 下级目录结构

  • bin (binaries)存放二进制可执行文件
  • sbin (super user binaries)存放二进制可执行文件,只有root才能访问
  • etc (etcetera)存放系统配置文件
  • usr (unix shared resources)用于存放共享的系统资源
  • home 存放用户文件的根目录
  • root 超级用户目录
  • dev (devices)用于存放设备文件
  • lib (library)存放跟文件系统中的程序运行所需要的共享库及内核模块
  • mnt (mount)系统管理员安装临时文件系统的安装点
  • boot 存放用于系统引导时使用的各种文件
  • tmp (temporary)用于存放各种临时文件
  • var (variable)用于存放运行时需要改变数据的文件

linux常用命令

新建用户/组并赋予docker权限

1
2
3
4
5
6
7
8
9
10
11
12
13
# Add a New User in Linux
adduser $USERNAME
passwd $USERNAME

# 创建docker用户组
sudo groupadd docker
# 应用用户加入docker用户组
sudo usermod -aG docker ${USER}
# 重启docker服务
sudo systemctl restart docker
# 切换或者退出当前账户再从新登入
su root 切换到root用户
su ${USER} 再切换到原来的应用用户以上配置才生效

查看用户所属group: groups ${USER}

ls: list

ls -l: a long listing format
ls -a: 显示所有文件,包括隐藏文件和目录(所有以“.”为开始的文件和目录为隐藏文件)
ls -la: 是列出当前目录中的所有文件和目录,包括隐藏文件和目录
ls -lh: human readable long listing format
ls -lSh: sorted by size and then by the exist time

ln 链接

ln -s 源文件或目录 目标文件或目录 软链接

cd: change directory

pwd: print name of current/working directory

删除软件

1
sudo apt-get remove <application_name>
1
sudo apt-get purge <package-name>

解压*.tar.xz文件

-c: 建立压缩档案
-x:解压 –extract
-t:查看内容
-r:向压缩归档文件末尾追加文件
-u:更新原压缩包中的文件

这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。

-z:有gzip属性的
-j:有bz2属性的
-Z:有compress属性的
-v:显示所有过程
-O:将文件解开到标准输出

下面的参数-f是必须的

-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接filename。

1
2
3
4
5
6
7
8
tar -xvf file.tar //解压 tar包
tar -xzvf file.tar.gz //解压tar.gz
tar -xjvf file.tar.bz2 //解压 tar.bz2
tar -xZvf file.tar.Z //解压tar.Z
unrar e file.rar //解压rar
unzip file.zip //解压zip
`tar -xf file.tar.xz`
gunzip file.gz //解压gz文件

https://www.cnblogs.com/nhdlb/p/11568991.html
https://scottlinux.com/2014/01/07/extracting-or-uncompressing-tar-xz-files-in-linux/

压缩文件成*.tar.xz

tar -zcvf archive-name.tar.gz directory-name

删除目录或文件

删除空文件夹:rmdir directoryname
Remove a directory with files and subdirectories (non-empty directory):rm -r directoryname
Remove a single file:rm file.txt

mv移动或重命名文件或目录

mv can do two jobs.

  1. It can move files or directories
  2. It can rename files or directories

To just rename a file or directory type this in Terminal:

mv old_name new_name

with space between the old and new names.

To move a file or directory type this in Terminal.

mv file_name ~/Desktop

it will move the file to the desktop.

If is a directory you should add -R before the directory name:

mv -R directory_name ~/Desktop

wget下载命令

https://www.cnblogs.com/wuheng1991/p/5332764.html

wget http://cn.wordpress.org/wordpress-3.1-zh_CN.zip

cp复制命令

cp filename direction
cp folder direction

新建文件

vi filename :打开或新建文件,并将光标置于第一行首

查看显卡型号:lspci |grep VGA (lspci是linux查看硬件信息的命令),屏幕会打印出主机的集显几独显信息
这里写图片描述

查看nvidia芯片信息:lspci |grep -i nvidia,会打印出nvidia系列的硬件信息,如果主机安装了没有视频输出的GPU(如tesla系列),这个命令会很有用
这里写图片描述

du: DiskUsage

查看硬盘使用情况
df -hl 查看磁盘剩余空间
df -h 查看每个根路径的分区大小;-h:human readable
du -sh [目录名] :Disk Usage返回该目录的大小
du -sm [文件夹] 返回该文件夹总M数
这里写图片描述

df: Disk space being used by File ystem

df -h: human readable

man: view the system’s reference MANuals

man pwd: get the manual for PWD command

top

查看CPU使用情况
top
这里写图片描述
top 1
这里写图片描述

tail 命令

https://www.cnblogs.com/mfryf/p/3336804.html
tail -f filename:监视filename文件的尾部内容(默认10行,相当于增加参数 -n 10),刷新显示在屏幕上。退出,按下CTRL+C。

tail -n 20 filename:显示filename最后20行。

tail -r -n 10 filename:逆序显示filename最后10行。

pwd

print working directory

http://linux.51yip.com/search/awk

md5sum : MD5算法一般用于检查文件完整性,不同的文件内容生成相同的报文摘要的概率是极其小的。

md5sum filename
这里写图片描述

scp

secure copy,远程拷贝文件

  1. 将本地文件上传到服务器上
    scp -P 2222 /home/lnmp0.4.tar.gz root@www.vpser.net:/root/lnmp0.4.tar.gz
    这里写图片描述

unzip

解压zip文件
unzip file.zip
unzip file.zip -d destination_folder

diff

比较文件不同
diff file1 file2

taskset

指定job在哪几块CPU上运行
taskset -c 0-7:指定job在1-8号CPU上运行

nohup

allows to run job in the background after you log out from a shell
Nohup Command in Linux

cat

catenate 命令用于连接文件并打印到标准输出设备上
cat file.txt:将file.txt的内容打印在屏幕上
13 Basic Cat Command Examples in Linux

touch

创建空文件,或者改变文件的时间戳属性
touch file.txt:创建一个新的空文件file.txt

visudo

添加sudo用户

查看NVIDIA显卡信息

由于我已经切换到NVIDIA专有驱动:nvidia-smi
驱动

nvidia-smi

watch -n 5 nvidia-smi:每隔5秒更新一下显卡使用情况, ctrl+c退出

more

more 允许你向前查看文本文件。
more file.txt:创建一个新的空文件file.txt
使用Enter可以向下翻页,输入 q 可以退出,输入 / 字符并在其后加上你想要查找的文字(区分大小写)可以搜索。例如你要查看的字段是 “terminal”,只需输入:/terminal

管道符|

将管道符|左边命令的输出输入给右边的命令
ls | more:有很多文件的目录,可以组合 more 跟 ls 命令完整查看这个目录当中的内容
grep ‘productivity’ core.md Dict.md lctt2014.md lctt2016.md lctt2018.md README.md | more :组合 more 和 grep 命令,实现在多个文件中找到指定的文本 “productivity”
ps -u Hailin | more:列出你用户下(Hailin)正在运行的进程

查看Linux系统的发行版(Ubuntu还是CentOS)

$ cat /etc/os-release
$ uname -a:显示全部的版本信息

find

$ find ~ -name '*jpg':在主目录中找到所有的 JPEG 文件。 -name 参数允许你将结果限制为与给定模式匹配的文件
$ find ~ -iname '*jpg':-iname 就像 -name,但是不区分大小写。
$ find ~ ( -iname 'jpeg' -o -iname 'jpg' ):用“或”(表示为 -o)来组合。在主目录中找到所有的 JPEG或JPG 文件
$ find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f:指定查找类型为file文件
$ find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d:指定查找类型为directory目录
$ find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7:指定修改日期在7天以内的file
你可以根据文件状态更改时间 (ctime)、修改时间 (mtime) 或访问时间 (atime) 来执行时间过滤。 这些是在几天内,所以如果你想要更细粒度的控制,你可以表示为在几分钟内(分别是 cmin、mmin 和 amin)。 除非你确切地知道你想要的时间,否则你可能会在 + (大于)或 - (小于)的后面加上数字。
$ find /var/log -size +1G:在 log 目录下找到所有“大于 1GB”的文件
$ find /data -owner hailin:在 /data 中找到 hailin 拥有的所有文件
$ find ~ -perm -o=r:在主目录中找到对所有人可读的文件
How to use FIND in Linux

awk

awk是一种编程语言,用于在linux/unix下对文本和数据进行处理,数据可以来自标准输入、一个或多个文件。其名称得自于它的创始人Alfred Aho、Peter Weinberger 和 Brian Kernighan姓氏的首个字母。
$ awk '!seen[$9]++ { print $4,$5,$9 }' part-00000 part*:输出文件的第4,5,9列,并按第九列内容去重
awk命令详解

sed: Stream EDitor

$sed -i -e 's/abc/ABC/g' feature_list.csv: 将文件中的abc全局替换为ABC
$sed -e 's/$/hello/' -i feature_list.csv: 为每一行末尾append hello
$sed -i '/#/d' feature_list.csv: remove包含#的每一行
Linux sed 命令

设置开机自启动程序

  1. Run this command:sudo vim /etc/systemd/system/YOUR_SERVICE_NAME.service
  2. 输入以下的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Description=GIVE_YOUR_SERVICE_A_DESCRIPTION

Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=YOUR_COMMAND_HERE
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target
  1. 重启服务:sudo systemctl daemon-reload
  2. 启用服务:sudo systemctl enable YOUR_SERVICE_NAME
  3. 开始服务:sudo systemctl start YOUR_SERVICE_NAME
  4. 检查服务状态:systemctl status YOUR_SERVICE_NAME
  5. 尝试重启设备检查服务有没有重新开始运行

Linux查看端口占用情况

1、lsof(list open files)列出当前系统打开文件

1
2
3
4
# Centos
yum install lsof -y
# Ubuntu
apt-get install -y lsof

查看指定端口号语法格式:

lsof -i:端口号

1
2
3
[root@localhost ~]# lsof -i:7075
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 17394 root 4u IPv6 75775 0t0 TCP *:7075 (LISTEN)

查看所有端口

1
2
3
4
5
6
7
8
[root@localhost ~]# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1105 root 3u IPv4 18473 0t0 TCP *:ssh (LISTEN)
sshd 1105 root 4u IPv6 18482 0t0 TCP *:ssh (LISTEN)
master 1904 root 13u IPv4 19162 0t0 TCP localhost:smtp (LISTEN)
master 1904 root 14u IPv6 19163 0t0 TCP localhost:smtp (LISTEN)
openvpn 2411 root 6u IPv4 21972 0t0 TCP 192.168.1.56:openvpn (LISTEN)
sshd 3172 root 3u IPv4 33620 0t0 TCP 192.168.1.56:ssh->192.168.2.113:53585 (ESTABLISHED)

2、 netstat

1
2
# Ubuntu
apt-get install -y net-tools

netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况
netstat 查看端口占用语法格式:
netstat -tunlp | grep 端口号

  • -t (tcp) 仅显示tcp相关选项
  • -u (udp)仅显示udp相关选项
  • -n 拒绝显示别名,能显示数字的全部转化为数字keygen
  • -l 仅列出在Listen(监听)的服务状态
  • -p 显示建立相关链接的程序名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@recall-gpu-01:~/fuhailin/projects/opt# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:33719 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:35709 0.0.0.0:* LISTEN 5237/java
tcp 0 0 127.0.0.1:8125 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:19999 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:9090 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:2182 0.0.0.0:* LISTEN 5237/java
tcp6 0 0 :::7895 :::* LISTEN -
tcp6 0 0 :::7896 :::* LISTEN -
tcp6 0 0 :::7897 :::* LISTEN -
udp 0 0 0.0.0.0:68 0.0.0.0:* -
udp 0 0 127.0.0.1:323 0.0.0.0:* -
udp 0 0 127.0.0.1:8125 0.0.0.0:* -
udp6 0 0 ::1:323 :::* -
udp6 0 0 :::7895 :::* -
udp6 0 0 :::7897 :::* -

3、 ps(process selection 用于显示当前进程的状态)

1
2
3
4
# Ubuntu
apt-get install -y procps
# Centos
yum install -y procps

使用PS命令查看和控制进程

ps 显示本用户的进程

1
2
3
4
[root@localhost ~]# ps
PID TTY TIME CMD
30059 pts/0 00:00:00 bash
31149 pts/0 00:00:00 ps

ps -au 显示所有用户的进程

1
2
3
4
5
[root@localhost ~]# ps -au
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3319 0.0 0.0 110092 860 tty1 Ss+ 07:28 0:00 /sbin/agetty --noclear tty1 linux
root 21611 0.0 0.0 115408 1912 pts/0 Ss 16:13 0:00 -bash
root 21640 0.0 0.0 155332 1776 pts/0 R+ 16:15 0:00 ps -au

ps aux 显示所有进程和其状态

1
2
3
4
5
root@recall-gpu-01:~# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 5976 2328 pts/0 Ss 08:58 0:00 bash
root 5237 0.1 0.1 6685904 116328 pts/0 Sl 09:17 0:08 java -Xmx512M -Xms512M -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlineLevel=15 -Djava.awt.headless=true -Xlog:gc*:file=/roo
root 6295 0.0 0.0 8576 1624 pts/0 R+ 10:27 0:00 ps aux

ps -aux | grep 服务 显示指定进程和其状态

1
2
3
4
5
6
[root@localhost ~]# ps -aux | grep httpd
root 31122 0.0 0.0 112680 696 pts/0 S+ 14:01 0:00 grep --color=auto httpd
[root@localhost ~]#
[root@localhost ~]# ps -aux | grep vsftpd
root 31124 0.0 0.0 112680 696 pts/0 S+ 14:01 0:00 grep --color=auto vsftpd
[root@localhost ~]#

ps -ef

-e: Displays all processes.

-f: view full-format listing.

1
ps -C rclone -o pid= | xargs -r kill

How to pass output of ps x | grep rclone to kill command?

1
2
3
4
5
6
7
8
9
10
$ ps x | grep rclone
7111 ? Sl 0:00 rclone mount xxx
7112 ? Sl 0:00 rclone mount xxx
7113 ? Sl 10:16 rclone mount xxx
9843 pts/1 S+ 0:00 grep --color=auto rclone

$ killall tf_model_server or pkill rclone

$ ps -C tf_model_server -o pid= | xargs -r kill -9

4、pidof(查看服务的pid)

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# ps aux | grep nginx
root 11579 0.0 0.0 112704 956 pts/2 S+ 16:45 0:00 grep --color=auto nginx
root 27326 0.0 0.0 56712 1264 ? Ss Jun26 0:00 nginx: master process /usr/sbin/nginx
root 27327 0.0 0.0 57252 2652 ? S Jun26 0:00 nginx: worker process
[root@localhost ~]#
[root@localhost ~]# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27326/nginx
[root@localhost ~]#
[root@localhost ~]# pidof nginx
27327 27326

dpkg -L package name (List the file location)

5、nload(查看网卡io速度)

nload eth0


BASIC LINUX COMMAND
Linux Cheat Sheet David Relyea | PDF
Introduction to Linux Commands/Shell Scripting for Data Scientists | YouTube
Linux 下的这些高效指令,是你快速入门运维的神器
Linux最常用命令:简单易学,但能解决95%以上的问题