linux 终端设置技巧_通过这8个技巧成为Linux终端高级用户

linux 终端设置技巧

linux 终端设置技巧

bash tricks header

There’s more to using the Linux terminal than just typing commands into it. Learn these basic tricks and you’ll be well on your way to mastering the Bash shell, used by default on most Linux distributions.

使用Linux终端不仅仅可以在其中输入命令,还可以使用更多功能。 学习这些基本技巧,您将掌握如何在大多数Linux发行版中默认使用的Bash shell。

This one’s for the less experienced users – I’m sure that many of you advanced users out there already know all these tricks. Still, take a look – maybe there’s something you missed along the way.

这是针对经验不足的用户的-我敢肯定,你们中的许多高级用户已经知道所有这些技巧。 不过,请看一下–也许您在此过程中错过了一些东西。

制表符完成 (Tab Completion)

Tab completion is an essential trick. It’s a great time saver and it’s also useful if you’re not sure of a file or command’s exact name.

制表符补全是必不可少的技巧。 这是节省时间的好方法,如果您不确定文件或命令的确切名称,它也很有用。

For example, let’s say you have a file named “really long file name” in the current directory and you want to delete it. You could type the entire file name, but you’d have to escape the space characters properly (in other words, add the \ character before each space) and might make a mistake. If you type rm r and press Tab, Bash will automatically fill the file’s name in for you.

例如,假设您在当前目录中有一个名为“ really long file name”的文件,并且您想要删除它。 您可以键入整个文件名,但必须正确转义空格字符(换句话说,在每个空格之前添加\字符),可能会出错。 如果键入rm r并按Tab,Bash将自动为您输入文件名。

Of course, if you have multiple files in the current directory that begin with the letter r, Bash won’t know which one you want. Let’s say you have another file named “really very long file name” in the current directory. When you hit Tab, Bash will fill in the “really\ “ part, since the files both begin with that. After it does, press Tab again and you’ll see a list of matching file names.

当然,如果当前目录中有多个以字母r开头的文件,则Bash不会知道您要哪个。 假设您在当前目录中有另一个名为“非常长的文件名”的文件。 当您点击Tab时,Bash将填写“ really \”部分,因为文件均以此开头。 完成后,再次按Tab键,您将看到匹配文件名的列表。

tab completion

Continue typing your desired file name and press Tab. In this case, we can type an “l” and press Tab again and Bash will fill in our desired file name.

继续输入所需的文件名,然后按Tab。 在这种情况下,我们可以输入“ l”,然后再次按Tab键,Bash将填写所需的文件名。

This also works with commands. Not sure what command you want, but know it begins with “gnome”? Type “gnome” and press Tab to see a list.

这也适用于命令。 不确定要使用什么命令,但知道它以“ gnome”开头吗? 输入“ gnome”,然后按Tab键查看列表。

管子 (Pipes)

Pipes allow you to send the output of a command to another command. In the UNIX philosophy, each program is a small utility that do one thing well. For example, the ls command lists the files in the current directory and the grep command searches its input for a specified term.

管道允许您将命令的输出发送到另一个命令。 在UNIX哲学中,每个程序都是一个小工具,可以很好地完成一件事。 例如, ls命令列出当前目录中的文件,而grep命令在其输入中搜索指定的术语。

Combine these with pipes (the | character) and you can search for a file in the current directory. The following command searches for the word “word”:

将它们与管道( |字符)结合使用,您可以在当前目录中搜索文件。 以下命令搜索单词“ word”:

ls | grep word

ls | grep word

piping

通配符 (Wild Cards)

The * character – that is, the asterisk – is a wild card that can match anything. For example, if we wanted to delete both “really long file name” and “really very long file name” from the current directory, we could run the following command:

*字符(即星号)是可以匹配任何内容的通配符。 例如,如果要从当前目录中删除“真正的长文件名”和“真正的长文件名”,则可以运行以下命令:

rm really*name

rm real * name

This command deletes all files with file names beginning with “really” and ending with “name.” If you ran rm * instead, you’d delete every file in the current directory, so be careful.

