linux gcc 示例
Linux is a powerful operating system that powers most servers and most mobile devices. In this guide, we will show you examples of how to use some of its most powerful features. This involves using the Bash command line.
Linux是功能強大的操作系統,可為大多數服務器和大多數移動設備提供動力。 在本指南中,我們將向您展示如何使用其一些最強大功能的示例。 這涉及使用Bash命令行。
12個簡單實用的Linux命令 (12 Simple and Useful Linux Commands)
The commands listed here are basic, and will help you get started quickly. But they’re also powerful, and they’ll continue to be useful as your Linux expertise expands.
此處列出的命令是基本命令,可幫助您快速入門。 但是它們也很強大,隨著您的Linux專業知識的擴展,它們將繼續發揮作用。
echo
Command: This takes the text you give it and sends it somewhere— back to the screen, to a file, or to another command. Example:echo "hello!"
echo
命令:這會將您提供的文本發送到某個地方-返回屏幕,文件或另一個命令。 示例:echo "hello!"
cat
Command: To display the contents of a text file, just typecat myfile
.cat
命令:要顯示文本文件的內容,只需鍵入cat myfile
。find
Command: It does what it says, and it’s good at it. Use it to locate files by path, size, date, owner and a bunch of other useful filters. Example:find . -type f -mtime -1h # List files in this directory modified in the past hour
.find
Command:它按其說的做,而且很擅長。 使用它可以按路徑,大小,日期,所有者和其他許多有用的過濾器查找文件。 示例:find . -type f -mtime -1h # List files in this directory modified in the past hour
find . -type f -mtime -1h # List files in this directory modified in the past hour
。date
Command: Just type date when you want to know what time it is. Example:date "+It's %l:%m%p on %A"
. Use it in a script to name files according to the current date.date
命令:只要想知道現在幾點,就輸入date。 示例:date "+It's %l:%m%p on %A"
。 在腳本中使用它可以根據當前日期命名文件。ls
Command: What’s in this directory? Combinels
with some useful flags to display and sort directory contents by date and size. It also gives you lots of options for formatting the output.ls
命令:此目錄中有什么? 將ls
與一些有用的標志結合使用,以按日期和大小顯示和排序目錄內容。 它還為您提供了許多格式化輸出的選項。pwd
Command: Where am I? Linux can be unforgiving, particularly when you delete something. Make sure you know are before you issue your commands.pwd
命令:我在哪里? Linux可能會令人寬容,尤其是在刪除某些內容時。 發出命令之前,請確保您已知道。mail
Command: Linux’s mail program isn’t good looking, but it can be really helpful. You can create a message and add text, recipients, and attachments all in one command. Example:echo "We're having a great time." | mail -s "Wish you were here!" -A postcard.png -t mom@example.com
mail
命令:Linux的郵件程序雖然外觀不好,但是確實很有幫助。 您可以創建一條消息,并在一個命令中添加文本,收件人和附件。 示例:echo "We're having a great time." | mail -s "Wish you were here!" -A postcard.png -t mom@example.com
echo "We're having a great time." | mail -s "Wish you were here!" -A postcard.png -t mom@example.com
cut
Command: When you have a string with separators in it, usecut
to filter out certain fields. Example:echo "this, that, and the other" | cut -d, -f2 # "that"
cut
命令:當字符串中包含分隔符時,請使用cut
過濾掉某些字段。 示例:echo "this, that, and the other" | cut -d, -f2 # "that"
echo "this, that, and the other" | cut -d, -f2 # "that"
grep
Command: To find lines of text that contain a certain string, use grep. Example:grep 'root' /etc/passwd # root:x:0:0:root:/root:/bin/bash
grep
命令:要查找包含特定字符串的文本行,請使用grep。 示例:grep 'root' /etc/passwd # root:x:0:0:root:/root:/bin/bash
sed
Command: Usesed
to find and change a substring in a piece of text. Example:echo "this, that, and the other" | sed 's/that/those/' # "this, those, and the other"
sed
命令:使用sed
查找和更改一段文本中的子字符串。 示例:echo "this, that, and the other" | sed 's/that/those/' # "this, those, and the other"
echo "this, that, and the other" | sed 's/that/those/' # "this, those, and the other"
shutdown
Command: Use this command to shut down the system and turn off the power. Example:shutdown -h now
shuts down system immediately.shutdown -h +5
shuts down system after five minutes.shutdown
命令:使用此命令可關閉系統并關閉電源。 示例:shutdown -h now
立即關閉系統。shutdown -h +5
五分鐘后關閉系統。wget
Command: Usewget
to download files from the command line.wget
命令:使用wget
下載文件的命令行。Example:
例:
wget http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
wget http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
Use these commands in scripts and at the command line. They’re all very powerful commands, and Linux’s main page has a lot more information about each one.
在腳本和命令行中使用這些命令。 它們都是非常強大的命令,Linux主頁上有很多有關每個命令的信息。
Linux用戶管理命令示例 (Linux User Management Commands Example)
Also, important commands used for System Administrators are following:
另外,用于系統管理員的重要命令如下:
uptime
Command: In Linux, the uptime command shows how long your system has been running and the number of users are currently logged in. It also displays load average for 1,5 and 15 minutes intervals.uptime
命令:在Linux中,uptime命令顯示系統已運行了多長時間以及當前已登錄的用戶數。它還顯示了1,5和15分鐘間隔的平均負載。w
Command: This will display users currently logged in and their process, along with load averages. Also shows the login name, tty name, remote host, login time, idle time, JCPU, PCPU, command and processes.w
命令:它將顯示當前登錄的用戶及其進程以及平均負載。 還顯示登錄名,tty名稱,遠程主機,登錄時間,空閑時間,JCPU,PCPU,命令和進程。users
Command: displays currently logged in users. This command don’t have other parameters other than help and version.users
命令:顯示當前登錄的用戶。 該命令除了help和version之外沒有其他參數。who
Command: the who command simply returns the user name, date, time and host information. The who command is similar to the w command. Unlike the w command, who doesn’t print what users are doing.who
命令:who命令僅返回用戶名,日期,時間和主機信息。 who命令類似于w命令。 與w命令不同,誰不打印用戶正在做什么。whoami
Command: the whoami command prints the name of the current user. You can also use “who am i” to display the current user. If you are logged in as a root using sudo command, “whoami” returns root as current user. Use “who am i” if you want to know the exact user logged in.whoami
命令:whoami命令顯示當前用戶的名稱。 您也可以使用“我是誰”來顯示當前用戶。 如果使用sudo命令以root用戶身份登錄,“ whoami”將以root用戶身份返回root。 如果您想知道確切的用戶登錄名,請使用“我是誰”。ls
Command: ls displays a list of files in human readable format.ls
命令:ls以易于閱讀的格式顯示文件列表。crontab
Command: list schedule jobs for current user with the crontab command and -l option.crontab
命令:使用crontab命令和-l選項列出當前用戶的調度作業。less
Command: the less command allows you to quickly view a file. You can page up and down. Press ‘q‘ to quit from the less window.less
命令:less命令可讓您快速查看文件。 您可以上下翻頁。 按“ q”從較少窗口中退出。more
Command: the more command allows you to quickly view a file and shows details in percentage. You can page up and down. Press ‘q‘ to quit out from the more window.more
命令:more命令可讓您快速查看文件并以百分比顯示詳細信息。 您可以上下翻頁。 按“ q”退出更多窗口。cp
Command: Copy a file from its source to its destination while preserving the same mode.cp
命令:在保留相同模式的同時,將文件從其源復制到目標。
如何建立使用者 (How to Create a User)
使用adduser
或useradd
命令將新用戶添加到系統中。 (Use the adduser
or useradd
command to add a new user to your system.)
$ sudo adduser username
Be sure to replace username
with the user that you want to create.
確保將username
替換為您要創建的用戶。
使用passwd
命令更新新用戶的密碼。 (Use the passwd
command to update the new user’s password.)
$ sudo passwd username
A strong password is highly recommended!
強烈建議您使用強密碼!
如何創建一個Sudo用戶 (How to Create a Sudo User)
To create a sudo
user, you need to create a regular user first using the command above, then add this user to the group of sudoers
using the usermod
command.
要創建sudo
用戶,您需要首先使用上面的命令創建一個普通用戶,然后使用usermod
命令將該用戶添加到sudoers
組中。
在Debian系統(Ubuntu / LinuxMint / ElementryOS)上, sudo
組的成員具有sudo特權。 (On Debian systems (Ubuntu/LinuxMint/ElementryOS), members of the sudo
group have sudo privileges.)
$ sudo usermod -aG sudo username
在基于RHEL的系統(Fedora / CentO)上, wheel
組的成員具有sudo特權。 (On RHEL based syatems (Fedora/CentOs), members of the wheel
group have sudo privilages.)
$ sudo usermod -aG wheel username
如何刪除用戶 (How to Delete a User)
對于Debian(Ubuntu) (For Debian (Ubuntu))
$ sudo deluser username
對于RHEL(Fedora / CentOS) (For RHEL (Fedora/CentOS))
$ sudo userdel username
創建組并添加用戶 (Creating groups and adding users)
$ sudo groupadd editorial
$ sudo usermod -a -G editorial username
注意:以上所有命令在root
模式下無需sudo即可執行 (Note: All the above commands can be executed without sudo in root
mode)
To switch to root on ubuntu, run the ?su -i
command followed by the password of the user logged in. Prompt changes to #
instead of $
.
要切換到ubuntu上的root用戶,請運行su -i
命令,然后輸入登錄用戶的密碼。提示更改為#
而不是$
。
在Debian系統(Ubuntu / LinuxMint / ElementryOS)上, sudo
組的成員具有sudo特權。 (On Debian systems (Ubuntu/LinuxMint/ElementryOS), members of the sudo
group have sudo privileges.)
$ sudo usermod -aG sudo username
如何建立群組 (How to Create a Group)
To create a group, use the command groupadd
要創建組,請使用命令groupadd
$ sudo groupadd groupname
如何刪除群組 (How to delete group)
To delete a group, use the command ‘groupdel’
要刪除組,請使用命令“ groupdel”
$ sudo groupdel grouname
Linux查找命令示例 (The Linux Find Command Example)
使用查找命令 (Using the Find Command)
The Linux find command is a powerful tool to help you locate files and directories on your server. With a little practice, you can easily track things down based on name, type, size, or date (when they were created or last updated).
Linux find命令是一個功能強大的工具,可幫助您在服務器上查找文件和目錄。 稍作練習,您就可以輕松地根據名稱,類型,大小或日期(創建或最后更新的時間)來跟蹤內容。
Think of find as your eager helper:
將find視為您急切的幫助者:
You: “I’m looking for something on my server.”
您:“我正在服務器上尋找東西。”
Find: “I can help! What can you tell me about it?”
發現:“我可以幫助您! 你能告訴我什么?”
You: “It was a file, bigger than 2GB, somewhere under my home directory, updated in the last 48 hours.”
您:“它是一個文件,大于2GB,位于我的主目錄下,在過去48小時內已更新。”
Find: “Tada!”
找到:“多田!”
Find is a program, so really you’d have to tell it find ~ -type f -size +2G
.
Find是一個程序,所以您真的必須告訴它find ~ -type f -size +2G
。
Here are some sample commands using find:
以下是一些使用find的示例命令:
find ~ -type d # Show me all the subdirectories inside my home directory
find ~ -type d # Show me all the subdirectories inside my home directory
find / -type f -name 'todo.txt' # Show me files named 'todo.txt' anywhere under the root directory (i.e. anywhere)
find / -type f -name 'todo.txt' # Show me files named 'todo.txt' anywhere under the root directory (ie anywhere)
The first parameter always names the directory in which we’ll look. In our examples above, these are ~ (home directory of the current user) and / (root directory of the filesystem).
第一個參數始終命名我們將在其中查找的目錄。 在上面的示例中,它們是?(當前用戶的主目錄)和/(文件系統的根目錄)。
Other parameters are optional and can be combined in any ways you find useful:
其他參數是可選的,可以以您認為有用的任何方式組合:
- The type parameter allows you to constrain the search for files only (f), directories only (d) or symbolic links (l). If you omit the type parameter, you’ll be searching for all of these types. 使用type參數可以限制僅搜索文件(f),僅目錄(d)或符號鏈接(l)的搜索。 如果省略type參數,則將搜索所有這些類型。
- The name parameter lets you specify what you want to find by name, either with a literal string (‘filename.txt’) or using wildcards (‘file?.*’). 通過name參數,您可以使用文字字符串('filename.txt')或通配符('file?。*')指定要按名稱查找的內容。
man find
will show you many more parameters, and is worth reviewing. Find can locate files by name, user, creation date, size and much more. Next time you’re looking for something, find it!
man find
將為您顯示更多參數,并且值得回顧。 查找可以按名稱,用戶,創建日期,大小等查找文件。 下次您要尋找東西時,找到它!
Linux dd命令示例 (Linux dd Command Example)
The “dd” command can be used to create a file of a specific size. This is useful if you would like to test download speeds, or any other tests, and need a file of a specific size.
“ dd”命令可用于創建特定大小的文件。 如果您想測試下載速度或任何其他測試,并且需要特定大小的文件,這將很有用。
dd if=/dev/zero of=file_name.txt bs=1024k count=10
This will create a file of 1MB called file_name.txt.
這將創建一個1MB的文件,名為file_name.txt。
bs is your byte size and count represent the number of blocks. An easy way to look at is 1024K X 10.
bs是您的字節大小,計數表示塊數。 一個簡單的方法是1024K X 10。
Here is an even simpler way to create a 1MB file:
這是創建1MB文件的更簡單方法:
dd if=/dev/zero of=file_name.txt bs=1MB count=1
如何編寫Linux Bash腳本的示例 (Example of how to write a Linux Bash Script)
編寫Bash腳本 (Writing a Bash Script)
By typing commands on the Linux command line, you can give the server instructions to get some simple tasks done. A shell script is a way to put together a series of instructions to make this easier. Shell scripts become even more powerful when you add logic like if
and while
to automatically control how they behave as circumstances change.
通過在Linux命令行上鍵入命令,您可以為服務器提供指令以完成一些簡單的任務。 Shell腳本是一種將一系列指令組合在一起的方法,可簡化此操作。 當您添加諸如if
和while
類的邏輯以自動控制它們隨環境變化的行為時,Shell腳本將變得更加強大。
什么是Bash? (What’s Bash?)
Bash is the name of a command line interpreter, a program that makes sense of the Linux commands you enter at the command prompt, or in your script.
Bash是命令行解釋器的名稱,該程序可以理解您在命令提示符下或腳本中輸入的Linux命令。
腳本中有什么? (What’s in a Script?)
A script is just a file. A basic script is made up of an introductory line that tells the server what to make of it, and one or more instructions to execute. Here’s an example:
腳本只是一個文件。 一個基本腳本由一個介紹性行和一個或多個要執行的指令組成,該行告訴服務器該怎么做。 這是一個例子:
#!/bin/bash
echo "Hi. I’m your new favorite bash script."
The first line has special meaning, which we’ll discuss below. The second line is just a Linux command, one you could type out on the command line.
第一行具有特殊含義,我們將在下面討論。 第二行只是一個Linux命令,您可以在命令行中鍵入該命令。
有何評論? (What’s a Comment?)
Comments are text you add to your script that you intend bash to ignore. Comments start with a pound sign, and are useful for annotating your code so you and other users can understand it.
注釋是您添加到腳本中希望bash忽略的文本。 注釋以井號開頭,對于注釋您的代碼很有用,以便您和其他用戶可以理解它。
To add a comment, type the #
character, followed by any text that’s helpful you. Bash will ignore the #
and everything after it.
要添加評論,請鍵入#
字符,然后輸入任何對您有幫助的文本。 Bash將忽略#
及其后面的所有內容。
Note: the first line of the script is not a comment. This line is always first, always starts with #!
and has special meaning to bash.
注意:腳本的第一行不是注釋。 此行始終是第一行,始終以#!
開頭#!
對bash有特殊的意義
Here’s the script from before, commented:
這是之前的腳本,評論道:
#!/bin/bash # Designates the path to the bash program. Must start with '#!' (but isn't a comment).
echo "Hi. I’m your new favorite bash script." # 'echo' is a program that sends a string to the screen.
執行腳本 (Executing a Script)
You can open a text editor, paste that example code and save the file, and you’ve got a script. Scripts are conventionally named ending in ‘.sh,’ so you might save that code as myscript.sh.
您可以打開一個文本編輯器,粘貼該示例代碼并保存文件,然后便有了一個腳本。 腳本通常以“ .sh”結尾,因此您可以將該代碼另存為myscript.sh。
The script won’t execute until we do 2 things:
在執行以下兩項操作之前,腳本不會執行:
First, make it executable. (We’ll only have to do this once.) Linux relies extensively on file permissions. They determine a lot about how your server behaves. There’s a lot to know about permissions, but for now we only need to know this: you can’t run your script until you give yourself execute permissions. To do that, type:
首先,使其可執行。 (我們只需要這樣做一次。)Linux廣泛依賴于文件權限。 它們決定了服務器的行為方式。 關于權限有很多要了解的內容,但是現在我們只需要知道這一點:您必須給自己執行權限后才能運行腳本。 為此,請鍵入:
chmod +x my script.sh
chmod +x my script.sh
Second, run it. We execute the script from the command line just like any other command like ls
or date
. The script name is the command, and you need to precede it with a ‘./’ when you call it:
其次,運行它。 與其他命令(例如ls
或date
一樣,我們從命令行執行腳本。 腳本名稱是命令,調用時需要在其前面加上“ ./”:
./myscript.sh # Outputs "Hi. I'm your new favorite bash script." (This part is a comment!)
./myscript.sh # Outputs "Hi. I'm your new favorite bash script." (This part is a comment!)
有條件的 (Conditionals)
Sometimes you want your script to do something only if something else is true. For example, print a message only if a value is below a certain limit. Here’s an example of using if
to do that:
有時,您希望腳本僅在其他情況正確時才執行某些操作。 例如,僅當值小于某個限制時才打印消息。 這是使用if
進行操作的示例:
#!/bin/bashcount=1 # Create a variable named count and set it to 1if [[ $count -lt 11 ]]; then # This is an if block (or conditional). Test to see if $count is 10 or less. If it is, execute the instructions inside the block.echo "$count is 10 or less" # This will print, because count = 1.
fi # Every if ends with fi
Similarly, we can arrange the script so it executes an instruction only while something is true. We’ll change the code so that the value of the count variable changes:
同樣,我們可以安排腳本,以便僅在某些情況正確時才執行指令。 我們將更改代碼,以使count變量的值更改:
#!/bin/bashcount=1 # Create a variable named count and set it to 1while [[ $count -lt 11 ]]; do # This is an if block (or conditional). Test to see if $count is 10 or less. If it is, execute the instructions inside the block.echo "$count is 10 or less" # This will print as long as count <= 10.count=$((count+1)) # Increment count
done # Every while ends with done
The output of this version of myscript.sh will look like this:
此版本的myscript.sh的輸出將如下所示:
"1 is 10 or less"
"2 is 10 or less"
"3 is 10 or less"
"4 is 10 or less"
"5 is 10 or less"
"6 is 10 or less"
"7 is 10 or less"
"8 is 10 or less"
"9 is 10 or less"
"10 is 10 or less"
真實腳本 (Real World Scripts)
These examples aren’t terribly useful, but the principles are. Using while
, if
, and any command you might otherwise type manually, you can create scripts that do valuable work.
這些示例并不是非常有用,但是原理是有用的。 使用while
, if
和其他可能手動鍵入的命令,可以創建執行重要工作的腳本。
翻譯自: https://www.freecodecamp.org/news/linux-example-bash-command-line/
linux gcc 示例