Linux common commands

WBOY
Release: 2024-02-19 20:57:03
forward
1244 people have browsed it

Linux common commands

Command 1: pwd

pwd displays the current working path and checks the location.

[root@bunian ~]# pwd
/root
Copy after login

Command 2: ls

The ls command is very commonly used. ls refers to list, a command to view files or directories. Commonly used after adding parameters:

  • ls: View all files in the directory
  • ls -l: Display all information in files and directories
  • ls -a: List all files, including hidden files, where a means all
  • ls -R: List all files in the subdirectory, which is equivalent to recursively listing all the contents, which means that all files in the directory will be displayed
  • ls [0-9]: Display file names and directory names containing numbers

Command 3: cp

cp means copy and is used to copy files. This command can also copy multiple files to the same directory at one time

  • cp -a: Copy the characteristics of the file together
  • cp -p: Copy together with the attributes of the file, similar to the -a parameter above, often used for backup
  • cp -i: If the target file exists, ask before overwriting
  • cp -r: Recursive continuous copy
  • cp -u: Copying will only occur when there are differences between the target file and the source file

Command 4: mv

mv means move, which is used to move files, directories or change file names

  • mv -f: f refers to force, no inquiry will be made before overwriting
  • mv -i: Ask before overwriting
  • mv -u: The target file will be updated only when it is newer than the source file
