Commonly used basic commands in Linux

We all know that common Linux commands refer to command tools that are widely used in the Linux operating system. These command tools can complete various tasks, such as managing files and directories, operating processes, network communications, software installation, etc. Some commonly used Linux commands include cd, ls, mkdir, rm, cp, mv, ps, top, ping, ssh, tar, grep, find, shutdown, reboot, apt, and yum, etc. These command tools usually have their own options and parameters, which can be used to perform more refined and complex operations. Mastering these common Linux commands can help users use the Linux operating system more efficiently and complete various tasks.

Then the editor will introduce some common instructions to you to help you use them better in the Linux operating system.

1. cd command

cd: The cd command is used to switch the current working directory to the specified directory. For example, enter "cd /home/user" to switch the current directory to the /home/user directory. Commonly used options are as follows:

cd ..		返回上一级目录
cd../.. 	返回上两级目录
cd ~		进入个人主目录
cd -		返回上次所在的目录

2. pwd command

pwd: The pwd command is used to display the full path of the current working directory

3. ls command

ls command: used to display a list of files and directories in a specified directory. If no directory is specified, the file and directory list in the current directory will be displayed by default.

ls		    查看当前目录下的文件
ls -l(ll)	查看文件和目录的详细资料
ls -a		列出全部文件,包含隐藏文件
ls -R		连同子目录的内容一起列出(递归列出文件)
ls [0-9]    显示包含数字的文件名和目录名

4. cp command

cp: The cp command is used to copy specified files or directories. For example, enter "cp file.txt /home/user" to copy the file named file.txt in the current directory to the /home/user directory.

-a :将文件的特性一起复制
-p :连同文件的属性一起复制,而非使用默认方式,与-a相似,常用于备份
-i :若目标文件已经存在时,在覆盖时会先询问操作的进行
-r :递归持续复制,用于目录的复制行为
-u :目标文件与源文件有差异时才会复制

5. mv command

mv: The mv command is used to move the specified file or directory. For example, enter "mv file.txt /home/user" to move the file named file.txt in the current directory to the /home/user directory.

-f		若目标已经存在,不会询问。
-i		若目标文件已经存在,则询问是否覆盖
-u		若目标文件已经存在。且比目标文件新,才会更新

6. rm command

rm: The rm command is used to delete specified files or directories.

-f	  忽略不存在的文件,不会存在警告消息
-i	  在删除前会询问用户是否操作、
-r	  递归删除,常常用于目录删除
eg: rm -rf: 删库跑路常用代码

7. cat command

cat: The cat command is used to view the contents of the specified file. For example, enter "cat file.txt" to display the contents of a file named file.txt.

cat file1 	从第一个字节开始正向查看文件的内容
tac file1 	从最后一行开始反向查看一个文件的内容
cat -n file1 	标示文件的行数
more file1 	查看一个长文件的内容
head -n 2 file1 	查看一个文件的前两行
tail -n 2 file1 	查看一个文件的最后两行
tail -n +1000 file1  	从1000行开始显示,显示1000行以后的
cat filename | head -n 3000 | tail -n +1000  	显示1000行到3000行
cat filename | tail -n +3000 | head -n 1000  	从第3000行开始,显示1000(即显示3000~3999行)

8. find command

find: The find command is used to find files in the file system. For example, enter "find / -name file.txt" to find all files named file.txt in the root directory of the file system and output the results to the screen. "-type" means searching according to file type, "-mtime" means searching according to file modification time, etc.

find / -name file1 	从 '/' 开始进入根文件系统搜索文件和目录
find / -user user1 	搜索属于用户 'user1' 的文件和目录
find /usr/bin -type f -atime +100 		搜索在过去100天内未被使用过的执行文件
find /usr/bin -type f -mtime -10 		搜索在10天内被创建或者修改过的文件
whereis halt 	显示一个二进制文件、源码或man的位置
which halt 	显示一个二进制文件或可执行文件的完整路径

9. chmod command

chmod command: The chmod command is used to modify the permissions of a specified file or directory. For example, enter "chmod 755 file.txt" to set the permissions of the file named file.txt to 755, that is, the owner has read, write, and execute permissions, and group users and other users only have read and execute permissions.

ls -lh 显示权限
chmod ugo+rwx directory1 设置目录的所有人(u)、群组(g)以及其他人(o)以读(r,4 )、写(w,2)和执行(x,1)的权限
chmod go-rwx directory1  删除群组(g)与其他人(o)对目录的读写执行权限

10. chown command

chown command: The chown command is used to modify the owner of a specified file or directory. For example, enter "chown user file.txt" to set the owner of a file named file.txt to user.

(改变文件的所有者)
chown user1 file1 改变一个文件的所有人属性
chown -R user1 directory1 改变一个目录的所有人属性并同时改变改目录下所有文件的属性
chown user1:group1 file1 改变一个文件的所有人和群组属性

11. chgrp command

(Change the user group to which the file belongs)

chgrp group1 file1 changes the group of the file

12. grep command

grep command: The grep command is used to find matching text in a specified file. For example, enter "grep keyword file.txt" to find lines of text containing the keyword "keyword" in a file named file.txt.

(分析一行的信息,若当中有我们所需要的信息,就将该行显示出来,该命令通常与管道命令一起使用,用于对一些命令的输出进行筛选加工等等)
grep Aug /var/log/messages  在文件 '/var/log/messages'中查找关键词"Aug"
grep ^Aug /var/log/messages 在文件 '/var/log/messages'中查找以"Aug"开始的词汇
grep [0-9] /var/log/messages 选择 '/var/log/messages' 文件中所有包含数字的行
grep Aug -R /var/log/* 在目录 '/var/log' 及随后的目录中搜索字符串"Aug"
sed 's/stringa1/stringa2/g' example.txt 将example.txt文件中的 "string1" 替换成 "string2"
sed '/^$/d' example.txt 从example.txt文件中删除所有空白行

13. paste command

paste: The paste command is used to merge two or more files line by line and output the results to standard output. Its syntax is:

paste [option]... [file]...

paste file1 file2 合并两个文件或两栏的内容
paste -d '+' file1 file2 合并两个文件或两栏的内容,中间用"+"区分

14. sort command

sort: The sort command is used to sort files. Its syntax is:

sort [option]... [file]... :其中,option表示选项,file表示要排序的文件名。如果不指定文件名,则默认从标准输入读取数据。
-r:降序排列。
-n:按数值大小排序。
-u:去重,只输出不重复的行。
-k:指定排序的键,可以是行中的某个字段。


sort file1 file2 排序两个文件的内容
sort file1 file2 | uniq 取出两个文件的并集(重复的行只保留一份)
sort file1 file2 | uniq -u 删除交集,留下其他的行
sort file1 file2 | uniq -d 取出两个文件的交集(只留下同时存在于两个文件中的文件)

Fifteen: touch

touch command: The mkdir command is used to create a new directory at the specified location. For example, enter "mkdir new_dir" to create a new directory named new_dir in the current directory.

rm-rf /^c command

The editor will share another command. The rm-rf /^c command means to delete the library and run away (it is recommended that you do not try it easily), otherwise it will directly delete all the files in the system. Of course, if there are not many system files, you can try it and delete it. Then I needed to reinstall the system, and it was installed in two minutes. Haha, I learned not to damage other people’s computers intentionally! There are also some practical operation instructions, which I will share with you in the next issue.

Guess you like

Origin blog.csdn.net/whwwyshf/article/details/133329356