Linux常用命令
Linux常用命令
-
切换用户
su username -
切换目录 cd 如:
cd /home -
查看当前目录下的文件和目录
ll (ls -l的缩写) -
新建/删除文件夹 mkdir/rmdir
mkdir [-m 777] [-p] new_folder1
-m 创建文件夹并授权;-p 创建目录,如果父级目录不存在则一并创建;多个文件夹以空格隔开rmdir [-f] [-r] folder-f 强制删除; -r 递归删除文件夹及其所有内容;多个文件夹以空格隔开 -
隐藏文件以.开头,隐藏文件可以避免用户误操作系统重要文件,如:.filename、.foldername/、.config/、.bashrc
-
查看当前目录还剩多大空间
df -h $(pwd) -
挂载/卸载
mount查看挂载信息
mount [-t ext4] /dev/sdb1 /mnt/mydisk挂载设备到目录 (-t 指定文件系统类型)
umount -f /mnt/mydisk下载指定挂载点 (-f 强制卸载)
umount /dev/sdb1通过设备卸载 (-l 懒卸载)
umount -a卸载所有
umount -a -r -t ext4卸载所有(-r 如果卸载失败,尝试以只读的方式重新挂载;-t 卸载指定类型的文件系统 ) -
创建/删除文件
touch newfilename.txt创建文件,一次可以创建多个,以都好隔开文件名。如果文件名中有空格等符号,则需要加上双引号vim newfilename会打开编辑器,在编辑器中编辑并保存rm [-fiv] filename删除文件(-f 强制删除不提示;-i 询问是否确认删除;-v 显示删除过程) -
更改文件的权限
chmod -R 775 filename给文件属主、所属组、其他用户的权限分别为7、7、5chmod [u/g/o/a] +/-/= [rwx]
= 给指定角色指定权限;- 给指定角色减少权限 ;+ 给指定角色增加权限。u表示文件属主,g表示所属组,o表示其他用户,a表示所有(即包含ugo) -
实时查看文件末尾
tail -f -
过滤文件内容中包含ERROR的行
grep 'ERROR' filename -
grep和find的区别
grep用于在文件中查找匹配的行
find用于在文件管理系统中查找匹配的文件或目录 -
如何在/home目录中查找aaa.log文件
find -name 'aaa.log' /home
-name后的参数可通过正则的方式,如’^a.*.log’表示以a开头的所有.log文件。 -
查看ip地址
ip addr
ifconfig -
查看文件的命令有那些
cat filename一次性展示所有内容
more filename一页一页查看内容,可以向下,不能向上回看
less filename一页一页的查看内容,可以向下看,也可以向上回看
tail -f n filename实时查看文件的后n行
head [-n 20] finename查看文件的前20行,不加-C,默认前10行 -
查看端口号
lsof [-i keyword]查找包含keyword的网络连接netstat -tulnp
-t tcp端口;-u udp端口;-l 仅显示监听端口;-n 以数字形式显示地址和端口,不解- - 析主机名和服务名;-p 显示进程名PID,需要sudo权限ss -tulnp与netstat一样的功能,但是效率更高
sudo lsof -i -P -n | grep LISTEN只查看监听端口 -
查看进程用什么命令
ps -ef | grep 'keyword'查看包含keyword的进程
ps -u username查看指定用户的进程 -
查看CPU、内存使用率(实时进程)
top -d n没n秒刷新一次数据 -
查看cpu信息
cat /proc/cpuinfo -
top命令的数据源是哪里
主要是/proc目录下,如:/proc/stat系统及信息、/proc/meminfo内存信息、/proc/[PID]/进程信息 -
查询出指定进程并杀掉这个进程
ps -ef | grep 'process_name' kill -9 进程ID -
把一个文件夹打包成.tar.gz文件,以及解压该文件的命令
tar -cvfz disfile.tar.gz /sourcefile/path把sourcerfile压缩成 disfile.tar.gz
tar -xvfz disfile.tar.gz [-C /target/path]解压缩到指定目录
-c 创建,即压缩
-x 抽取,就解压 -
以/etc/passwd的前五行为例,提取用户名
cat /etc/passwd | awk -F : 'NR<=5{print $1}' filename冒号为分隔符,打印filename前五行的第五列的内容 -
自动任务
crontab的格式
分 时 日 月 星期 命令
分的范围:0-59 *表示不指定
时的范围:0-23 *表示不指定
日的范围:1-31 *表示不指定
月的范围:1-12 *表示不指定
星期的范围:0-6(周日用0或7表示),*表示不指定星期
如:
0 2 * * * command 每天2点0分执行命令command
0 9 * * 1-5 command 每周一至周五每天9点执行commandcrontab -l查看当前所有任务
crontab -r清空所有任务
crontab -e编辑任务文件
crontab -l > ~/crontab_backup.txt备份当前任务
:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).#
# For example, you can run a backup of all your user accounts# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
#分 时 日 月 周 命令
#每一小时的每一分钟执行一次
#* */1 * * * /home/autoTask.sh >> /home/crontab_debug.log 2>&1
#每一分钟执行一次
#*/1 * * * * /home/autoTask.sh >> /home/crontab_debug.log 2>&1
#每到0分的时候执行一次,即每小时执行一次
0 * * * * /home/autoTask.sh >> /home/crontab_debug.log 2>&1
查看自动任务执行日志
grep -a CRON /var/log/syslog
:~$ grep -a CRON /var/log/syslog | tail
May 29 14:30:01 A25position-pc CRON[536194]: (root) CMD (/opt/kingsoft/wtool-pdf/wpsupdate /from:task)
May 29 14:30:02 A25position-pc CRON[536183]: (CRON) info (No MTA installed, discarding output)
May 29 15:00:01 A25position-pc CRON[591962]: (root) CMD (/opt/kingsoft/wtool-pdf/wpsupdate /from:task)
May 29 15:00:01 A25position-pc CRON[591964]: (A25position) CMD (/home/A25position/learning/autoTask.sh >> /home/A25position/learning/crontab_debug.log 2>&1)
May 29 15:00:01 A25position-pc CRON[591963]: (root) CMD (/opt/kingsoft/wtool/wpsupdate /from:task)
May 29 15:00:01 A25position-pc CRON[591949]: (CRON) info (No MTA installed, discarding output)
May 29 15:17:01 A25position-pc CRON[623611]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
May 29 15:30:01 A25position-pc CRON[647484]: (root) CMD (/opt/kingsoft/wtool-pdf/wpsupdate /from:task)
May 29 15:30:01 A25position-pc CRON[647485]: (root) CMD (/opt/kingsoft/wtool/wpsupdate /from:task)
May 29 15:30:01 A25position-pc CRON[647477]: (CRON) info (No MTA installed, discarding output)
- 软连接和硬链接的区别
- 硬链接指向原文件;软连接存储源文件的路径,并不直接指向原文件。
- 对于硬链接,删除源文件后,硬链接仍可用;对于软连接,删除源文件后,软连接就会失效。
- 硬链接只能链接文件,不能链接目录;软连接可以链接目录,也可以链接文件。
- 记忆点:硬链接是多一条路回家,原来的路坏了,不影响硬链接这条路;软连接是记录家的路径,原来的路坏了,就回不去了。
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐
所有评论(0)