博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux gcc 示例_最好的Linux示例
阅读量:2518 次
发布时间:2019-05-11

本文共 16702 字,大约阅读时间需要 55 分钟。

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专业知识的扩展,它们将继续发挥作用。

  1. 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!"

  2. cat Command: To display the contents of a text file, just type cat myfile.

    cat命令:要显示文本文件的内容,只需键入cat myfile

  3. 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

  4. 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" 。 在脚本中使用它可以根据当前日期命名文件。

  5. ls Command: What’s in this directory? Combine ls 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与一些有用的标志结合使用,以按日期和大小显示和排序目录内容。 它还为您提供了许多格式化输出的选项。

  6. 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可能会令人宽容,尤其是在删除某些内容时。 发出命令之前,请确保您已知道。

  7. 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

  8. cut Command: When you have a string with separators in it, use cut 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"

  9. 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

  10. sed Command: Use sed 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"

  11. 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五分钟后关闭系统。

  12. wget Command: Use wget to download files from the command line.

    wget命令:使用wget下载文件的命令行。

    Example:

    例:

    wget

    wget

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:

另外,用于系统管理员的重要命令如下:

  1. 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分钟间隔的平均负载。

  2. 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,命令和进程。

  3. users Command: displays currently logged in users. This command don’t have other parameters other than help and version.

    users命令:显示当前登录的用户。 该命令除了help和version之外没有其他参数。

  4. 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命令不同,谁不打印用户正在做什么。

  5. 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。 如果您想知道确切的用户登录名,请使用“我是谁”。

  6. ls Command: ls displays a list of files in human readable format.

    ls命令:ls以易于阅读的格式显示文件列表。

  7. crontab Command: list schedule jobs for current user with the crontab command and -l option.

    crontab命令:使用crontab命令和-l选项列出当前用户的调度作业。

  8. 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”从较少窗口中退出。

  9. 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”退出更多窗口。

  10. cp Command: Copy a file from its source to its destination while preserving the same mode.

    cp命令:在保留相同模式的同时,将文件从其源复制到目标。

如何建立使用者 (How to Create a User)

使用adduseruseradd命令将新用户添加到系统中。 (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脚本是一种将一系列指令组合在一起的方法,可简化此操作。 当您添加诸如ifwhile类的逻辑以自动控制它们随环境变化的行为时,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/bashecho "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:

其次,运行它。 与其他命令(例如lsdate一样,我们从命令行执行脚本。 脚本名称是命令,调用时需要在其前面加上“ ./”:

./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 countdone # 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.

这些示例并不是非常有用,但是原理是有用的。 使用whileif和其他可能手动键入的命令,可以创建执行重要工作的脚本。

翻译自:

linux gcc 示例

转载地址:http://cqrwd.baihongyu.com/

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
win7(64位)php5.5-Apache2.4-mysql5.6环境安装
查看>>
生成商户订单号/退款单号
查看>>
使用Android OpenGL ES 2.0绘图之六:响应触摸事件
查看>>
我们过去几年做对了哪些事
查看>>