博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux bash命令_Ultimate Linux命令行指南-Full Bash教程
阅读量:2518 次
发布时间:2019-05-11

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

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 , via the , and via the .

您可以通过和阅读有关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/bashecho "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.shHello 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, use cd .., 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 as system, users, etc.

    /是系统访问核心文件夹(例如systemusers等)的根目录。

  • ~ 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                                                                ⚬ masterCODE_OF_CONDUCT.md bin                package.json       utilsCONTRIBUTING.md    gatsby-browser.js  plugins            yarn.lockLICENSE.md         gatsby-config.js   srcREADME.md          gatsby-node.js     staticassets             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 targetmv 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。

翻译自:

linux bash命令

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

你可能感兴趣的文章
小技巧之a标签自动解析URL
查看>>
51Nod 1099 任务执行顺序 (贪心)
查看>>
Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目
查看>>
时间对象 <-> 定时器 <-> 电子时钟 <-> 倒计时效果
查看>>
Xposed模块开发入门-最基本的项目创建
查看>>
PHP多线程类
查看>>
Mysql主从复制原理及配置
查看>>
Golang- import 导入包的语法(转)
查看>>
FMDB的使用
查看>>
jquery追加元素的不同语法
查看>>
微信开发者工具和开发
查看>>
const 指针的三种使用方式
查看>>
Codeforces 1167C - News Distribution
查看>>
四连测Day2
查看>>
Qt模态对话框和非模态对话框
查看>>
emacs 编译 如何把 emacs 的 el 文件编译为 elc 文件
查看>>
腾讯云云机安装dockers
查看>>
项目接口书写心得(1)
查看>>
Java学习(五)
查看>>
java获取整数的各位数值
查看>>