Linux--初识和基本的指令(2)

目录

前言

1. 指令

1.1 cd其它携带指令

1.2 ls其它携带指令

1.3 which指令

1.4 alias指令

1.5 mkdir其他携带指令

1.7 yum -y install 安装指令

1.8 stat指令

1.9解决指令失控状态

1.10  rmdir&&rm指令

1.11 man指令

1.12 cp指令

1.13 mv指令

1.14 nano 指令

2.在Linux中写代码

 3.概念补充

上篇文章: 


前言

        本章我们将学习更多的Linux指令,和大家进一步了解Linux ,最后会介绍如何用Linux编写代码。


1. 指令


1.1 cd其它携带指令

        1.cd -:返回最近访问目录。如下图我们第一次pwd是在/root/data路径下,随后我们cd进入home,再cd -我们就到了上一次(最近)访问的路径下了,如果再使用cd -的话又会回到home路径下,频繁的使用cd -就可以再两个路径下来回跳转。(方便我们做路径的切换)

        2.cd ~:进入用户家目录。(家目录是在Linux和其他类Unix,windows操作系统中,每个用户登录时所分配的一个目录。它通常位于用户的用户名下,用于存储用户的个人文件和配置文件。)任何用户首次登陆,所处的路径都是自己的家目录。


1.2 ls其它携带指令

ll:ls -l的别名,ll就是ls -l指令,这个别名是操作系统自己设定的。


1.11 man指令

 Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是。
man 语法: man [选项] 命令、

解释一下,面手册分为8章
1 是普通的命令
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
3 是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件
5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义
6 是给游戏留的,由各个游戏自己定义
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
8 是系统管理用的命令,这些命令只能由root使用,如ifconfig

 


1.3 which指令

which:找到所给名称的路径。


1.4 alias指令

alias:可以给指定的指令起别名。

语法:alias 别名='指令'。


1.5 mkdir其他携带指令

 mkdir -p:创建一串路径,它可以递归地创建目录,即如果某个目录的父目录不存在,则会先创建父目录,然后再创建子目录。用法如下,就得到了一串目录。


1.6  tree指令

tree:用于显示文件和目录结构的命令。它可以递归地列出指定目录下的所有文件和子目录,并以树状结构展示。在1.6我们使用mkdir -p创建了一串路径,可我们想查他父目录下的全部子目录,这时我们就可以使用tree。

(这就是a下面的所有子目录)

如果你第一次使用,出现了下面这种情况(找不到这个命令)。

这是就要用到安装命令的命令。请看1.7。


1.7 yum -y install 安装指令

yum -y install:是在使用 Yellowdog Updater Modified (YUM) 软件包管理器的 Linux 发行版中安装软件包的命令。该命令会在不提示确认的情况下安装指定的软件包。

现在就代表安装成功了,可以使用安装的指令了。


1.8 stat指令

stat:了解文件更加详细的信息。

任何文件都有三种时间:1. "Access"(访问)指的是文件或文档的创建时间。它表示文件最初是何时被创建或首次被用户或程序访问的。这个时间戳有助于追踪文件的年龄或起源。

2. "Modify"(修改)指的是文件或文档的修改时间。它表示文件最后一次被修改或编辑的时间。每当对文件进行任何更改,如添加、删除或修改其内容时,这个时间戳都会更新。

3. "Change"(改变)可以是一个更通用的术语,包括文件或文档的创建时间和修改时间。它指的是改变或调整与文件相关的时间。这可能涉及更改创建时间、修改时间或两者。具体更改的目的或上下文将决定哪些时间戳会受到影响。


1.9解决指令失控状态

CTRL+D:中断当前正在运行的程序或进程。

当我们误操作在根目录下使用tree+. 查看根目录下的子目录,由于根目录下的文件量非常大,这时程序一时停不下来,这时我们就可以使用CTRL+C停止程序。

(只要是影响你命令行输入的,直接CTRL+C就可以了。)


1.10  rmdir&&rm指令

rmdir:用于删除空目录的命令。如果指定的目录不为空,则命令将无法删除该目录。

