Linux Getting Started Instructions

file management commands

df command

df command description: This command checks the disk space usage of the file system. You can use this command to obtain information such as how much space is occupied by the hard disk and how much space is left.

df command syntax:

df [参数] [目录或文件名]

Parameter Description:

parameter illustrate
-a List all file systems, including system-specific file systems such as /proc.
-k Returns the capacity of each file system in KBytes.
-m Returns the capacity of each file system in MBytes.
-h Returns the capacity of each file system in GBytes, MBytes, and KBytes.
-H Display the capacity of each file system with M=1000K instead of M=1024K.
-T Displays the file system type.
-i Display inode information.

Example of using the df command:

Example 1: Display disk usage.

Run the following command to display disk usage.

df

The returned result is shown below.

img

Example 3: Display all special file formats, names and disk usage in the system.

Execute the following command to display all special file formats, names and disk usage in the system.

df -aT

The returned result is shown below.

img

Example 4: Display the capacity of each file system in GBytes, MBytes, KBytes and other formats.

Run the following command to display the capacity of each file system in GBytes, MBytes, KBytes and other formats.

df -h

The returned result is shown below.

img

du instruction

du command description: View disk space usage. The difference between du and the df command is that the du command is used to view the used space of the file and directory disk.

du command syntax:

du [参数] [文件或目录名称]

Parameter Description:

parameter illustrate
-a List all file and directory capacities.
-h Returns the capacity in units of G, M, K.
-s List the total.
-S List totals excluding subdirectories.
-k Returns the capacity in KBytes.
-m Returns the capacity in MBytes.

Example of using the du command:

Example 1: List the capacity of all folders in the current directory.

Execute the following command to list the capacity of all folders in the current directory.

du

The returned result is shown below.

img

Example 2: List the capacity of all folders and files in the current directory.

Execute the following command to list the capacity of all folders and files in the current directory.

du -a

The returned result is shown below.

img

Example 3: List the capacity of all folders and files in the current directory, and display the capacity in G, M, K format.

Execute the following command to list the capacity of all folders and files in the current directory.

du -ah

The returned result is shown below.

img

Example 4: List the capacity occupied by each directory under the root directory, and display the capacity in MBytes.

Execute the following command to list the capacity occupied by each directory under the root directory, and display the capacity in MBytes.

