linux常用命令详解


总结了常用的近30个linux命令及小技巧,把这些命令学了就可以对linux进行基本的操作了。

man

查看命令的使用手册,当对命令参数或格式不确定时可以查看。

  • 格式: man <命令>

快捷键:

  • /<关键字> 从上往下搜索
  • ?<关键字> 从下往上搜索
  • n和N 在搜索时定位到下一个关键字和上一个关键字

cd(change direction)

切换目录路径

cd ..	# 返回上一级
cd - 	# 切换到上次访问路径
cd ~ 	# 切换到家目录

cp(copy)

拷贝文件、文件夹。
参数:

  • -r 复制时递归进行, 常用与复制目录
  • -a 在复制时保留文件属性, 相当于参数组合-pdr
  • -t 复制到执行的目录, 通常cp的第二个参数为复制到的目的路径, 但当省略第一个参数是, 需要用-t来指定目的路径
  • -d 若对象为"链接", 则保留"链接"文件属性,即复制后仍为"链接"文件
  • -p 复制时保留文件原属性

小技巧

cp a.conf{,.bak}	# 拷贝配置文件,命令等价于cp a.conf a.conf.bak
# 效果
[root@lqc ~]# cp a.conf{,.bak}
[root@lqc ~]# ll
-rw-r--r--  1 root root   14 Aug  1 21:35 a.conf
-rw-r--r--  1 root root   14 Aug  1 21:41 a.conf.bak

mv

移动目录、文件夹。
格式:mv <src_dir> <dest_dir>

小技巧

mv ./a.conf{,.bak}	# 把文件设为备份
mv ./a.conf{.bak,}	# 还原备份

rm

删除文件、文件夹。危险命令!重要数据、配置文件删前记得备份。
格式:rm <option> <file/dir>rm <file/dir> <option>
参数:

  • -r 递归删除,用于删除目录
  • -f 强制删除

