Common Commands for Linux Virtual Machines

1. File/folder management

1. ls command

It is  list the abbreviation of , through  ls commands, you can not only view  linux the files contained in the folder, but also view the file permissions (including directories, folders, file permissions), view directory information, and so on.

 ls -a 列出目录所有文件,包含以.开始的隐藏文件

 ls -A 列出除.及..的其它文件

 ls -r 反序排列

 ls -t 以文件修改时间排序

 ls -S 以文件大小排序

 ls -h 以易读大小显示

 ls -l 或 ll,除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来

2. cd command

Abbreviation for changeDirectory, the function is to switch folders

(1)进入根目录

     cd /

(2)进入"家"目录

     cd ~

(3)进入上一次工作路径

     cd -

(4)把上个命令的参数作为cd参数使用。

     cd !$

(5)返回当前目录的上一级目录

     cd ..

Expansion: The home directory is the user's directory, such as the root user directory, and the root directory is the top layer, that is, the root directory contains the home directory

3. pwd command

View the path of the current directory

(1)查看当前路径

     pwd

(2)查看软链接的实际路径

     pwd -P

4. mkdir command

create folder

(1)在指定目录下创建文件夹,若这个指定目录不存在则报错

     mkdir 【文件夹路径+文件夹名】

(2)在指定目录下创建文件夹,若这个指定目录不存在则 创建这个目录

     mkdir -p 【文件夹路径+文件夹名】

5. rmdir command

Delete empty folders, note that this directory can only delete empty folders

(1)删除一个空目录

     rmdir 【文件】

(2)删除一个空目录,如果删除后使上级目录也为空,则一并删除

     rmdir -p【文件】

6. rm command

Delete one or more files or directories in a directory, if no  - r option is used,  rm the directory will not be removed. If you  rm delete a file with , you can usually still get that file back  rm [选项] 文件 .

(1)删除任何.txt文件;删除前逐一询问确认

     rm -i *.txt

(2)删除test子目录及子目录中所有档案删除,并且不用一一确认

     rm -rf test

(3)删除以-k开头的文件

     rm -- -k*

7. mv command

Move the file or modify the file name, according to the type of the second parameter (such as a directory, move the file; if it is a file, reorder the file).

When the second parameter is a directory, multiple files can be separated by spaces as the first parameter, and multiple files can be moved to the directory specified by parameter 2.

(1)将文件test.log重命名为test1.txt

     mv test.log test1.txt

(2)将文件log1.txt,log2.txt,log3.txt移动到根的test3目录中

     mv llog1.txt log2.txt log3.txt /test3

(3)将文件file1改名为file2,如果file2已经存在,则询问是否覆盖

     mv -i log1.txt log2.txt

(4)移动当前文件夹下的所有文件到上一级目录

     mv * ../

8. find command

Used to find files in the file tree and make corresponding processing

 命令格式:

     find pathname -options [-print -exec -ok ...]

 命令参数:

     pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。

     -print: find命令将匹配的文件输出到标准输出。

     -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {  } \;,注意{   }和\;之间的空格。

     -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

 命令选项:

     -name 按照文件名查找文件

     -perm 按文件权限查找文件

     -user 按文件属主查找文件

     -group  按照文件所属的组来查找文件。

     -type  查找某一类型的文件,诸如:

               b - 块设备文件

               d - 目录

               c - 字符设备文件

               l - 符号链接文件

               p - 管道文件

               f - 普通文件

     -size n :[c] 查找文件长度为n块文件,带有c时表文件字节大小

     -amin n   查找系统中最后N分钟访问的文件

     -atime n  查找系统中最后n*24小时访问的文件

     -cmin n   查找系统中最后N分钟被改变文件状态的文件

     -ctime n  查找系统中最后n*24小时被改变文件状态的文件

     -mmin n   查找系统中最后N分钟被改变文件数据的文件

     -mtime n  查找系统中最后n*24小时被改变文件数据的文件

     (用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。 )

     -maxdepth n 最大查找目录深度

     -prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略

     -newer 如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项