du -sm /*

The returned result is shown below.

img

fdisk command

fdisk command description: This command is used for disk partitioning.

fdisk command syntax:

fdisk [-l] 装置名称

Parameter Description:

parameter illustrate
-l Outputs all partition contents following the device name. If there is only fdisk -l, the system will list all the partitions of the device that can be searched in the whole system.

Example of using the fdisk command:

Example 1: List partition information of all devices in the system.

Execute the following command to list the partition information of all devices in the system.

fdisk -l

The returned result is shown below.

img

Example 2: List the disk where the root directory in the system is located, and check the relevant information in the hard disk.

a. Run the following command to find out the name of the disk where the root directory is located.

df /

The returned result is shown below.

img

b. Run the following commands to partition the disk /dev/vda.

fdisk /dev/vda

Notice:

When partitioning a disk, the disk name does not contain numbers.

The returned result is shown below.

img

c. Run the following command to get help.

m

The returned result is shown below.

img

Run the following command to check the disk status.

p

The returned result is as follows, and you can view the related status of the disk.

img

d. Execute the following command, leave without saving any operations.

q

file directory management commands

ls command

Command description: The ls command is used to display the contents of the specified working directory.

Command format: ls [parameter] [directory name].

Parameter Description:

parameter illustrate
-a Show all files and directories (including hidden files)
-l List file permissions, owner, file size and other details (ll is equivalent to ls -l)
-r Deserialize the files (default alphabetical order)
-t List files in positive order by creation time
-R Recursively traverse files in a directory

Example of command usage:

View all files in the current directory (including hidden files).

ll -a

Command output:

img

pwd command

Command description: Get the absolute path of the current working directory.

Example of command usage:

img

file manipulation commands

cd command

Command description: The cd command is used to switch the working directory.

Example of command usage:

img

In the path representation:

  • A period (.) indicates the current directory, for example the path ./app/log is equivalent to app/log.
  • Two half-width periods (…) indicate the upper-level directory. For example, the path /usr/local/…/src is equivalent to /usr/src, where the local and src directories are at the same level.

The default parameter of the cd command is , the symbol indicates the home directory of the current user, that is, when the root user logs in, the commands cd, cd ~ and cd /root have the same effect.

touch naming

Command description: The touch command is used to modify the time attribute of a file or directory, including access time and change time. If the file does not exist, the system will create a new file.

Command format: touch [parameter] [file].

Parameter Description:

parameter illustrate
-c If the specified file does not exist, no new file will be created
-r Time Recording Using Reference Documents
-t Set the time record of the file

Example of command usage:

  • Create two empty files.
touch demo1.txt demo2.txt

img

  • The time of modifying demo1.txt is recorded as the current system time.

img

  • Update the time record of demo2.txt to be the same as the time record of demo1.txt.

img

mkdir command

Command description: The mkdir command is used to create a new subdirectory. The -p parameter ensures that the directory name exists, and creates a new one if it does not exist.

mkdir -p a/b/c/d

Example of command usage:

Create a new directory a/b/c/d, and use the tree command to view the created directory structure.

tree

img

rm command

Command description: The rm command is used to delete a file or directory.

Command format: rm [parameter] [file].

Parameter Description:

parameter illustrate
-i Ask for confirmation one by one before deleting
-f Delete without confirmation
-r delete all files in the directory

Example of command usage:

Delete files directly without confirmation.

rm -rf demo*

img

Directly delete directory a and all subdirectories and files under it without confirmation.

rm -rf a

img

cp command

Command description: The cp command is mainly used to copy files or directories.

Command format: cp [parameter] [source file] [target file].

Parameter Description:

parameter illustrate
-d keep link when copying
-f Overwrite an existing object file without prompting
-i Ask before coverage
-p In addition to copying the contents of the file, the modification time and access rights are also copied to the new file
-r Copy a directory and all items within the directory

Example of command usage:

Copy everything in directory c/d to directory a/b.

img

mv command

Command description: The mv command is used to rename a file or directory, or move a file or directory to another location.

Command format: mv [parameter] [source file] [target file]

Parameter Description:

parameter illustrate
-i If there is a file with the same name in the specified directory, first ask whether to overwrite the old file
-f If the target file already exists, it will be overwritten without asking

Example of command usage:

  • Change the file name a.txt to b.txt.img

  • Move the c directory to a/b/c/d/.img

  • Move all the contents of the current directory to the /tmp directory.

mv ./* /tmp

rename command

Command description: The rename command changes file names in batches by string replacement. There are two versions of the rename command in C language and Perl language. Here, the C language version of the rename command is introduced, which does not support regular expressions.

Example of command usage:

  • Change the string demo in all file names in the current directory to the uppercase string DEMO.img

  • Change the suffix of all .txt files in the current directory to text.img

\4. File permissions

The ls command can view the permissions of files, directories, and devices on a Linux system.

img

The first column displayed in the above ls -l command is the file permission information, which consists of 11 characters and is divided into 5 parts.

  • 第1位表示存档类型,d表示目录,-表示一般文件。
  • 第2~4位表示当前用户的权限(属主权限)。
  • 第5~7位表示同用户组的用户权限(属组权限)。
  • 第8~10位表示不同用户组的用户权限(其他用户权限)。
  • 第11位是一个半角句号.,表示SELinux安全标签。

用户权限每组三位,rwx分别表示读、写、执行权限,对应八进制表示为4、2、1。

例如efi目录的root用户权限为drwxr-xr-x.。

  • 该目录对root用户具有读写和执行所有权限。
  • 该目录对root组其他用户有读和执行权限。
  • 该目录对其他用户有读和执行权限。

所以该权限表示对应八进制权限表示为:

  • 属主权限:4+2+1=7。
  • 属组权限:4+1=5。
  • 其他用户权限:4+1=5。

即755。

####chmod命令

chmod命令用于修改文件权限mode,-R参数以递归方式对子目录和文件进行修改。

命令使用示例:

  1. 新建名为hello.sh的Shell脚本,该脚本将会输出Hello World。用ll命令可以看到新建的脚本没有执行权限,其权限用八进制表示为644。img
  2. 将hello.sh文件增加属主的执行权限。 img
  3. 将hello.sh文件撤销属主的执行权限。 img
  4. 将hello.sh文件权限修改为八进制表示的744权限。 img
  5. 使用bash命令解释器执行hello.sh脚本文件。 img

其中,u+x表示增加属主的执行权限,u表示属主,g表示属组,o表示其他,a表示所有用户。

chown命令

chown命令修改文件的属主和属组;-R参数以递归方式对子目录和文件进行修改;ls -l命令显示的第三列和第四列就是文件的属主和属组信息。

命令使用示例:

  1. 新建一个文本文件test.txt,用ll命令可以看到该文件的属主和属组是root。whoami命令可以查看当前Shell环境登录的用户名。img
  2. 创建两个用户。

img

img

  1. 修改test.txt文件的属主用户为test。 img
  2. 修改test.txt文件的属主和属组为admin。 img

chgrp命令

chgrp命令用于修改文件的属组。

命令使用示例:

将test.txt文件的属组改为root。

img

磁盘管理命令

cat命令

cat命令描述:该命令用于连接文件并打印到标准输出设备上。

cat命令语法:

cat [参数] [文件名]

参数说明:

参数 说明
-n 由1开始对所有输出的行数进行编号。
-b 由1开始对所有输出的行数进行编号,对于空白行不编号。
-s 当遇到有连续两行以上的空白行,就替换为一行的空白行。
-E 在每行结束处显示$。
-T 将 TAB 字符显示为^I。

cat命令使用示例:

a. 执行如下命令,将一个自增序列写入test1.txt文件中。

for i in $(seq 1 10); do echo $i >> test1.txt ; done

img

b. 执行如下命令,查看文件test1.txt内容。

cat test1.txt

返回结果如下所示。

img

c. 执行如下命令,将test1.txt的文件内容加上行号后输入到test2.txt文件。

cat -n test1.txt > test2.txt

img

d. 执行如下命令,查看文件test2.txt内容。

cat test2.txt

返回结果如下所示。

img

e. 执行如下命令,将test1.txt文件内容清空。

cat /dev/null > test1.txt

img

f. 执行如下命令,查看文件test1.txt内容。

cat test1.txt

返回结果如下所示,您可以看到test1.txt文件没有任何内容。

img

cmp命令

cmp命令描述:该命令用于比较两个文件是否有差异。当相互比较的两个文件完全一样时,该指令不会显示任何信息。否则会标示出第一个不同之处的字符和列数编号。当不指定任何文件名称,或文件名为"-",则cmp指令会从标准输入设备读取数据。

cmp命令语法:

cmp [-clsv][-i <字符数目>][--help][第一个文件][第二个文件]

参数说明:

参数 说明
-c 除了标明差异处的十进制字码之外,一并显示该字符所对应字符。
-i <字符数目> 指定一个数目。
-l 标示出所有不一样的地方。
-s 不显示错误信息。
-v 显示版本信息。
–help 在线帮助。

cmp命令使用示例:

a. 执行如下命令,将一个自增序列1-5写入test1.txt文件中。

for i in $(seq 1 5); do echo $i >> test1.txt ; done

img

b. 执行如下命令,比较test1.txt文件和test2.txt文件是否相同。

cmp test1.txt test2.txt

返回结果如下所示,您可以看到test1.txt文件和test2.txt文件第一行就有不同之处。

img

diff命令

diff命令描述:该命令用于比较文件的差异。diff命令以逐行的方式,比较文本文件的异同处。如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录。

diff命令语法:

diff [参数] [文件或目录1] [文件或目录2]

参数说明:

参数 说明
-<行数> 指定要显示多少行的文本。此参数必须与-c或-u参数一并使用。
-c 显示全部内文,并标出不同之处。
-u 以合并的方式来显示文件内容的不同。
-a diff预设只会逐行比较文本文件。
-b 不检查空格字符的不同。
-d 使用不同的演算法,以较小的单位来做比较。
-i 不检查大小写的不同。
-y 以并列的方式显示文件的异同之处。
-W<宽度> 在使用-y参数时,指定栏宽。

diff命令使用示例:

执行如下命令,比较test1.txt文件和test2.txt文件,以并排格式输出。

diff test1.txt test2.txt -y -W 50

img

返回结果如下所示,您可以看到test1.txt文件和test2.txt文件的不同之处。

img

file命令

file命令描述:该命令用于辨识文件类型。

file命令语法:

file [参数] [文件]

参数说明:

参数 说明
-b 列出辨识结果时,不显示文件名称。
-c 详细显示指令执行过程,便于排错或分析程序执行的情形。
-f<名称文件> 指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称。
-L 直接显示符号连接所指向的文件的类别。
-v 显示版本信息。
-z 解读压缩文件的内容。

file命令使用示例:

a. 执行如下命令,显示test1.txt文件类型。

file test1.txt

img

返回结果如下所示,您可以看到test1.txt文件类型是ASCII text。

img

b. 执行如下命令,显示test2.txt文件类型并不显示文件名称。

file -b test2.txt

img

find命令

find命令描述:该命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

find命令语法:

find [参数] [文件]

参数说明:

参数 说明
-mount 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件。
-amin n 在过去n分钟内被读取过文件。
-type c 文件类型是c的文件。
-cmin n 在过去n分钟内被修改过。
-name name 查找文件名称为name的文件。

find命令使用示例:

a. Execute the following command to list .txtthe files with the suffix of all files in the current directory and its subdirectories.

find . -name "*.txt"

img

The returned result is shown below.

img

b. Execute the following command to find all ordinary files whose file length is 0 in the system, and list their full paths.

find / -type f -size 0 -exec ls -l {} \;

img

The returned result is shown below.

img

Guess you like

Origin blog.csdn.net/qq_43475285/article/details/122816873