该命令删除所有文件名以“ really”开头并以“ name”结尾的文件。 如果改为运行rm * ,则将删除当前目录中的每个文件,因此请小心。

wild card

输出重定向 (Output Redirection)

The > character redirects a command’s output to a file instead of another command. For example, the following line runs the ls command to list the files in the current directory and, instead of printing that list to the terminal, it prints the list to a file named “file1” in the current directory:

>字符会将命令的输出重定向到文件,而不是另一个命令。 例如,以下行运行ls命令以列出当前目录中的文件,而不是将列表打印到终端,而是将列表打印到当前目录中名为“ file1”的文件:

ls > file1

ls> file1

bash tricks header

命令历史 (Command History)

Bash remembers a history of the commands you type into it. You can use the up and down arrow keys to scroll through commands you’ve recently used. The history command prints a list of these commands, so you can pipe it to grep to search for commands you’ve used recently. There are many other tricks you can use with Bash history, too.

Bash会记住您键入命令的历史记录。 您可以使用向上和向下箭头键滚动浏览最近使用的命令。 history命令将显示这些命令的列表,因此您可以将其通过管道传送到grep,以搜索您最近使用的命令。 您还可以在Bash历史记录中使用其他技巧

history

〜, &.. (~, . & ..)

The ~ character – also known as the tilde – represents the current user’s home directory. So, instead of typing cd /home/name to go to your home directory, you can type cd ~ instead. This also works with relative paths – cd ~/Desktop would switch to the current user’s desktop.

字符(也称为波浪号)代表当前用户的主目录。 因此,您可以键入cd〜 ,而不是输入cd / home / name进入主目录。 这也适用于相对路径– cd〜/ Desktop将切换到当前用户的桌面。

Similarly, the . represents the current directory and the .. represents the directory above the current directory. So, cd .. goes up a directory. These also work with relative paths – if you’re in your Desktop folder and want to go to the Documents folder, which is in the same directory as the Desktop folder, you can use the cd ../Documents command.

同样, 代表当前目录,而..代表当前目录上方的目录。 因此, cd ..进入目录。 它们也可以使用相对路径–如果您位于Desktop文件夹中,并且想要转到与Desktop文件夹位于同一目录中的Documents文件夹,则可以使用cd ../Documents命令。

characters

在后台运行命令 (Run a Command in the Background)

By default, Bash executes every command you run in the current terminal. That’s normally fine, but what if you want to launch an application and continue using the terminal? If you type firefox to launch Firefox, Firefox will take over your terminal and display error messages and other output until you close it. Add the & operator to the end of the command to have Bash execute the program in the background:

默认情况下,Bash执行您在当前终端中运行的每个命令。 通常,这很好,但是如果您要启动应用程序并继续使用终端怎么办? 如果键入firefox启动Firefox,则Firefox将接管您的终端并显示错误消息和其他输出,直到您将其关闭。 在命令末尾添加运算符,以使Bash在后台执行程序:

firefox &

火狐

background process

有条件的执行(Conditional Execution)

You can also have Bash run two commands, one after another. The second command will only execute if the first command completed successfully. To do this, put both commands on the same line, separated by a &&, or double ampersand.

您还可以让Bash运行两个命令,一个接一个。 仅当第一个命令成功完成时,第二个命令才会执行。 为此,请将两个命令放在同一行,并用&&或双“&”号分隔。

For example, the sleep command takes a value in seconds, counts down, and completes successfully. It’s useless alone, but you can use it to run another command after a delay. The following command will wait five seconds, then launch the gnome-screenshot tool:

例如, sleep命令以秒为单位获取一个值,倒计时并成功完成。 单独使用它是没有用的,但是您可以在延迟后使用它来运行另一个命令。 以下命令将等待五秒钟,然后启动gnome-screenshot工具:

sleep 5 && gnome-screenshot

睡眠5 && gnome屏幕截图



Do you have any more tricks to share? Leave a comment and help your fellow readers!

您还有其他技巧可以分享吗? 发表评论并帮助您的其他读者!

翻译自: https://www.howtogeek.com/110150/become-a-linux-terminal-power-user-with-these-8-tricks/

linux 终端设置技巧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值