9. cp command

Copy a source file to a target file, or copy multiple source files to a target directory.

Note : command line copy, if the target file already exists, it will prompt whether to overwrite, but in  shell the script, if no  -i parameters are added, there will be no prompt, but directly overwrite!

     -i 提示

     -r 复制目录及目录内所有项目

     -a 复制的文件与原文件时间一样

10. touch command

Create one or more empty files

(1) 创建一个新的空文件
touch file.txt
 
(2) 创建多个新的空文件
touch file1.txt file2.txt file3.txt

2.  System Management

1.  fdisk command

The fdisk command is a command-line tool for partitioning hard disks in the Linux operating system. It can be used to create, delete, adjust partitions, and set partition properties, etc.

fdisk -l           #查看系统分区信息

#为一块新的SCSI硬盘进行分区
# 进入交互式分区工具(/dev/sda为示例)
fdisk /dev/sda
 
# 创建新分区
n
# 按照提示设置分区大小和起始位置
 
# 删除分区
d
# 选择要删除的分区号
 
# 修改分区类型
t
# 选择要修改的分区号
# 按照提示选择新的分区类型
 
# 保存分区表更改
w

2.  chown command

command to change the owner of a file or directory

Help memory: [change owner]

usage:

chown [选项] <新所有者> <文件/目录>

Common options:

  • -R Recursively change the owner of all files and subdirectories under the specified directory.

  • --reference=<参考文件/目录> Get the owner and group information from the reference file/directory and apply it to the target file/directory.

Example:

 1. Change the owner of the file file.txt to the root user:

chown root file.txt

2. Change the owner of the directory dir1 and its subdirectories and files to user1: 

chown -R user1 dir1

3. According to the owner and group information of the reference file ref.txt, the owners and groups of all files and subdirectories under the directory dir2 are consistent:

chown --reference=ref.txt -R dir2

Note: The use of the chown command requires sufficient authority, and generally needs to be executed as the root user or a user with sudo authority. 

3.  chgrp command

Command to change the group to which a file or directory belongs

Help memory: [change group]

usage:

chgrp [参数] [属组GID|属组名称] [文件|目录]

options:

-R	递归处理,将指定目录下的所有文件及子目录一起更改
-v	显示命令执行过程

Example:

Change the file ztj.sh from root to ztj

chgrp -v ztj ztj.sh 

4. chmod command

linuxCommands to change the access permissions of system files or directories

There are two uses for this command. One is a text setting method including letters and operator expressions; the other is a number setting method including numbers.

There are three groups of access permissions for each file or directory, and each group is represented by three digits, which are the read, write, and execute permissions of the file owner; the read, write, and execute permissions of users in the same group as the owner; Read, write, and execute permissions for the user. can be ls -l 或 llfound using

Example:

 以文件log2012.log为例:

     -rw-r--r-- 1 root root 296K 11-13 06:03 log2012.log

There are 10 positions in the first column, and the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a dash, it means a non-directory file. If it is d, it means a directory. From the second character to the tenth, there are 9 characters in total, and a group of 3 characters represents the permissions of 3 groups of users on files or directories. Permission characters use a horizontal line to represent empty permissions, r for read-only, w for write, and x for executable.

 常用参数:

     -c 当发生改变时,报告处理信息

     -R 处理指定目录以及其子目录下所有文件

 权限范围:

     u :目录或者文件的当前的用户

     g :目录或者文件的当前的群组

     o :除了目录或者文件的当前用户或群组之外的用户或者群组

     a :所有的用户及群组



 权限代号:

     r :读权限,用数字4表示

     w :写权限,用数字2表示

     x :执行权限,用数字1表示

     - :删除权限,用数字0表示

     s :特殊权限

Example:

(1)增加文件t.log所有用户可执行权限

     chmod a+x t.log

