終端定時任務 開始緩沖
by Luciano Strika
通過盧西亞諾·斯特里卡(Luciano Strika)
如何開始使用終端以提高生產力 (How to start using the terminal to be more productive)
As developers, the terminal can be our second home.
作為開發人員,碼頭可以成為我們的第二故鄉。
However, we can’t use it until we learn how to, and need to practice using it to learn, really — it’s a catch-22!
但是,只有在我們學習如何使用它之前,我們才能使用它,并且需要練習使用它來學習,這確實是一個陷阱-22!
I hope this introduction will solve that puzzle for you. I want to help you start making use of the terminal right away.
我希望本文能為您解決這個難題。 我想幫助您立即開始使用終端。
入門 (Getting Started)
I’ll cover the basics first, so if you know all the things in this article stay tuned for the next ones, where I’ll tackle more advanced topics.
我將首先介紹基礎知識,因此,如果您知道本文中的所有內容,請繼續關注下一篇,我將在其中討論更高級的主題。
With that taken care of, I’ll start from the very beginning. If you’re on Ubuntu, all you have to do to open your terminal is press ctrl+alt+. On a Mac, you should press cmd+spacebar, start typing terminal and press enter when the option appears.
處理完這些后,我將從頭開始。 如果您使用的是Ubuntu,則只需按ctrl + alt +即可打開終端。 上 在Mac上,您應該按cmd +空格鍵,開始輸入終端 ,然后在出現選項時按Enter。
In both cases, you should see a dark background with your username followed by your computer’s name (in Linux) or the reverse order (in a Mac).
在這兩種情況下,您都應該看到黑色背景,其中包含用戶名,后跟計算機名稱(在Linux中)或相反的順序(在Mac中)。
I strongly advise you to open your own terminal and try these commands out on an empty directory, to see for yourself and get the hang of them.
我強烈建議您打開自己的終端,并在一個空目錄中嘗試這些命令,以親自看看并掌握其中的內容。
You’ll see a prompt inviting you to type commands. To enter a command just type it down and press enter. Some navigation commands are:
您會看到一個提示,邀請您鍵入命令。 要輸入命令,只需將其鍵入,然后按Enter。 一些導航命令是:
cd:移動工作目錄。 (cd : Moving your working directory.)
cd <relative path>
This will make your terminal point to a different directory, from which you can run new commands. For instance, if you’re in a folder called animals with three folders cats, dogs, and turtles, you’d run
這將使您的終端指向另一個目錄,您可以從中運行新命令。 例如,如果您位于一個名為動物的文件夾中,而這三個文件夾為cat , dogs , 和海龜 ,你會跑
cd turtles
to move into that directory. To move up one level from the current directory (e.g., moving back to animals from turtles), hit
移到該目錄。 要從當前目錄上移一個級別(例如,從海龜移回動物 ),請點擊
cd ..
mkdir并觸摸:創建文件夾或文件。 (mkdir and touch: Creating folders or files.)
If you need to create a new, empty directory, all you have to do is run
如果您需要創建一個新的空目錄,則只需運行
mkdir <directory name>
Whereas running
而跑步
touch <file_name>
will create an empty file in the current working directory, with the first argument as its name.
將在當前工作目錄中創建一個空文件,并將第一個參數作為名稱。
If another file with the given name already existed, this will only update the file’s last update date. It will not make any changes to its content.
如果已經存在具有給定名稱的另一個文件,則只會更新文件的最后更新日期。 它不會對其內容進行任何更改。
‘But could I possibly know if the file exists?!’ You ask. Well, I’m glad you’re asking.
“但是我可能知道文件是否存在嗎?!” 你問。 好吧,我很高興你問。
ls:查看目錄的內容。 (ls : See a directory’s contents.)
The ls command lists the name of every file and directory inside the current working directory, in alphabetical order. You can pass it a few arguments by using dashes, like this:
ls命令按字母順序列出當前工作目錄中每個文件和目錄的名稱。 您可以使用破折號向它傳遞一些參數,如下所示:
ls -a -l
In this case, the -a argument makes ls show invisible files. The -l command makes the output look like a list. It displays one row for each item, with some extra data like the size of each file or its creation date.
在這種情況下, -a參數使ls顯示不可見的文件。 -l命令使輸出看起來像一個列表。 它為每個項目顯示一行,并帶有一些額外的數據,例如每個文件的大小或其創建日期。
One of my favorite arguments for ls is -R, which recursively calls ls on each listed subdirectory for a quick look into a repository or file tree.
我對ls最喜歡的參數之一是-R,它遞歸地在每個列出的子目錄上調用ls ,以快速查看存儲庫或文件樹。
Note that for all commands, arguments can actually be combined after a single dash:
請注意,對于所有命令,參數實際上可以在短劃線后合并:
ls -alR
Now I hear you asking ‘How in the world am I going to remember all of these arguments and options? Do all commands have so many crazy features?’But don’t worry — we got you covered.
現在,我聽到您在問:“我將如何記住所有這些論點和選擇? 所有命令是否都具有這么多瘋狂的功能?'但請放心,我們已為您覆蓋。
男人:永遠不要停止學習! (man : Never stop learning!)
If you’ve been in Stack Overflow or Reddit, you’ve probably come across the phrase ‘read the man pages’ used either educationally or as an insult. I’m here for the first use.
如果您曾經在Stack Overflow或Reddit中學習過,您可能會碰到“讀手冊頁”這兩個詞在教育上或侮辱性的使用。 我在這里是第一次使用。
Try running
嘗試跑步
man <command name>
It will display that command’s man page — official documentation, with all of its possible arguments and uses. Most of us use it when we’re sure a certain program did something, but we can’t quite remember which flag made it do it. It’s also very good to call man on a command the first time you use it (for instance, if it shows up in a google result), to learn a bit more about it and maybe find better ways to invoke it. To close a man page, just press Q.
它將顯示該命令的手冊頁-官方文檔,以及所有可能的參數和用法。 我們大多數人在確定某個程序已執行某些操作時會使用它,但我們不記得是哪個標志使它執行了操作。 初次使用man時(例??如,如果它出現在google結果中)調用man也是很好的,以了解更多有關它的信息,并可能找到更好的方法來調用它。 要關閉手冊頁,只需按Q。
頭,尾巴,貓和更少:讀取文件的內容。 (head and tail, cat and less : Read a file’s contents.)
Calling head or tail on a file will show you its first or last 10 lines, respectively.Some cool arguments you can call it with are:
調用文件的頭或尾將分別顯示其前10行或后10行。您可以使用以下一些很酷的參數來調用它:
-n <number>: display number lines instead of the default 10
-n <數字 >: 顯示 數字行而不是默認的10行
-f (for tail) : Show the lines in real time and don’t stop (perfect for keeping tabs on a log file when you ssh into a server)
-f (用于tail ):實時顯示各行,并且不停止(在ssh進入服務器時,可以在日志文件中保留制表符)
Calling cat will simply display a file’s content. Make sure you’re using it on actual text files, or you’ll see some trippy stuff.
呼叫cat只會顯示文件的內容。 確保在實際的文本文件上使用它,否則會看到一些令人毛骨悚然的東西。
If you call cat on a large (or even large-ish, to be honest) file, you’ll probably find it pretty awkward to keep scrolling up and down, looking for the relevant lines. There’s actually a more convenient way of doing that: the less command.
如果你調用一個大型貓科動物 (甚至large- 十歲上下 ,要誠實)的文件,你可能會發現它非常尷尬,以保持向上和向下滾動,尋找相關線路。 實際上,有一種更方便的方法: 更少的命令。
less will show you less of a file by loading its contents in a buffered way. You can scroll the file with the arrow keys instead of using the mouse wheel/touchpad, which is a lot more comfortable. You can also press /, type something in and press Enter to search the file (like using ctrl+f).To exit less mode, just press Q.
less將以緩沖方式加載文件內容,從而顯示較少的文件。 您可以使用箭頭鍵滾動文件,而不是使用鼠標滾輪/觸摸板,這更加舒適。 您也可以按/,輸入內容,然后按Enter鍵搜索文件(例如使用ctrl + f )。要退出省少模式,只需按Q。
cp和mv:復制,剪切和粘貼。 (cp and mv : Copy, cut and paste.)
cp (copy) and mv (move) are the bash equivalents to copy and cut, respectively. You can use them like this:
cp (復制)和mv (移動) 是分別復制和剪切的bash等效項。 您可以像這樣使用它們:
cp <source> <destination>
To copy the file(s) in source to destination.
將源中的文件復制到目標。
The source can either be a file, or a set of files. To select more than one file, you can leverage bash’s wildcard character: *. This character will match any string, even an empty one.
源可以是一個文件或一組文件。 要選擇多個文件,可以利用bash的通配符: * 。 此字符將匹配任何字符串,甚至是空字符串。
For instance, this command will copy all files in the some_folder folder into the some_other_folder folder, situated one level upwards in the file system.
例如,該命令將所有文件復制在some_folder文件夾放到some_other_folder文件夾,在文件系統位于一個指標向上。
cp some_folder/* ../some_other_folder
But if we wanted to only move the .txt files into a directory called texts, we’d use:
但是,如果我們只想將.txt文件移動到名為texts的目錄中,則可以使用:
cp *.txt texts/
since * matches any string. Ee are enforcing its ending in .txt. (for instance, *.txt matches filename.txt, since * matches filename, but not filename.xtt, since even though * matches the whole name, there’s nothing that matches .txt).
因為*匹配任何字符串。 EE正在以.txt強制其結尾。 (例如,* .TXT FILENAME.TXT相匹配,因為*匹配文件名,但不filename.xtt,因為即使*匹配全名,沒有什么是匹配.TXT)。
The destination can be a file’s path (overwriting the current file in that path, if it exists, or creating a new one otherwise) if the source is a single file, or a directory name if you wish to copy/move many files.
如果源是單個文件,則目標可以是文件的路徑(如果存在,則覆蓋該路徑中的當前文件;否則,可以創建一個新文件);如果要復制/移動許多文件,則目標可以是目錄名。
rm:刪除文件和目錄。 (rm : Deleting files and directories.)
The opposite of touch, rm deletes a file or directory.
與觸摸相反,rm刪除文件或目錄。
Using it in its default form
以其默認形式使用
rm file_name
will work when deleting a file, but throw an error when deleting a directory. This prevents us from deleting important files in a directory, or a whole directory thinking it’s just a file.
在刪除文件時將起作用,但在刪除目錄時將引發錯誤。 這樣可以防止我們刪除目錄中的重要文件,或認為它只是文件而刪除整個目錄。
To bypass this, if you feel courageous, just add -r, to recursively delete every file in a directory until it’s empty, before deleting it like some kind of serial deleter. If you only feel like deleting the empty directories, use -d instead.
要避免這種情況,如果您感到膽怯,只需添加-r,以遞歸方式刪除目錄中的每個文件,直到其為空為止,然后像某種串行刪除器一樣將其刪除。 如果只想刪除空目錄,請使用-d 。
Note that you can always use the wildcard (*) character to delete many files or directories in a single command. For instance, calling
請注意,您始終可以使用通配符(*)刪除單個命令中的許多文件或目錄。 例如,打電話
rm *.txt
removes all text files from the current working directory.
從當前工作目錄中刪除所有文本文件。
結束...現在。 (The end… for now.)
Whew, that was some introduction. You’re now familiar with the most common commands you’ll be using in your daily programming life.
哇,那是一些介紹。 現在,您已經熟悉了日常編程生活中將要使用的最常見命令。
There are a lot of things I didn’t cover yet. I plan to make a follow up with more use cases, more commands and more real problems to solve.
我還沒有涉及很多東西。 我計劃跟進更多用例,更多命令和更多實際問題來解決。
While I prepare the next article, I’d like to encourage you to try these commands on your own. See which ones save you time, and get used to this whole terminal thing. Maybe bookmark this article and use it for reference. I won’t tell anyone.
在準備下一篇文章時,我鼓勵您自己嘗試這些命令。 看看哪些可以節省您的時間,并習慣于整個終端過程。 也許將本文添加為書簽并用作參考。 我不會告訴任何人。
I promise you, after a while you’ll start to see why it’s worth it. (I know it took me a while). Eventually you’ll just instinctively open the terminal every time you start doing something.
我保證,一段時間后,您將開始明白它為什么值得。 (我知道花了我一段時間)。 最終,每次您開始做某事時,您都會本能地打開終端。
I hope you found some of this introduction useful, and if so please let me know! I value my readers’ feedback a lot. This is the main reason I’m writing, so please tell me if some part was hard to understand, some commands seem useless, or my tutorial is simply too boring. Also let me know if some part was interesting!
希望本文對您有所幫助,如果是,請告訴我! 我非常重視讀者的反饋意見。 這是我正在編寫的主要原因,所以請告訴我是否有些部分難以理解,某些命令似乎無用,或者我的教程太無聊了。 還請讓我知道某個部分是否有趣!
Part 2 is already available.
第2部分已經可用 。
Follow me for more programming tutorials, tips and tricks. You can also read my articles on www.datastuff.tech
跟我來了解更多編程教程,技巧和竅門。 您也可以在www.datastuff.tech上閱讀我的文章
翻譯自: https://www.freecodecamp.org/news/how-you-can-be-more-productive-right-now-using-bash-29a976fb1ab4/
終端定時任務 開始緩沖