Manage directories and files
管理目錄和檔案
Previously, you explored how to manage the file system using Linux commands. The following commands were introduced: mkdir, rmdir, touch, rm, mv, and cp. In this reading, you’ll review these commands, the nano text editor, and learn another way to write to files.
之前,您已經探索過如何使用 Linux 命令來管理文件系統。以下命令已被介紹:mkdir、rmdir、touch、rm、mv 和 cp。在本閱讀中,您將回顧這些命令、nano 文本編輯器,並學習另一種寫入文件的方法。
Creating and modifying directories
創建和修改目錄
mkdir
The mkdir command creates a new directory. Like all of the commands presented in this reading, you can either provide the new directory as the absolute file path, which starts from the root, or as a relative file path, which starts from your current directory.
mkdir 命令用於創建新目錄。與本閱讀中介紹的所有命令一樣,您可以提供新目錄的絕對文件路徑(從根目錄開始)或相對文件路徑(從當前目錄開始)。
For example, if you want to create a new directory called network in your /home/analyst/logs directory, you can enter mkdir /home/analyst/logs/network to create this new directory. If you’re already in the /home/analyst/logs directory, you can also create this new directory by entering mkdir network
例如,如果您想在 /home/analyst/logs 目錄中創建一個名為 network 的新目錄,可以輸入 mkdir /home/analyst/logs/network 來創建這個新目錄。如果您已經在 /home/analyst/logs 目錄中,也可以通過輸入 mkdir network 來創建這個新目錄。.
Pro Tip: You can use the ls command to confirm the new directory was added.
專業提示:您可以使用 ls 指令來確認新目錄已被添加。
rmdir
The rmdir command removes, or deletes, a directory. For example, entering rmdir /home/analyst/logs/network would remove this empty directory from the file system.
rmdir 指令用於移除或刪除目錄。例如,輸入 rmdir /home/analyst/logs/network 將會從檔案系統中移除這個空目錄。
Note: The rmdir command cannot delete directories with files or subdirectories inside. For example, entering rmdir /home/analyst returns an error message.
注意:rmdir 命令無法刪除內含文件或子目錄的目錄。例如,輸入 rmdir /home/analyst 會返回一個錯誤訊息。
Creating and modifying files
創建和修改文件
touch and rm touch 和 rm
The touch command creates a new file. This file won’t have any content inside. If your current directory is /home/analyst/reports, entering touch permissions.txt creates a new file in the reports subdirectory called permissions.txt
touch 命令會創建一個新文件。這個文件裡面不會有任何內容。如果您當前的目錄是 /home/analyst/reports,輸入 touch permissions.txt 會在 reports 子目錄中創建一個名為 permissions.txt 的新文件。.
The rm command removes, or deletes, a file. This command should be used carefully because it’s not easy to recover files deleted with rm. To remove the permissions file you just created, enter rm permissions.txt.
rm 命令用於移除或刪除文件。此命令應謹慎使用,因為用 rm 刪除的文件不容易恢復。要移除您剛創建的 permissions 文件,請輸入 rm permissions.txt。
Pro Tip: You can verify that permissions.txt was successfully created or removed by entering ls
專家提示:您可以輸入 ls 來確認 permissions.txt 是否已成功創建或移除.
mv and cp mv 和 cp
You can also use mv and cp when working with files. The mv command moves a file or directory to a new location, and the cp command copies a file or directory into a new location. The first argument after mv or cp is the file or directory you want to move or copy, and the second argument is the location you want to move or copy it to.
在處理文件時,您也可以使用 mv 和 cp。mv 命令將文件或目錄移動到新位置,而 cp 命令則將文件或目錄複製到新位置。mv 或 cp 後的第一個參數是您想要移動或複製的文件或目錄,第二個參數是您想要移動或複製到的位置。
To move permissions.txt into the logs subdirectory, enter mv permissions.txt /home/analyst/logs. Moving a file removes the file from its original location. However, copying a file doesn’t remove it from its original location. To copy permissions.txt into the logs subdirectory while also keeping it in its original location, enter cp permissions.txt /home/analyst/logs
要將 permissions.txt 移動到 logs 子目錄中,輸入 mv permissions.txt /home/analyst/logs。移動檔案會將檔案從其原始位置移除。然而,複製檔案不會將其從原始位置移除。要將 permissions.txt 複製到 logs 子目錄中,同時保留在其原始位置,輸入 cp permissions.txt /home/analyst/logs。.
Note: The mv command can also be used to rename files. To rename a file, pass the new name in as the second argument instead of the new location. For example, entering mv permissions.txt perm.txt renames the permissions.txt file to perm.txt
注意:mv 命令也可以用來重新命名檔案。要重新命名檔案,將新名稱作為第二個參數傳入,而不是新位置。例如,輸入 mv permissions.txt perm.txt 將 permissions.txt 檔案重新命名為 perm.txt.
nano text editor nano 文本編輯器
nano is a command-line file editor that is available by default in many Linux distributions. Many beginners find it easy to use, and it’s widely used in the security profession. You can perform multiple basic tasks in nano, such as creating new files and modifying file contents.
nano 是一個命令行文件編輯器,預設在許多 Linux 發行版中可用。許多初學者覺得它易於使用,並且在安全專業領域中被廣泛使用。你可以在 nano 中執行多個基本任務,例如創建新文件和修改文件內容。
To open an existing file in nano from the directory that contains it, enter nano followed by the file name. For example, entering nano permissions.txt from the /home/analyst/reports directory opens a new nano editing window with the permissions.txt file open for editing. You can also provide the absolute file path to the file if you’re not in the directory that contains it.
要在包含現有文件的目錄中使用 nano 開啟文件,輸入 nano 後接文件名。例如,從 /home/analyst/reports 目錄輸入 nano permissions.txt,會開啟一個新的 nano 編輯窗口,並打開 permissions.txt 文件以供編輯。如果您不在包含該文件的目錄中,也可以提供文件的絕對路徑。
You can also create a new file in nano by entering nano followed by a new file name. For example, entering nano authorized_users.txt from the /home/analyst/reports directory creates the authorized_users.txt file within that directory and opens it in a new nano editing window.
您也可以通過輸入 nano 後接新的文件名來在 nano 中創建新文件。例如,從 /home/analyst/reports 目錄輸入 nano authorized_users.txt,會在該目錄中創建 authorized_users.txt 文件,並在新的 nano 編輯窗口中打開它。
Since there isn't an auto-saving feature in nano, it’s important to save your work before exiting. To save a file in nano, use the keyboard shortcut Ctrl + O. You’ll be prompted to confirm the file name before saving. To exit out of nano, use the keyboard shortcut Ctrl + X
由於 nano 沒有自動保存功能,因此在退出前保存您的工作很重要。要在 nano 中保存文件,使用鍵盤快捷鍵 Ctrl + O。系統會提示您在保存前確認文件名。要退出 nano,使用鍵盤快捷鍵 Ctrl + X。.
Note: Vim and Emacs are also popular command-line text editors.
注意:Vim 和 Emacs 也是受歡迎的命令列文字編輯器。
Standard output redirection
標準輸出重定向
There’s an additional way you can write to files. Previously, you learned about standard input and standard output. Standard input is information received by the OS via the command line, and standard output is information returned by the OS through the shell.
還有另一種方法可以寫入文件。之前,你已經學習了標準輸入和標準輸出。標準輸入是操作系統通過命令行接收的信息,而標準輸出是操作系統通過 shell 返回的信息。
You’ve also learned about piping. Piping sends the standard output of one command as standard input to another command for further processing. It uses the pipe character (|).
你也學習過管道。管道將一個命令的標準輸出作為標準輸入發送到另一個命令以進行進一步處理。它使用管道字符 (|)。
In addition to the pipe (|), you can also use the right angle bracket (>) and double right angle bracket (>>) operators to redirect standard output.
除了管道 (|) 之外,你還可以使用右尖括號 (>) 和雙右尖括號 (>>) 運算符來重定向標準輸出。
When used with echo, the > and >> operators can be used to send the output of echo to a specified file rather than the screen. The difference between the two is that > overwrites your existing file, and >> adds your content to the end of the existing file instead of overwriting it. The > operator should be used carefully, because it’s not easy to recover overwritten files.
當與 echo 一起使用時,> 和 >> 運算符可以用來將 echo 的輸出發送到指定的文件,而不是顯示在螢幕上。兩者的區別在於 > 會覆蓋現有的文件,而 >> 則是將內容添加到現有文件的末尾,而不是覆蓋它。> 運算符應該謹慎使用,因為覆蓋的文件不容易恢復。
When you’re inside the directory containing the permissions.txt file, entering echo "last updated date" >> permissions.txt adds the string “last updated date” to the file contents. Entering echo "time" > permissions.txt after this command overwrites the entire file contents of permissions.txt with the string “time”.
當你在包含 permissions.txt 文件的目錄中時,輸入 echo "last updated date" >> permissions.txt 會將字串“last updated date”添加到文件內容中。在此命令之後輸入 echo "time" > permissions.txt 會用字串“time”覆蓋整個 permissions.txt 文件的內容。
Note: Both the > and >> operators will create a new file if one doesn’t already exist with your specified name.
注意:如果您指定的名稱尚不存在文件,> 和 >> 運算符都會創建一個新文件。
Key takeaways 關鍵要點
Knowing how to manage the file system in Linux is an important skill for security analysts. Useful commands for this include: mkdir, rmdir, touch, rm, mv, and cp. When security analysts need to write to files, they can use the nano text editor, or the > and >> operators.
了解如何在 Linux 中管理文件系統是安全分析師的一項重要技能。這方面有用的命令包括:mkdir、rmdir、touch、rm、mv 和 cp。當安全分析師需要寫入文件時,他們可以使用 nano 文本編輯器,或使用 > 和 >> 運算符。