不能理解命令行选项 “n”_如何理解命令行

不能理解命令行选项 “n”

Today, I want to talk about something that I feel many people are either downright afraid of or at least outright hate using: the command line. It’s a beautiful tool once you learn how to use it, but for years, I could not bring myself to even try it — and it seriously hindered my programming journey.

今天,我想谈一谈我觉得很多人要么完全害怕使用,要么至少完全讨厌使用:命令行。 一旦您学会了如何使用它,它就是一个漂亮的工具,但是多年来,我什至无法尝试使用它-并且它严重阻碍了我的编程之旅。

So I want to show you not so much how to use it but rather why you should and what it is good for.

因此,我不想向您展示太多如何使用它,而是向您展示为什么要使用它以及它有什么好处。

太慢了,不是吗? 为什么不只使用鼠标? (It’s Just So Slow, Isn’t It? Why Don’t You Just Use the Mouse?)

Does this feel relatable? Each time you read a tutorial that starts with “bring up a command line,” you sigh, retire into your shell, realize your own inadequacy, the dire state of politics, and the world as a whole.

觉得相关吗? 每次阅读以“打开命令行”开头的教程时,您都会叹息,退休,意识到自己的不足,政治的严峻状况以及整个世界。

Believe me, I used to be there with you. I absolutely hated how slow and cumbersome it seemed to work with a black-and-white text editor. Now I love it. It feels sleek and downright enjoyable. So, yes, I’ve been there and I’ve been here and I can attest to the fact that it feels better to sleep on this side of the divide.

相信我,我曾经和你在一起。 我绝对不喜欢黑白文本编辑器的运行速度如此缓慢和麻烦。 现在我喜欢它。 感觉圆滑而令人愉悦。 所以,是的,我去过那里,我去过这里,我可以证明一个事实,那就是在鸿沟的这一侧睡觉会更好。

您可以更快地进行大规模运营 (You Can Do Mass Operations Much Faster)

For some reason, the hard drive that I use for my 3D rendering insists on making itself read-only every couple of days. I still have not figured out why, but that usually means that I can no longer save my currently open project, which — as you can guess — is not nice.

由于某种原因,我用于3D渲染的硬盘坚持要求每两天将其设为只读。 我仍然没有弄清楚为什么,但是通常这意味着我不能再保存我当前打开的项目,这(您可以猜到)不是很好。

Normally, I would need to go in and manually reset the attributes, taking at least ten minutes out of my day. Instead, I just open up a command line and type the following few lines:

通常,我需要花至少十分钟时间手动设置属性。 相反,我只是打开一个命令行并输入以下几行:

diskpart
list disk
select disk 1
attributes clear disk readonly

This then reverts the whole hard drive to read-write mode in all of three seconds compared to the minutes it would take me using the regular Windows interface. Yes, that is clearly an issue where I am just fighting the symptoms instead of curing the cause, but right now, I don’t have another 5TB drive to back up my data before resetting the file system.

然后,与使用常规Windows界面所需的时间相比,这将使整个硬盘驱动器在三秒钟内全部恢复为读写模式。 是的,这显然是一个问题,我只是在解决症状而不是解决问题,但现在,在重置文件系统之前,我没有另一个5TB驱动器来备份我的数据。

您所做的一切都可以自动重用 (Everything You Do Can Be Automated for Reuse)

The command line is great for running quick scripts, but everything you do in it can also be automated using batch files.

命令行非常适合运行快速脚本,但是您在命令行中所做的所有操作也可以使用批处理文件自动执行。

Just write the same commands as you would, put them into a text file, and change the file extension to .bat. Congratulations, you can now run that script with either a double-click or by using your console to start it.

只需编写与命令相同的命令,然后将它们放入文本文件,然后将文件扩展名更改为.bat即可。 恭喜,您现在可以双击运行该脚本,也可以使用控制台启动该脚本。

Batch file syntax is not necessarily the easiest (it’s arguably the worst), but that doesn’t mean that a bit of Googling and cursing won’t get you where you want.

批处理文件的语法不一定是最简单的(可以说是最差的),但这并不意味着有些谷歌搜索和诅咒无法使您到达所需的位置。

For example, I have one batch script that compiles my Python scripts into executable files:

例如,我有一个批处理脚本,它将我的Python脚本编译为可执行文件:

pyinstaller --onefile ".\awesome_script.py"

And then I even have another batch file that loops through all recursive folders of my script folder and looks for compile.bat files to run them. As you can imagine, this saves a lot of time and more importantly prevents cases where my script.exe files aren’t current and up to date with the Python code I may have fixed in the meantime.

然后,我什至还有另一个批处理文件,该文件循环遍历脚本文件夹的所有递归文件夹,并寻找compile.bat文件来运行它们。 可以想象,这节省了很多时间,更重要的是防止了我的script.exe文件不是最新的并且与我同时修复的Python代码保持最新的情况。