mv 旧文件名 新文件名-- 重命名
mv hello.txt /home/peter-- 移动位置
mv /dir1 /dir2-- 将目录dir1移动到目录dir2中,前提是dir2已经存在,若不存在则改名
mv /dir1/* . -- 将dir1下面的全部文件(*代表全部)移动到当前目录下
Copy after login

Command 5: rm

rm means remove, which is used to delete files or directories

  • rm -f: Forced deletion of content without any warning content
  • rm -i: Interactive mode, ask whether to delete before deleting
  • rm -r: Recursive deletion, most commonly used to delete all contents in a directory

PS: Use this command with caution! ! !

Command 6: cd

The cd command represents switching directories and can use relative or absolute paths as parameters.

Several commonly used commands for switching paths:

  • cd /home/user: Switch to the "/home/user" directory
  • cd: Switch to personal home directory
  • cd ..: Return to the previous directory
  • cd ../..: Return to the directory two levels above
  • cd -: Return to the last directory
  • cd ~: Return to the user’s home directory

It should be noted that the parent directory of the root directory is itself

Special symbols Function
~ Represents the home directory of the currently logged in user
~Username Indicates switching to the home directory of the specified user
represents the directory where you were last located
. represents the current directory
.. Represents the superior directory

Command 7: mkdir

mkdir refers to make directory, which means creating a directory. If the directory exists, an error will be reported.

mkdir test# 创建空白的test目录
mkdir -p test/a/b/c/d# 使用-p参数来递归地创建多个层级目录
Copy after login

Command 8: touch

The touch command is used to create a blank file or set the time of the file.

Creating a file is simple:

touch bunian.txt  # 创建bunian.txt文件
Copy after login

The time to set the file is mainly reflected in:

  • Set the modification time of file content: mtime
  • File permission or attribute change time: ctime
  • File reading time: atime

The corresponding parameters are:

parameter effect
-a Only modify the "read time" atime
-m Only modify the "modification time" mtime
-d Modify both simultaneously

命令9:cat

cat命令主要是用来查看文件的内容,后面跟上我们的文件名即可。通常可以用管道符和命令more或者less进行连用。常用的参数选项:

  • cat filename:查看文件的全部内容
  • cat -n  filename :将文件的行数全部显示出来,包含空行
  • cat -b filename:和-n类似,只是不显示空行
  • cat -s filename:当遇到有连续两行以上的空白行,就代换为一行的空白行
  • cat -E:在每行的结尾显示$

命令10:more

more命令的功能和cat命令是类似的,只不过是常用来显示一个长文件,它是以全屏的方式按照分页的方式显示内容。cat命令是整个文件的内容显示在屏幕上,more命令是以分页的方式来显示的。

常用的操作:空白键space跳到下一页,b键则返回上一页。

常用的参数为:

  • +n :从第n开始显示文件内容
  • -n :屏幕只显示n行数
  • -s:将连续的空行显示为一行
  • -u:将文件内容中的下划线去掉
  • -c:不进行滚屏操作。每次刷新这个屏幕
  • -l:忽略Ctrl+l换页字符
  • +/pattern:在每个文档显示前搜寻该字串(pattern),然后从该字串之后开始显示

配合该命令的常用操作:

  • Enter n:向下翻动n行,默认是1行,可自定义
  • Ctrl+F:向下滚动一屏
  • 空格键:向下滚动一屏
  • Ctrl+B:返回上一屏
  • V:调用vim编辑器
  • q:推出more命令
  • more +3 bunian.txt# 从第3行开始显示
    more -20 bunian.txt # 屏幕只显示20行
    more +/hello bunian.txt # 查询文件中第1次出现hello的位置
    more -c -5 file# 每5行显示一次,而且在显示之前先清屏
    Copy after login

    如果某个目录下文件过多,我们可以使用more命令来进行分页显示:

    ls -l | more -5   # 输出当前目录下的全部文件,并且每页显示5个文件信息
    Copy after login

    还有其他的显示文件行数的命令:less、head、tail、tac

    tac file# 从最后一行开始显示行号
    head -n 4 file# 查看文件的前4行
    tail -n 8 file# 查看文件的最后8行
    tail -n +500 file# 从第500行开始显示,即只显示500行以后的
    cat file | head -n 200 | tail -n +100# 显示100-300行的
    cat file | tail -n +200 | head -n 100# 从第200行开始,显示100行(即200-299行)
    Copy after login

    命令11:ps

    ps命令是用来查看系统中的进程所在的状态

    ps -a:查看所有的进程(包含其他用户的进程)

    [root@bunian ~]# ps -a
    PID TTYTIME CMD
    579 pts/000:00:00 ps
    Copy after login

    ps -u:查看用户及其他信息

    [root@bunian ~]# ps -u
    USER PID %CPU %MEMVSZ RSS TTYSTAT START TIME COMMAND
    root 6250.00.0 1554481832 pts/0R+ 01:25 0:00 ps -u
    root13990.00.0 110208 664 tty1 Ss+Dec01 0:00 /sbin/agetty --noclear tty1 linux
    root14040.00.0 110208 640 ttyS0Ss+Dec01 0:00 /sbin/agetty --keep-baud 115200,38400,96
    root 188280.00.0 1167283268 pts/0Ss Dec19 0:00 -bash
    Copy after login

    ps -x:显示没有控制终端的进程

    [root@bunian ~]# ps -x
    PID TTYSTAT TIME COMMAND
    1 ?Ss 1:36 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    2 ?S0:00 [kthreadd]
    4 ?S< 0:00 [kworker/0:0H]
    6 ?S0:27 [ksoftirqd/0]
    7 ?S0:08 [migration/0]
    8 ?S0:00 [rcu_bh]
    9 ?S 17:06 [rcu_sched]
     10 ?S< 0:00 [lru-add-drain]
     ......此处省略
    
    Copy after login

    Linux系统中进程的5种常见状态

  • R:运行,进程正在运行或者在队列中等待
  • S:中断,进程处于休眠状态中。当接受到某个条件后,即可脱离该状态
  • D:不可中断,在这种状态下即使kill命令也无法将其中断
  • Z:僵死,进程已经终止,但是进程描述符依然存在。若父进程调用wait()系统函数后将进程释放
  • T:停止,进行收到停止信号后停止运行
  • 命令12:top

    top命令是用来动态地监控进程活动或者系统负载等信息的,它的功能可以看做是Windows系统中的“Windows任务管理器”。

    查询进程的端口号:

    netstat -tunlp | grep 端口号   
    Copy after login

    命令13:pidof

    该命令是用来查询某个指定的服务进程的pid值

    pidof sshd  # 查看sshd服务的进程pid值
    Copy after login

    命令14:kill、killall

    kill用于终止某个指定pid号的服务进程

    kill 18828  # 杀死pid为18828的服务
    Copy after login

    killall用来终止某个指定名称的服务所对应的全部进程,参数为进程名称

    pidof httpd#查询httpd服务的全部进程
    killall httpd # 终止http服务的全部进程
    Copy after login

    命令15:ifconfig

    ifconfig命令主要是用来获取网卡配置和系统的网络状态等信息,主要信息为:

    • 网卡名称
    • inet参数后面的IP地址
    • MAC地址
    • RX、TX的接收和发送数据等信息
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
    inet 10.0.8.2netmask 255.255.252.0broadcast 10.0.11.255
    inet6 fe80::5054:ff:fe95:2cdfprefixlen 64scopeid 0x20<link>
    ether 52:54:00:95:2c:dftxqueuelen 1000(Ethernet)
    RX packets 48350308bytes 13922587280 (12.9 GiB)
    RX errors 0dropped 0overruns 0frame 0
    TX packets 50364002bytes 30975667765 (28.8 GiB)
    TX errors 0dropped 0 overruns 0carrier 0collisions 0
    
    Copy after login

    命令16:history

    history命令主要是用来查看我们敲过的历史命令。

    history# 查看全部历史命令
    history 10# 查看最近的10条命令
    Copy after login

    history可以与grep、tail配合使用进行条件过滤来查找我们需要的命令:

    history | grep dnf# 和dnf相关的命令
    history | tail -n 3 # 末尾3条命令
    Copy after login

    清除历史命令:

    history-d 50# 清除第100条
    history -c# 全部清除掉
    Copy after login

    The above is the detailed content of Linux common commands. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:mryunwei.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!