ls
is a command on Unix-like operating systems to list contents of a directory, for example folder and file names.
ls
是类似Unix的操作系统上的命令,用于列出目录的内容,例如文件夹和文件名。
cat [options] [file_names]
Most used options:
最常用的选项:
-a
, all files and folders, including ones that are hidden and start with a .
-a
,所有文件和文件夹,包括隐藏的文件和文件夹,并以.
开头.
-l
, list all files in long format
-l
,以长格式列出所有文件
-G
, enable colorized output
-G
,启用彩色输出
List files in freeCodeCamp/guide/
在freeCodeCamp/guide/
列出文件
After cloning the main freeCodeCamp repo, here is the output after running ls
in the freeCodeCamp
directory:
克隆主freeCodeCamp存储库后,这是在freeCodeCamp
目录中运行ls
后的输出:
api-server docker-compose.yml public
change_volumes_owner.sh Dockerfile.tests README.md
client docs sample.env
CODE_OF_CONDUCT.md HoF.md search-indexing
config lerna.json SECURITY.md
CONTRIBUTING.md LICENSE.md server
curriculum node_modules tools
docker-compose-shared.yml package.json utils
docker-compose.tests.yml package-lock.json
head
is used to print the first ten lines (by default) or any other amount specified of a file or files. cat
, on the other hand, is used to read a file sequentially and print it to the standard output (that is, it prints out the entire contents of the file).
head
用于打印前十行(默认情况下)或文件指定的任何其他数量。 另一方面, cat
用于顺序读取文件并将其打印到标准输出(即,打印出文件的全部内容)。
That is not always necessary, though – perhaps you just want to check the contents of a file to see if it is the correct one, or check that it is indeed not empty. The head
command allows you to view the first N lines of a file.
但是,这并非总是必要的–也许您只想检查文件的内容以查看它是否正确,或者检查它是否确实为空。 head
命令允许您查看文件的前N行。
If more than on file is called, then the first ten lines of each file is displayed, unless a specific number of lines are specified. Choosing to display the file header is optional using the option below.
如果调用的文件多于文件上的文件,那么将显示每个文件的前十行,除非指定了特定的行数。 使用下面的选项来选择显示文件头是可选的。
head [options] [file_name(s)]
Most used options:
最常用的选项:
-n N
, prints out the first N lines of the file(s)
-n N
,打印出文件的前N行
-q
, doesn’t print out the file headers
-q
,不打印出文件头
-v
, always prints out the file headers
-v
,总是打印出文件头
head file.txt
Prints the first ten lines of file.txt (default)
打印file.txt的前十行(默认)
head -n 7 file.txt
Prints the first seven lines of file.txt
打印file.txt的前七行
head -q -n 5 file1.txt file2.txt
Prints the first 5 lines of file1.txt, followed by the first 5 lines of file2.txt
打印file1.txt的前5行,然后打印file2.txt的前5行
This bash command moves files and folders.
此bash命令移动文件和文件夹。
mv source target
mv source ... directory
The first argument is the file you want to move, and the second is the location to move it to.
第一个参数是您要移动的文件,第二个参数是将其移动到的位置。
Commonly used options:
常用选项:
-f
to force move them and overwrite files without checking with the user.
-f
强制移动它们并覆盖文件,而无需与用户检查。
-i
to prompt confirmation before overwriting files.
-i
在覆盖文件之前提示确认。
cat
is one of the most frequently used commands in Unix operating systems.
cat
是Unix操作系统中最常用的命令之一。
cat
is used to read a file sequentially and print it to the standard output. The name comes from the way it can concatenates files.
cat
用于顺序读取文件并将其打印到标准输出。 这个名字来源于它可以精读猫 enates文件的方式。
cat [options] [file_names]
Most used options:
最常用的选项:
-b
, number non-blank output lines
-b
,非空白输出行数
-n
, number all output lines
-n
编号所有输出线
-s
, squeeze multiple adjacent blank lines
-s
,挤压多个相邻的空白行
-v
, display nonprinting characters, except for tabs and the end of line character
-v
,显示非打印字符,制表符和行尾字符除外
Print the content of file.txt:
打印file.txt的内容:
cat file.txt
Concatenate the content of the two files and display the result in terminal:
连接两个文件的内容,并在终端中显示结果:
cat file1.txt file2.txt
Bash (short for Bourne Again SHell) is a Unix shell, and a command language interpreter. A shell is simply a macro processor that executes commands. It’s the most widely used shell packaged by default for most Linux distributions, and a successor for the Korn shell (ksh) and the C shell (csh).
Bash (Bourne Again SHell的缩写)是Unix shell,也是命令语言解释器。 外壳程序只是执行命令的宏处理器。 默认情况下,它是大多数Linux发行版中使用最广泛的shell,它是Korn shell(ksh)和C shell(csh)的后继产品。
Many things that can be done in the GUI of a Linux operating system can be done via the command line. Some examples are:
可以通过命令行完成Linux操作系统的GUI中可以完成的许多操作。 一些例子是:
You can read more about bash here, via the GNU Documentation, and via the tldp guide.
您可以在此处通过GNU文档和tldp指南阅读有关bash的更多信息。
You can start using bash on most Linux and OS X operating systems by opening up a terminal. Let’s consider a simple hello world example. Open up your terminal, and write the following line (everything after the $ sign):
您可以通过打开终端来在大多数Linux和OS X操作系统上开始使用bash。 让我们考虑一个简单的hello world示例。 打开您的终端,并编写以下行($符号之后的所有内容):
zach@marigold:~$ echo "Hello world!"
Hello world!
As you can see, we used the echo command to print the string “Hello world!” to the terminal.
如您所见,我们使用echo命令来打印字符串“ Hello world!”。 到终端。
You can also put all of your bash commands into a .sh file, and run them from the command line. Say you have a bash script with the following contents:
您还可以将所有bash命令放入.sh文件,然后从命令行运行它们。 假设您有一个包含以下内容的bash脚本:
#!/bin/bash
echo "Hello world!"
This script only has two lines. The first indicates what interpreter to use to run the file (in this case, bash). The second line is the command we want to use, echo
, followed by what we want to print, here, "Hello world!"
该脚本只有两行。 第一个指示用于运行文件的解释器(在本例中为bash)。 第二行是我们要使用的命令echo
,后跟我们要打印的内容,这里是“ Hello world!”。
It’s worth noting that first line of the script starts with #!
. It is a special directive which Unix treats differently.
值得注意的是,脚本的第一行以#!
开头#!
。 这是Unix对待的特殊指令。
That is because it is a convention to let the interactive shell know what kind of interpreter to run for the program that follows.
这是因为让交互式外壳程序知道为随后的程序运行哪种解释器是一种约定。
The first line tells the operating system that the file should be executed by the program at /bin/bash
, the standard location of the Bourne shell on almost every Unix or Unix-like system. By adding #!/bin/bash
at the beginning of the script, it tells the OS to use the shell at that specific path to execute all the following commands in the script.
第一行告诉操作系统该文件应由程序/bin/bash
执行,该程序是几乎每个Unix或类似Unix的系统上Bourne shell的标准位置。 通过在脚本的开头添加#!/bin/bash
,它告诉OS在该特定路径上使用Shell来执行脚本中的以下所有命令。
#!
goes by many names such as "hash-bang", "she-bang", "sha-bang", or "crunch-bang". Note that this first line is only considered if the script is an executable.
#!
有很多名称,例如“ hash-bang”,“ she-bang”,“ sha-bang”或“ crunch-bang”。 请注意,仅当脚本是可执行文件时才考虑第一行。
For example, if myBashScript.sh
is executable, the command ./myBashScript.sh
will cause the OS will look at the first line figure out which interpreter to use. In this case it would be #!/bin/bash
.
例如,如果myBashScript.sh
是可执行的,则命令./myBashScript.sh
将导致操作系统在第一行查找要使用的解释器。 在这种情况下,它将是#!/bin/bash
。
On the other hand, if you run bash myBashScript.sh
, then the first line is ignored since the OS already knows to use bash.
另一方面,如果运行bash myBashScript.sh
,则第一行将被忽略,因为操作系统已经知道要使用bash。
To make myBashScript.sh
executable, simply run sudo chmod +x myBashScript.sh
. Then run the following command to execute the script:
要使myBashScript.sh
可执行,只需运行sudo chmod +x myBashScript.sh
。 然后运行以下命令以执行脚本:
zach@marigold:~$ ./myBashScript.sh
Hello world!
Sometimes the script won’t be executed, and the above command will return an error. It is due to the permissions set on the file. To avoid that, use:
有时脚本不会被执行,并且上面的命令将返回错误。 这是由于在文件上设置的权限。 为避免这种情况,请使用:
zach@marigold:~$ chmod u+x myBashScript.sh
And then execute the script.
然后执行脚本。