一般情况下我们是不会使用rmdir删除目录,在这里我们只是用它来进行对比。接下来请看rm命令。


rm:它可以删除指定的文件

rm -r:删除目录,可以递归地删除目录及其所有内容。需要注意的是,使用rm命令删除的文件和目录将无法恢复,因此需要谨慎使用。(需要格外注意的是,不要误操作删除根目录,这等于windows系统中的格式化C盘,这样操作系统就直接挂掉了)。

eg 1:我们试着删除一下普通文件,这时候我们看见,操作系统会用户要不要删除,这种情况只有在主用户下才会出现,因为主用户的文件一般比较重要,操作系统会提醒你。这时你可以输入n取消删除,输入y表示确定删除。

如果你不想看见他问这个问题,那么你可以在rm后面加上-f就可以直接删除了。

eg 2:删除目录,使用rm -r,再加上f,(rm -rf)表示我要删除目录,而且是强制删除,不要问我删不删。


1.11 man指令

Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是man 语法: man [选项] 命令
eg:man rm

进入手册后,按回车上下翻,按q退出。

如果不能使用man的话,安装就行了

 解释一下,面手册分为8章
1 是普通的命令
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
3 是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件
5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义
6 是给游戏留的,由各个游戏自己定义
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
8 是系统管理用的命令,这些命令只能由root使用,如ifconfig


1.12 cp指令

语法:cp [选项] 源文件或目录 目标文件或目录

功能: 复制文件或目录

Description: The cp command is used to copy files or directories. If two or more files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

Common options:

1.-f or --force forcefully copies a file or directory, regardless of whether the destination file or directory already exists.

2. -r recursively processes the files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.

The directory is defined recursively, so we cannot copy the directory directly.

If we copy the directory, we just add -r. Generally, rf is used together and can be written as (cp -rf).


1.13 mv command

 mv: The mv command is the abbreviation of move. It can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.

Syntax: mv [options] source file or directory target file or directory

Function: 1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.

        2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. specified target file name.

        ​​​​ 3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.

Commonly used options: -f: force means force. If the target file already exists, it will be overwritten directly without asking.

                   -i: If the destination file (destination) already exists, it will ask whether to overwrite it!


1.14 nano command

nano: In Linux, nano is a text editor that is relatively simple and easy to use. It provides basic text editing functions and has some commonly used shortcut keys. To use the nano command, enter the following command in the terminal.

Syntax: nano [file name]

If the nano command is not available, just use the yum -y install command to install it.

这将打开一个新的终端窗口,并在其中显示文件的内容。您可以使用箭头键来移动光标,进行编辑。
一些常用的快捷键包括:

- Ctrl + O:保存文件
- Ctrl + X:退出  nano  编辑器
- Ctrl + G:获取帮助
- Ctrl + K:剪切一行
- Ctrl + U:粘贴剪切的内容

这只是一些  nano  编辑器的基本命令和快捷键。
您可以通过按Ctrl + G来获取更多帮助和详细的命令列表。

请注意, nano是一个基本的文本编辑器,如果您需要更高级的功能和选项,
您可能需要考虑使用其他编辑器,如  vim  或  emacs  。

1.15 cat command

Syntax: cat [options][file]

Function: View the contents of the target file

Common options: -b number non-empty output lines

                   -N all row numbers output

                   -S does not output multi-line empty lines 


2. Write code in Linux

1. Use touch to create a file with the suffix .c

2. Use nano to open the .c file and write the code. CTRL+X Save and exit.

3.gcc [file name] obtains the file a.out

4. ./a.out to run the program.


 3. Concept supplement

 Here is an additional concept: Each instruction is an executable program used to perform a specific operation or task. When you enter a command into the terminal, the operating system interprets and executes the program represented by the command.

        The commands are stored in the /usr/bin directory

What are you doing when installing and uninstalling?

        ​ ​ ​Copy/delete the executable program to the system path.

Instructions, programs, and executable programs are all the same thing, they are all files (= content + attributes).

Previous article: 

Linux--First introduction and basic instructions (1)-CSDN Blog

Guess you like

Origin blog.csdn.net/2301_76618602/article/details/134482622