(2)撤销原来所有的权限,然后使拥有者具有可读权限,并输出处理信息

     chmod u=r t.log -c

(3)给file的属主分配读、写、执行(7)的权限,给file的所在组分配读、执行(5)的权限,给其他用户分配执行(1)的权限

     chmod 751 t.log -c(或者:chmod u=rwx,g=rx,o=x t.log -c)

(4)将test目录及其子目录所有文件添加可读权限

     chmod u+r,g+r,o+r -R text/ -c

5. useradd command

The useradd command is a command used to add a new user in a Linux system.

基本语法:

useradd [选项] 用户名

选项:

-c, --comment COMMENT:设置用户描述信息

-d, --home HOME_DIR:设置用户的主目录

-e, --expiredate EXPIRE_DATE:设置账号的失效日期

-f, --inactive INACTIVE:设置账号的非活动期限

-g, --gid GROUP:设置用户所属的主要组

-G, --groups GROUPS:设置用户所属的其他附加组

-m, --create-home:创建用户的主目录

-s, --shell SHELL:设置用户的登录Shell

-k, --skel SKEL_DIR:指定用于创建用户家目录的骨架目录

-p, --password PASSWORD:设置用户的密码

-r, --system:创建系统账号

-U, --user-group:同时创建与用户名同名的用户组

-M, --no-create-home:不创建用户主目录

Example:

(1)创建一个名为"testuser"的新用户,并指定用户描述信息和主目录:

useradd -c "Test User" -d /home/testuser testuser

(2)创建一个新用户"devuser",并将其添加到"developers"组中:

useradd -G developers devuser

(3)创建一个系统账号"sysuser",并设置其主目录为"/var/sysuser":

useradd -r -d /var/sysuser sysuser

6. g roupadd command

The groupadd command is a command used to create a new user group in the Linux system.

它的基本语法如下:

groupadd [选项] 组名

其中,选项可以包括:

-g GID:指定新用户组的GID(组ID)。

-o:允许新用户组的GID与已存在用户组的GID重复。

-r:创建一个系统用户组。

Example:

例如,要创建一个名为"testgroup"的用户组,可以使用以下命令:

groupadd testgroup

7. p asswd command

passwd command is a command used to change user password in Linux system. It allows users to change their own passwords or administrators to change other users' passwords.

If you want to change your password, just enter the passwd command in the terminal, and then follow the prompts to enter the current password and new password.

If you are an administrator and want to change the password of another user, you need to use the sudo passwd <username> command. where "<username>" is the username of the user whose password you want to change. Then follow the prompts to enter the administrator password and new password.

Common options:

-d 用户名    #删除用户密码也能登陆
-S 用户名    #查询账号密码

Example:

$ passwd
当前密码:*******
新的密码:*******
重新输入新的密码:*******
$ sudo passwd alice
[sudo] password for admin: ******
新的密码:*******
重新输入新的密码:*******

 It should be noted that the passwd command can only be executed by administrators or users themselves, and ordinary users cannot change the passwords of other users. Also, to protect account security, passwords should be complex and changed regularly.

8. u sermod command

usermodcommand is the command used to modify the user account. It can be used to modify the user's user name, user ID, group, user home directory, login shell and other information.

Administrator privileges are required to use usermodthe command, and the specific usage is as follows:

(1)修改用户名:usermod -l new_username old_username

示例:usermod -l user1 user2,将用户user2的用户名修改为user1。

(2)修改用户ID:usermod -u new_user_id username

示例:usermod -u 1001 user1,将用户名为user1的用户的用户ID修改为1001。

(3)修改所属组:usermod -g group_name username

示例:usermod -g group1 user1,将用户名为user1的用户的所属组修改为group1。

(4)修改用户主目录:usermod -d new_home_directory username

示例:usermod -d /home/user1 user1,将用户名为user1的用户的用户主目录修改为/home/user1。

(5)修改用户登录shell:usermod -s new_login_shell username