小技巧
在重要的服务器修改配置文件或者删除数据是很危险的,笔者曾经就把项目的数据库文件给误删了(rm -rf ./*), 幸好之前有意识备份了一下,否则要被骂惨了。通常在执行rm命令时不能直接执行,以提示用户此操作危险。因此常设置rm别名(alias)。

# 设置别名
alias rm="echo 操作危险,禁止直接执行"# 真正执行删除
\rm a.txt
/bin/rm a.txt

ls (list)

查看文件夹下的目录和文件。
参数:

  • -l 列表显示目录信息
  • -h human->h存储单位格式化,以M、G等人类易读单位显示
  • -i 在列表信息中显示inode
  • -t 列表信息以时间顺序显示
  • -tr 列表信息以时间逆序(time reverse)显示

常用:

ls -ilh
inode	  权限      硬链接数 属主 属组 大小    时间     文件/文件夹名称
34111974 drwxr-xr-x.  3 root root  101 Jul 21 10:36 abrt
17446237 -rw-r--r--.  1 root root   16 Jul 21 10:39 adjtime
16778297 -rw-r--r--.  1 root root 1.5K Apr  1 12:29 aliases
16777285 -rw-r--r--.  1 root root  12K Jul 21 10:40 aliases.db
   90884 drwxr-xr-x.  2 root root  261 Jul 21 15:42 alternatives
17470583 -rw-------.  1 root root  541 Aug  9  2019 anacrontab

cat

查看文件信息。
格式:cat <file>
使用

# 在文件尾部追加信息, xxxx为内容
cat >a.txt<EOF
xxxx
xxxx
xxxx
EOF

# 查看信息
cat a.txt
[root@lqc ~]# cat a.txt
slkdjfl
123
456

小技巧
通过管道符 | 把第一个命令的输出做为grep的输入来过滤内容。里的cat命令用来模拟标准输出。其实,grep命令可以直接对文本文件进行过滤,见图2。
grep是个强大的命令,linux三剑客之一,linux玩的这个命令必须熟练掌握,学习可点另一篇文章 linux三剑客
在这里插入图片描述
在这里插入图片描述

alias

为命令设置别名,在shell中通过命令设置是暂时的,只有在/etc/profile或/etc/bashrc中设置才是永久生效的。
示例:alias rm="echo 禁止直接删除"
取消别名: unalias <别名>

使用

#查看别名
[root@lqc ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

#设置别名
[root@lqc ~]# alias rm='禁止使用rm执行删除'
#进行查看
[root@lqc ~]# alias  
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='禁止使用rm执行删除'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

#删除别名
[root@lqc ~]# unalias rm
#查看
[root@lqc ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

which

查看命令文件路径,仅用户可执行文件。
格式:which <cmd>

[root@lqc ~]# which rm
/usr/bin/rm
[root@lqc ~]# which alias
/usr/bin/alias

whereis

查看命令路径详细信息
格式:whereis <cmd>

[root@lqc ~]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
[root@lqc ~]# whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
[root@lqc ~]# whereis yum
yum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8

source

重新加载shell配置文件。

常用可加载配置文件:

  • /etc/profile 系统变量、用户定义变量
  • /etc/bashrc 系统函数
  • ~/.bash_profile
  • ~/.bashrc

systemctl 重要

管理程序服务运行状态。
常用:network httpd
格式:systemctl <cmd> <service_name>
常用命令:

  • 启动服务 start,示例: systemctl start <service_name>
  • 停止服务 stop
  • 重启服务 restart
  • 查看服务运行状态 status
  • 让服务开机自启 enable
  • 关闭开机自启服务 disable
  • 确认服务是否运行 is-active
  • 确认服务是否开机自启 is-enable

使用
以邮件服务postfix为例

# postfix开始状态
[root@lqc ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-08-26 15:49:52 CST; 33min ago
  Process: 1253 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1249 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1243 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 1325 (master)
   CGroup: /system.slice/postfix.service
           ├─1325 /usr/libexec/postfix/master -w
           ├─1326 pickup -l -t unix -u
           └─1327 qmgr -l -t unix -u

Aug 26 15:49:50 lqc.com systemd[1]: Starting Postfix Mail Transport Agent...
Aug 26 15:49:52 lqc.com postfix/postfix-script[1323]: starting the Postfix mail system
Aug 26 15:49:52 lqc.com postfix/master[1325]: daemon started -- version 2.10.1, configura...ix
Aug 26 15:49:52 lqc.com systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.

# 停止postfix服务
[root@lqc ~]# systemctl stop postfix
# 查看状态
[root@lqc ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2020-08-26 16:24:03 CST; 3s ago
  Process: 1642 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 1253 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1249 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1243 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 1325 (code=killed, signal=TERM)

Aug 26 15:49:50 lqc.com systemd[1]: Starting Postfix Mail Transport Agent...
Aug 26 15:49:52 lqc.com postfix/postfix-script[1323]: starting the Postfix mail system
Aug 26 15:49:52 lqc.com postfix/master[1325]: daemon started -- version 2.10.1, configura...ix
Aug 26 15:49:52 lqc.com systemd[1]: Started Postfix Mail Transport Agent.
Aug 26 16:24:03 lqc.com systemd[1]: Stopping Postfix Mail Transport Agent...
Aug 26 16:24:03 lqc.com systemd[1]: Stopped Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.

# 禁止postfix开机自启
[root@lqc ~]# systemctl disable postfix
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.

# 开启postfix服务
[root@lqc ~]# systemctl start postfix

ln

创建链接文件。分为软链接和硬链接。
格式:

  • ln <src> <dest> 把src链接到dest,不加参数默认为硬链接,注意:硬链接只能针对文件
  • ln -s <src> <dest> 把src链接到dest,软链接文件、目录均可链接。软链接就像一个快捷方式。

软硬链接区别:
在这里插入图片描述
从图中可以看导硬链接更像文件的一个副本,当删除文件时文件不会消失,我们还可以通过硬链接文件访问。而软链接指向文件删除后文件删除后,文件索引信息消失,文件等同删除,当再次往软链接写入文件,会新建与指向文件同名的文件,但二者已经不是一个文件。
使用

# 创建测试文件
[root@lqc ~/test]# touch a.txt
[root@lqc ~/test]# echo "hello world" > a.txt
[root@lqc ~/test]# cat a.txt
hello world

# 创建软链接
[root@lqc ~/test]# ln -s /root/test/a.txt /root/test/a_softLink.txt
[root@lqc ~/test]# ll
total 4
lrwxrwxrwx 1 root root 16 Aug 26 16:30 a_softLink.txt -> /root/test/a.txt
-rw-r--r-- 1 root root 12 Aug 26 16:29 a.txt

#创建硬链接
[root@lqc ~/test]# ln /root/test/a.txt /root/test/a_hardLink.txt
[root@lqc ~/test]# ll
total 8
-rw-r--r-- 2 root root 12 Aug 26 16:29 a_hardLink.txt
lrwxrwxrwx 1 root root 16 Aug 26 16:30 a_softLink.txt -> /root/test/a.txt
-rw-r--r-- 2 root root 12 Aug 26 16:29 a.txt

ss与netstat

二者功能相同,都是查看TOC、UDP接口情况。在centos7中不能直接使用netstat,需要下载net-tools软件包。
参数:

  • -l list — 列表显示网络服务信息
  • -n number — 以数字方式进行显示服务好,例如ssh显示为22
  • -t tcp — 网络协议
  • -u udp — 网络协议
  • -p process — 显示服务进程信息

常用&小技巧

netstat -lntup

# 显示的同时过滤信息
netstat -lntup  |grep <关键字>

tail head

tail显示尾部的分信息,head显示头部的。这个命令经常用来查看日志文件(/var/log/…)
参数:-n 显示多少行,默认10行。

# 示例,head相同
tail -n 100 /var/log/secure 

在这里插入图片描述

yum

包管理工具,下载,安装软件很方便。
安装软件: yum install <pkg_name> -y -y交互时自动选择yes
查看主机安装软件包: yum grouplist
安装未安装的软件包: yum groupinstall -y <未装软件包名>
在repo中搜索相关信息: yum provides <key_val>

常用:

yum repolist all				#列出所有仓库
yum list all					#列出仓库中所有软件包
yum info  <pkg-name>			#查看软件包名称
yum install <pkg-name>			#安装软件包
yum reinstall <pkg-name>		#重新安装软件包
yum update <pkg-name>			#更新软件包
yum remove  <pkg-name>			#移除软件包
yum clean all					#清除所有仓库缓存
yum check-update				#检查可更新的软件包
yum grouplist 					#查看系统中已经安装的软件包租
yum groupinstall <pkg-group>	#安装指定软件包租
yum groupremove <pkg-group>		#移除指定软件包租
yum groupinfo <pkg-group>		#查询指定的软件包租信息

rpm

centos、readhat包管理工具,提供包的安装、查询、更新等管理功能。
参数:

  • -q 查询
  • -a 所有
  • -l 列表显示
  • -f 文件
  • -i install 安装

常用:

rpm -qa  <filename>		#在系统中所有安装包查询是否安装
rpm -ql  <filename>		#查询安装包路径
rpm -qf <filename>		#查询命令属于哪个包
rpm -ivh <file.rpm>		#安装软件
rpm -Uvh <file.rpm>		#更新软件
rpm -e <filename>		#卸载软件
rpm -qpi <filename>		#列出软件描述信息
rpm -qpl  <filename> 	#列出软件文件信息的命令格式

sh、bash

运行shell脚本。
格式:sh <shell_script>

free

查看内存使用信息。
使用:

free -h

ps

查看进程信息.
常用:

ps <pid> 	# 查看指定线程信息
ps -ef 		#查看进程详细信息, 常配合grep使用

在这里插入图片描述

top、升级版htop

查看进程信息,相当于win任务管理器,直接输入命令进行使用
在这里插入图片描述
在这里插入图片描述

tar

格式:

  • tar -zcvf <压缩文件路径(文件名后缀.tar.gz)> <压缩源src> 压缩
  • tar -xvf <extract_dir> <dir_file_tar.gz> 解压
    注意:压缩时尽量用相对路径,否则有异常提示

压缩命令:

tar -zcvf   aaa.tar.gz  ./* 		#压缩当前目录下所有文件
tar -acvf aaa.tar.gz /opt/data/*	#压缩指定路径下文件

# 压缩时排除
tar -zcvf <dir_file_tar.gz>   <src_dir> --exclude-from=<exclude_dir/file>

# 压缩时批量排除
tar -zcvf <dir_file_tar.gz>  <src_dir> --exclude-from=<exclude_file>

# 多个h参数,把链接指向文件/目录找到并压缩
tar -zcvhf   <dir_file_tar.gz>  <src> <src>....

参数:

  • -z 压缩方式zip
  • -c 小写字母,create 创建压缩文件
  • -C 大写字母,指定到解压目录, 默认当前目录
  • -v 显示压缩过程
  • -x extract 提取–解压
  • -f 指定压缩文件路径信息
  • -t list the contents of an archive
  • -p 保持原来的权限和与属性
  • -h 备份文件是链接文件时找到源文件并压缩

检查是否压缩成功:

  • diff
  • vimdiff
  • tar tf <archieve_file_tar.gz>

列表列出压缩包内容: diff/vimdiff <file_1> <file_1>

注意:

  • 当以绝对路径指定压缩源文件时,默认将跟路径“/”去掉并输出提示信息,常用相对路径
  • –exclude-from 可以是排除目录或包含排除目录的文件,可以用find查找生成

tree

层级显示目录。
格式:tree <dir>

参数:

  • 不参数,将递归显示文件和目录
  • -L level 显示层级数
  • -d direct,仅显示目录

find

在目录下查找文件/目录。
格式:find <dir> -type <d/f> -name <d/f name>
参数:

  • -type 指明查找类型,d 目录,f 文件
  • -name 指定匹配名称的关键字,可以使用*模糊匹配

示例:

find / -type f -name "*nginx*" # 在/目录下查找名称包含nginx的文件

grep

在文本文件或管道输入流等中查找关键信息。
格式:grep [option] <key_str> <src>
示例:

history | grep "rm"		# 在执行命令历史中查找rm命令
grep -E "200$" ./a.txt	# 在a.txt文件中找出200结尾的行

useradd

创建用户
格式: useradd [option] 用户名
常用:

# 添加普通用户,并设置密码
echo "passwd" | useradd  normal_user --password --stdin
useradd normal_user
passwd normal_user

# 添加虚拟用户, nginx为用户名
useradd nginx -M -s /sbin/shell

passwd

以交互的方式设置密码,直接使用passwd是修改当前用户的登录密码,passwd加用户名修改指定用户的密码(root操作)。

chmod

修改文件、文件夹的权限。
常用参数:-R递归修改权限

chmod nnn <file/dir>		#直接赋予权限 , chmod 777赋予所有者,用户组,其他用户权力
chmod +<r/w/x> <file/dir>	#赋予所有者、用户组、其用户权力
chmod -<r/w/x> <file/dir> 	#移除所有者、用户组、其他用户权力
chomod <u/g/o=(+/-)(r/w/x)> <file/dir> 	# u user,g group, o other 示例:chmod u=+x 给user组设置运行权限

# 给予所有权限
chmod -R 777 <dir/file>

chown

修改文件、文件夹的属主数组。
常用参数:-R递归修改权限
示例:

# 两命令功能相同
chown -R nginx.nginx  ./nginx/html	# 为html文件夹及其子文件夹、文件设置属主数组为nignx
chown -R nginx:nginx  ./nginx/html	# 为html文件夹及其子文件夹、文件设置属主数组为nignx

其他文章


想了解作者更多,请移步我的个人网站,欢迎交流、留言~
极客技术空间:https://elltor.com/

  • 14
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值