控制台直接与系统连接,而不需要走风景线 (The Console Interfaces Directly With the System Instead of Taking the Scenic Route)

Have you ever tried deleting or copying a huge folder on Windows? Have you noticed it taking so long that you think about early retirement more than usual?

您是否曾经尝试在Windows上删除或复制一个巨大的文件夹? 您是否注意到花了这么长时间以至于比平时更想早退休?

Well, do the same with the console by typing:

好吧,通过键入以下命令对控制台执行相同的操作:

xcopy "path/to/source/" "path/to/destination/"

And you can watch the system run through the tasks at menacing speeds — several times faster than using the Windows explorer. Now you can call that stupid. I’m sure that anti-Windows snobs will find that hilarious because their superior operating system does not have that quirk. But to me, this is the way things are, and having a much faster workaround has saved me hours and days at work during a large migration project we had.

您可以看到系统以惊人的速度运行任务-比使用Windows资源管理器快几倍。 现在,您可以称其为愚蠢的。 我敢肯定,反Windows势利小人会觉得很有趣,因为它们的高级操作系统没有这种怪癖。 但是对我来说,事情就是这样,而更快的解决方法为我节省了一个大型迁移项目所需的时间和时间。

它使您在做某事之前先三思 (It Makes You Think Before You Do Something)

This is more of a “soft benefit,” but I realize that I work a lot more purposefully whenever I have a console in front of me. When I use the mouse, I’m instantly bored and start “mousing around” on the hunt for the menu I’m looking for, then I lose track and switch over to Chrome to search for music that can make this endeavour less painful.

这更多是“软收益”,但是我意识到,只要有控制台在我面前,我就会更有目标地工作。 当我使用鼠标时,我立即感到无聊,开始在寻找菜单上寻找鼠标,然后迷路了,切换到Chrome浏览器搜索音乐,以减轻痛苦。

I’m kidding a bit, of course, but there is truth in that. By using the console, you are much more focused on the task at hand — and you think before you type.

当然,我在开玩笑,但这是真的。 通过使用控制台,您可以更加专注于手头的任务-并在键入之前进行思考。

它具有隐藏的功能,大多数用户无法访问 (It Has Hidden Functions Most Users Don’t Have Access To)

Remember that diskpart thing I mentioned earlier? Most people would not even know of it or know how to search for something that automates read-only attribute reversals.

还记得我之前提到的diskpart吗? 大多数人甚至不知道它,也不知道如何搜索自动执行只读属性反转的操作。

But just by knowing batch exists, I can do just that:

但是只要知道批处理存在,我就可以做到:

google "batch mass set readonly"or google “batch set drive readonly”

谷歌“批量设置只读”或谷歌“批量设置驱动器只读”

And there are so many more. For example, you can mass-rename files, you can create for and while loops, and do a surprising lot of things. I use batch scripts every day to use the denoising app to clean up rendered images, and being able to do that saves me literally hours a day since I can render images at 300 instead of 1k samples with the same result.

还有更多。 例如,您可以批量重命名文件,可以创建forwhile循环,并可以做很多令人惊讶的事情。 我每天都使用批处理脚本来使用去噪应用程序来清理渲染的图像,并且能够做到这一点每天节省了我数小时的时间,因为我可以渲染300次而不是1000个样本的图像而获得相同的结果。

你可以抽烟斗 (You Can Smoke a Pipe)

Piping is one of the most powerful features of the command line because it lets you pass the output of one program into another, effectively chaining commands and tools together into one script.

配管是命令行最强大的功能之一,因为它使您可以将一个程序的输出传递到另一个程序,从而将命令和工具有效地链接到一个脚本中。

generateReport.bat > reportToExcelFile.bat

Read more about piping and chaining in this how-to.

本操作指南中阅读有关管道和链条的更多信息。

A coworker of mine made heavy use of this to have true incremental commits where they had a command-line script that would take each changed file, commit using the same overall commit message with the issue ID and the file name in the title, and then once they were all done, they would push the changes to the remote branch.

我的一个同事大量使用了此方法,以实现真正的增量提交,在这种情况下,他们拥有一个命令行脚本,该脚本将获取每个更改的文件,使用具有问题ID和标题中文件名的相同总体提交消息进行提交,然后完成所有操作后,他们会将更改推送到远程分支。

自动完成功能使命令行使用合理快速 (Autocomplete Makes Command-Line Usage Reasonably Fast)

After a while, you really get the hang of using the keyboard to navigate your file system. In essence, there are only two commands you need:

一段时间后,您真的可以使用键盘导航文件系统了。 本质上,您只需要两个命令:

  • cd (change directory) will let you jump into a subfolder of your current folder — or you can jump up one level by typing cd ..

    cd (更改目录)将使您跳至当前文件夹的子文件夹-或通过键入cd .跳至上一级cd .

  • dir will show you a list of all available files and subfolders inside a folder, letting you know which files you want to access.

    dir将显示文件夹中所有可用文件和子文件夹的列表,让您知道要访问哪些文件。

After that, you can always type the first few letters of the path, then use tab to either autocomplete or toggle through the available options. Hit enter and it all works. After a while, this becomes very fast and second nature. Everything else, you either learn or Google as you need it.

之后,您始终可以输入路径的前几个字母,然后使用Tab键自动完成或在可用选项之间切换。 按Enter键,一切正常。 一段时间后,这变得非常快,成为第二自然。 您可以根据需要学习或学习其他所有内容。

您消除人为错误 (You Take Out the Human Error)

This may sound weird with so much typing and that many chances to make typos, but that’s precisely the weird beauty of it. When you type something and do it wrong, then things won’t work — and you will get an error message.

这么多的打字听起来很奇怪,并且有很多机会打错字,但这恰恰是它的怪异之美。 当您键入某些内容并输入错误时,该内容将无法正常工作,并且您会收到一条错误消息。

You will never have the wrong path because then your script won’t even autocomplete to the right directory where you want to run your script.

您永远不会有错误的路径,因为这样您的脚本甚至都不会自动完成到要运行脚本的正确目录。

This comes into effect a lot more when it comes to tutorials. Now you have a standard that anyone can follow at their own pace instead of having someone search for menus on screen for two minutes in a screencast and then miss the right option, driving you crazy as you watch them struggle. It’s best to use the console and avoid anger management issues.

当涉及到教程时,这会更加有效。 现在,您有了一个标准,任何人都可以按照自己的节奏进行操作,而不是让某人在屏幕录像中搜索屏幕菜单两分钟,然后错过正确的选项,从而使您在观看他们的斗争时发疯。 最好使用控制台并避免愤怒管理问题。

Plus, of course, you can copy-paste console commands instead of painstakingly recreating the steps that this annoyingly helpful person did when they set down and spent time, effort, and money trying to help others.

另外,当然,您可以复制粘贴控制台命令,而不用费力地重新创建这个烦人的人在设置下来并花费时间,精力和金钱尝试帮助他人时所执行的步骤。

你看起来像个黑客 (You Look Like a Hacker)

While obviously a joke, this should not be underestimated. Faking your way through a meeting has actual, palpable benefits. Just reach for your keys and start playing a little keyboard solo as you try to bring your point across in a meeting and you’ll immediately notice that all the right-wrong people are wooed by your immense skills.

虽然这显然是个玩笑,但这不应该被低估。 假装开会确实有明显的好处。 当您试图在会议中表达自己的观点时,只需伸开键并开始独奏一个小键盘,您就会立即注意到,所有对错的人都被您的丰富技能所吸引。

I have made this realization time and time again, and after a while, I stopped feeling like a cheat, as I realized that this is exactly what Sherlock Holmes does: He impresses his clients and bystanders needlessly not to brag but to establish his expertise and allow himself to work faster and with fewer distractions. He also gets people to listen to him, whereas they would ridicule others for the same attempts and conclusions.

我一次又一次地意识到这一点,过了一段时间,我不再觉得自己像个骗子,因为我意识到这正是夏洛克·福尔摩斯所做的:他给客户和旁观者留下了不必要的印象,而不是吹牛,而是建立了自己的专业知识和让自己更快地工作,减少干扰。 他还会使人们听他的话,而他们会嘲笑其他人以同样的尝试和结论。

There is real value in this. I have used it several times to bring meetings that were hopelessly spiraling out of control back on track— and all I did was navigate into a folder to open a .exe file or something instead of using the mouse.

这是真正的价值。 我已经多次使用它来使无法控制的螺旋式会议重新回到正轨上,而我所做的只是导航到一个文件夹中以打开.exe文件或其他内容,而不是使用鼠标。

简介:命令行是可以为您的生产力带来奇迹的工具 (Summary: The Command Line Is a Tool That Can Do Wonders for Your Productivity)

I hope I could show you a small glimpse into the basic benefits of the command line and why it is still a thing in the time of touch screens and mouse input.

希望我能向您简要介绍一下命令行的基本优点,以及为什么在触摸屏和鼠标输入时仍然如此。

And if you like, you can enter the dark void of people who can lead flamewars over which terminal, console, or command-line tools are vastly superior. There are a lot of advanced things that I could not even begin to fit into this article.

而且,如果您愿意,您可以进入一个黑暗的人群,他们可以领导一场激烈的战争,而终端,控制台或命令行工具在这方面是极为优越的。 有很多高级的东西我什至无法开始适合本文。

翻译自: https://medium.com/better-programming/how-to-understand-the-command-line-448cf864dc43

不能理解命令行选项 “n”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值