示例:usermod -s /bin/bash user1,将用户名为user1的用户的登录shell修改为/bin/bash。

Note: When using the usermod command to modify user information, you need to be careful to avoid affecting the system or causing security problems.

9.  Userdel command

 The userdel command is used to delete a user account and its associated user groups and user home directory. When the userdel command is executed, the user account will be marked as deleted, but the user home directory will not be automatically deleted by default.

Common options:

-r:删除用户账户及其用户主目录。使用此选项将同时删除用户主目录,确保用户的所有数据都被清除。

-f:强制删除用户账户。即使用户当前登录,也会被强制删除。

-Z:删除用户账户时,不会删除用户的安全上下文。

10.  System service control operation

service [servicename] start/stop/restart 或
/etc/init.d/[servicename] start/stop/restart 

11. View hardware information

Note that some commands cannot be used because the plugin for the command has not been downloaded

uname -a     查看内核版本
cat /etc/issue 查看ubuntu版本
lsusb         查看usb设备
sudo ethtool eth0 查看网卡状态
cat /proc/cpuinfo 查看cpu信息
lshw         查看当前硬件信息
sudo fdisk -l  查看磁盘信息
df -h         查看硬盘剩余空间
free -m       查看当前的内存使用情况
ps -A         查看当前有哪些进程

12.  kill command

Send the specified signal to the corresponding process. Not specifying a model will send SIGTERM(15) to terminate the specified process. If the program cannot be terminated, the "-KILL" parameter can be used, and the signal sent is SIGKILL(9), which will forcibly end the process (ie kill -9). Use the ps command or the jobs command to view the process number. The root user will affect the user's process, and the non-root user can only affect its own process.
Common options:


     -l  信号,若果不加信号的编号参数,则使用“-l”参数会列出全部的信号名称

     -a  当处理当前进程时,不限制命令名和进程号的对应关系

     -p  指定kill 命令只打印相关进程的进程号,而不发送任何信号

     -s  指定发送信号

     -u  指定用户

13. Reboot and Shutdown Linux System

reboot Init 6 或 reboot 或 shutdown -r now   重启LINUX系统
halt Init 0 Shutdown –h now   关闭LINUX系统

3.  Packaging/Decompression (tar)

tar command

Used to compress and decompress files. tar itself does not have a compression function, but only a packaging function. The compression and decompression are done by calling other functions.

Clarify two concepts: packaging and compression. Packaging refers to turning a large number of files or directories into a total file; compression refers to turning a large file into a small file through some compression algorithms.

     常用参数:

     -c 建立新的压缩文件

     -f 指定压缩文件

     -r 添加文件到已经压缩文件包中

     -u 添加改了和现有的文件到压缩包中

     -x 从压缩包中抽取文件

     -t 显示压缩文件中的内容

     -z 支持gzip压缩

     -j 支持bzip2压缩

     -Z 支持compress解压文件

     -v 显示操作过程
     gzip实例:压缩gzip fileName .tar.gz和.tgz  解压:gunzip filename.gz或gzip -d filename.gz

     对应:tar zcvf filename.tar.gz     tar zxvf filename.tar.gz

     bz2实例:压缩bzip2 -z filename .tar.bz2 解压:bunzip filename.bz2或bzip -d filename.bz2

     对应:tar jcvf filename.tar.gz         解压:tar jxvf filename.tar.bz2

Example:

(1)将文件全部打包成tar包

     tar -cvf log.tar 1.log,2.log 或tar -cvf log.*

(2)将/etc下的所有文件及目录打包到指定目录,并使用gz压缩

     tar -zcvf /tmp/etc.tar.gz /etc

(3)查看刚打包的文件内容(一定加z,因为是使用gzip压缩的)

     tar -ztvf /tmp/etc.tar.gz

(4)要压缩打包/home, /etc ,但不要 /home/dmtsai

     tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc


