linux bash命令
Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.
歡迎使用我們的Linux命令行最終指南。 本教程將向您展示一些關鍵的Linux命令行技術,并向您介紹Bash腳本語言。
什么是Bash? (What is Bash?)
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 Linux operating system can be done via command line. Some examples are…
Linux操作系統可以通過命令行完成許多事情。 一些例子是…
- Editing files 編輯檔案
- Adjusting the volume of the operating system 調整操作系統的音量
- Fetching web pages from the internet 從互聯網上獲取網頁
- Automating work you do every day 每天執行的工作自動化
You can read more about bash here, via the GNU Documentation, and via the tldp guide.
您可以在此處通過GNU文檔和tldp指南閱讀有關bash的更多信息。
在命令行上使用bash(Linux,OS X) (Using bash on the command line (Linux, OS X))
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!”。 到終端。
編寫bash腳本 (Writing a bash script)
You can also put all of your bash commands into a .sh file, and run them from the command line. Say you had a bash script with the following contents:
您還可以將所有bash命令放入.sh文件,然后從命令行運行它們。 假設您有一個包含以下內容的bash腳本:
#!/bin/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對待的特殊指令。
為什么在腳本文件的開頭使用#!/ bin / bash? (Why did we use #!/bin/bash at the beginning of the script file?)
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 Unix that the file is to be executed by /bin/bash. This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #!
is often referred to as a “hash-bang”, “she-bang” or “sha-bang”. Though it is only executed if you run your script as an executable. For example, when you type ./scriptname.extension
, it will look at the top line to find out the interpreter, whereas, running the script as bash scriptname.sh
, first line is ignored.
這是因為讓交互式外殼程序知道為隨后的程序運行哪種解釋器是一種約定。 第一行告訴Unix,該文件將由/ bin / bash執行。 這是幾乎每個Unix系統上Bourne shell的標準位置。 將#!/ bin / bash添加為腳本的第一行,告訴操作系統調用指定的shell來執行腳本中后面的命令。 #!
通常被稱為“哈希爆炸”,“爆炸”或“爆炸”。 盡管僅當您將腳本作為可執行文件運行時才執行。 例如,當您鍵入./scriptname.extension
,它將在第一行中查找解釋程序,而以bash scriptname.sh
身份運行腳本時,第一行將被忽略。
Then you could run the script like so: For make file executable you should call this command under sudo chmod +x “filename”.
然后,您可以像這樣運行腳本:為使文件可執行,應在sudo chmod + x“ filename”下調用此命令。
zach@marigold:~$ ./myBashScript.sh
Hello world!
The 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 which is “Hello World”.
該腳本只有兩行。 第一個指示用于運行文件的解釋器(在本例中為bash)。 第二行是我們要使用的命令,echo,然后是我們要打印的“ 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.
然后執行腳本。
Linux命令行:Bash Cat (Linux Command Line: Bash Cat)
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 is derived from its function to concatenate files.
Cat用于順序讀取文件并將其打印到標準輸出。 這個名字是從它的功能,騙子貓 enate文件導出。
用法 (Usage)
cat [options] [file_names]
Most used options:
最常用的選項:
-b
, numer 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
,顯示非打印字符,制表符和行尾字符除外
例 (Example)
Print in terminal 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
Linux命令行:Bash cd (Linux Command Line: Bash cd)
Change Directory to the path specified, for example cd projects
.
將目錄更改為指定的路徑,例如cd projects
。
There are a few really helpful arguments to aid this:
有一些非常有用的參數可以幫助實現這一點:
.
refers to the current directory, such as./projects
.
引用當前目錄,例如./projects
..
can be used to move up one folder, usecd ..
, and can be combined to move up multiple levels../../my_folder
..
可以用于向上移動一個文件夾,使用cd ..
,并且可以組合用于向上移動多個級別../../my_folder
/
is the root of your system to reach core folders, such assystem
,users
, etc./
是系統訪問核心文件夾(例如system
,users
等)的根目錄。~
is the home directory, usually the path/users/username
. Move back to folders referenced relative to this path by including it at the start of your path, for example~/projects
.~
是主目錄,通常是路徑/users/username
。 通過將其包含在路徑的開頭,將其移回相對于此路徑引用的文件夾,例如~/projects
。
Linux命令行:Bash頭 (Linux Command Line: Bash head)
Head is used to print the first ten lines (by default) or any other amount specified of a file or files. Cat is used to read a file sequentially and print it to the standard output. ie prints out the entire contents of the entire file. - that is not always necessary, 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用于打印前十行(默認情況下)或一個或多個文件指定的任何其他數量。 Cat用于順序讀取文件并將其打印到標準輸出。 即打印出整個文件的全部內容。 -這并非總是必要的,也許您只想檢查文件的內容以查看它是否正確,或者檢查它確實不是空的。 head命令允許您查看文件的前N行。
if more than on file is called then the first ten lines of each file is displayed, unless specific number of lines are specified. Choosing to display the file header is optional using the option below
如果調用的文件多于文件上的文件,那么將顯示每個文件的前十行,除非指定了特定的行數。 使用以下選項選擇顯示文件頭是可選的
用法 (Usage)
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
,總是打印出文件頭
例 (Example)
head file.txt
Prints in terminal the first ten lines of file.txt (default)
在終端中打印file.txt的前十行(默認)
head -n 7 file.txt
Prints in terminal the first seven lines of file.txt
在終端中打印file.txt的前七行
head -q -n 5 file1.txt file2.txt
Print in terminal the first 5 lines of file1.txt, followed by the first 5 lines of file2.txt
在終端中打印file1.txt的前5行,然后打印file2.txt的前5行
Linux Command Line: Bash ls
Linux命令行:Bash ls
ls
is a command on Unix-like operating systems to list contents of a directory, for example folder and file names.
ls
是類似Unix的操作系統上的命令,用于列出目錄的內容,例如文件夾和文件名。
用法 (Usage)
cat [options] [file_names]
Most used options:
最常用的選項:
-a
, all files and folders, including ones that are hidden and start with a.
-a
,所有文件和文件夾,包括隱藏的文件和文件夾,并以.
開頭.
-l
, List in long format-l
,以長格式列出-G
, enable colorized output.-G
,啟用彩色輸出。
例: (Example:)
List files in freeCodeCamp/guide/
在freeCodeCamp/guide/
列出文件
ls ? master
CODE_OF_CONDUCT.md bin package.json utils
CONTRIBUTING.md gatsby-browser.js plugins yarn.lock
LICENSE.md gatsby-config.js src
README.md gatsby-node.js static
assets gatsby-ssr.js translations
Linux命令行:Bash man (Linux Command Line: Bash man)
Man, the abbreviation of manual, is a bash command used to display on-line reference manuals of the given command.
人, 人 UAL的縮寫,是用于顯示給定的命令的上線參考手冊bash命令。
Man displays the reletive man page (short for manual page) of the given command.
男子將顯示給定的命令的reletive手冊頁(以下簡稱人 UAL 頁 )。
用法 (Usage)
man [options] [command]
Most used options:
最常用的選項:
-f
, print a short description of the given command-f
,打印給定命令的簡短描述-a
, display, in succession, all of the available intro manual pages contained within the manual-a
,連續顯示手冊中包含的所有可用的入門手冊頁
例 (Example)
Display the man page of ls:
顯示ls的手冊頁:
man ls
Linux命令行:Bash mv (Linux Command Line: Bash mv)
Moves files and folders.
移動文件和文件夾。
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
在覆蓋文件之前提示確認。
That's all. Go forth and use Linux.
就這樣。 繼續并使用Linux。
翻譯自: https://www.freecodecamp.org/news/linux-command-line-bash-tutorial/
linux bash命令