tar -c 创建包 –x 释放包 -v 显示命令过程 –z 代表压缩包
tar –cvf benet.tar /home/benet   把/home/benet目录打包
tar –zcvf benet.tar.gz /mnt   把目录打包并压缩
tar –zxvf benet.tar.gz        压缩包的文件解压恢复
tar –jxvf benet.tar.bz2        解压缩

Four, make compile

1. make tool

People usually use the make tool to automatically complete the compilation work.
These tasks include: if only a few source files have been modified, only these few source files will be recompiled
; Source files
Utilizing this automatic compilation greatly simplifies development and avoids unnecessary recompilation
 

2. How make works

1. make will find the name "Makefile" or "makefile" in the current directory

2. If found, it will find the first target file (target) in the file. In the above example, it will find the "main" file and use this file as the final target file

3. If the main file does not exist, or the file modification time of the subsequent .o file that main depends on is newer than the main file, then it will execute the command defined later to generate the main file

4. If the .o file that main depends on also exists, then make will find the dependency whose target is the .o file in the current file, and if found, then generate the .o file according to that rule

5. Of course, your C file and H file exist, so make will generate the .o file, and then use the .o file. The ultimate task of make is to execute the file main

Note: If one or more files are updated in DEPENDENCIES, COMMAND will be executed, which is the core content of Makefile 

make 编译
make install 安装编译好的源码包

Five,  apt command

The apt command is a package management tool commonly used in Linux systems for installing, updating, and uninstalling software.

Example:

apt-cache search package 搜索包
apt-cache show package 获取包的相关信息,如说明、大小、版本等
sudo apt-get install package 安装包
sudo apt-get install package - - reinstall 重新安装包
sudo apt-get -f install 修复安装”-f = –fix-missing”
sudo apt-get remove package 删除包
sudo apt-get remove package - - purge 删除包,包括删除配置文件等
sudo apt-get update 更新源
sudo apt-get upgrade 更新已安装的包
sudo apt-get dist-upgrade 升级系统
sudo apt-get dselect-upgrade 使用 dselect 升级
apt-cache depends package 了解使用依赖
apt-cache rdepends package 是查看该包被哪些包依赖
sudo apt-get build-dep package 安装相关的编译环境
apt-get source package 下载该包的源代码
sudo apt-get clean && sudo apt-get autoclean 清理无用的包
sudo apt-get check 检查是否有损坏的依赖
sudo apt-get clean 清理所有软件缓存(即缓存在/var/cache/apt/archives目录里的deb包)

查看软件xxx安装内容    #dpkg -L xxx
查找软件                #apt-cache search 正则表达式
查找文件属于哪个包     #dpkg -S filename apt-file search filename
查询软件xxx依赖哪些包  #apt-cache depends xxx
查询软件xxx被哪些包依赖  #apt-cache rdepends xxx
增加一个光盘源       #sudo apt-cdrom add

系统升级
#sudo apt-get update
#sudo apt-get upgrade
#sudo apt-get dist-upgrade

清除所以删除包的残余配置文件
#dpkg -l |grep ^rc|awk ‘{print $2}’ |tr ["\n"] [" “]|sudo xargs dpkg -P -

编译时缺少h文件的自动处理
#sudo auto-apt run ./configure

查看安装软件时下载包的临时存放目录
#ls /var/cache/apt/archives

备份当前系统安装的所有包的列表
#dpkg –get-selections | grep -v deinstall > ~/somefile

从上面备份的安装包的列表文件恢复所有包
#dpkg –set-selections < ~/somefile sudo dselect

清理旧版本的软件缓存
#sudo apt-get autoclean

清理所有软件缓存
#sudo apt-get clean

删除系统不再使用的孤立软件
#sudo apt-get autoremove

查看包在服务器上面的地址
#apt-get -qq –print-uris install ssh | cut -d\’ -f2

Guess you like

Origin blog.csdn.net/jojo_oulaoula/article/details/132445751