Navigate Linux and read file content
導航 Linux 並閱讀檔案內容
In this reading, you’ll review how to navigate the file system using Linux commands in Bash. You’ll further explore the organization of the Linux Filesystem Hierarchy Standard, review several common Linux commands for navigation and reading file content, and learn a couple of new commands.
在這篇閱讀中,您將回顧如何使用 Bash 中的 Linux 命令來導航檔案系統。您將進一步探索 Linux 檔案系統層次結構標準的組織,回顧幾個常見的 Linux 命令以進行導航和閱讀檔案內容,並學習幾個新的命令。
Filesystem Hierarchy Standard (FHS)
檔案系統層次結構標準 (FHS)
Previously, you learned that the Filesystem Hierarchy Standard (FHS) is the component of Linux that organizes data. The FHS is important because it defines how directories, directory contents, and other storage is organized in the operating system.
之前,您已經了解到檔案系統層次結構標準 (FHS) 是 Linux 中組織數據的組件。FHS 很重要,因為它定義了目錄、目錄內容和其他存儲在操作系統中的組織方式。
This diagram illustrates the hierarchy of relationships under the FHS:
此圖解說明了在 FHS 下的關係層級:
Under the FHS, a file’s location can be described by a file path. A file path is the location of a file or directory. In the file path, the different levels of the hierarchy are separated by a forward slash (/).
在 FHS(文件系統層次結構標準)中,檔案的位置可以用檔案路徑來描述。檔案路徑是檔案或目錄的位置。在檔案路徑中,不同層級的層次結構由正斜線(/)分隔。
Root directory 根目錄
The root directory is the highest-level directory in Linux, and it’s always represented with a forward slash (/). All subdirectories branch off the root directory. Subdirectories can continue branching out to as many levels as necessary.
根目錄是 Linux 中最高層級的目錄,總是以正斜線(/)表示。所有子目錄都從根目錄分支出來。子目錄可以根據需要繼續分支到多個層級。
Standard FHS directories 標準 FHS 目錄
Directly below the root directory, you’ll find standard FHS directories. In the diagram, home, bin, and etc are standard FHS directories. Here are a few examples of what standard directories contain:
在根目錄的正下方,你會找到標準 FHS 目錄。在圖中,home、bin 和 etc 是標準 FHS 目錄。以下是一些標準目錄所包含內容的例子:
/home: Each user in the system gets their own home directory.
/home:系統中的每個使用者都有自己的主目錄。/bin: This directory stands for “binary” and contains binary files and other executables. Executables are files that contain a series of commands a computer needs to follow to run programs and perform other functions.
/bin:此目錄代表「binary」(二進位)並包含二進位文件及其他可執行文件。可執行文件是包含一系列命令的文件,電腦需要遵循這些命令來運行程式及執行其他功能。/etc: This directory stores the system’s configuration files.
/etc:此目錄存儲系統的配置文件。/tmp: This directory stores many temporary files. The /tmp directory is commonly used by attackers because anyone in the system can modify data in these files.
/tmp:此目錄存儲許多臨時文件。攻擊者通常會利用 /tmp 目錄,因為系統中的任何人都可以修改這些文件中的數據。/mnt: This directory stands for “mount” and stores media, such as USB drives and hard drives.
/mnt:此目錄代表「掛載」,用於存放媒體,如 USB 隨身碟和硬碟。
Pro Tip: You can use the man hier command to learn more about the FHS and its standard directories.
專業提示:您可以使用 man hier 命令來了解更多關於 FHS 及其標準目錄的資訊。
User-specific subdirectories
用戶專屬子目錄
Under home are subdirectories for specific users. In the diagram, these users are analyst and analyst2. Each user has their own personal subdirectories, such as projects, logs, or reports
在 home 目錄下有特定用戶的子目錄。在圖中,這些用戶是 analyst 和 analyst2。每個用戶都有自己的個人子目錄,例如 projects、logs 或 reports。.
Note: When the path leads to a subdirectory below the user’s home directory, the user’s home directory can be represented as the tilde (~). For example, /home/analyst/logs can also be represented as ~/logs
注意:當路徑指向使用者主目錄下的子目錄時,使用者的主目錄可以用波浪號(~)來表示。例如,/home/analyst/logs 也可以表示為 ~/logs。.
You can navigate to specific subdirectories using their absolute or relative file paths. The absolute file path is the full file path, which starts from the root. For example, /home/analyst/projects is an absolute file path. The relative file path is the file path that starts from a user's current directory.
您可以使用絕對或相對檔案路徑導航到特定的子目錄。絕對檔案路徑是完整的檔案路徑,從根目錄開始。例如,/home/analyst/projects 是一個絕對檔案路徑。相對檔案路徑是從使用者當前目錄開始的檔案路徑。
Note: Relative file paths can use a dot (.) to represent the current directory, or two dots (..) to represent the parent of the current directory. An example of a relative file path could be ../projects
注意:相對檔案路徑可以使用一個點(.)來表示當前目錄,或兩個點(..)來表示當前目錄的父目錄。相對檔案路徑的範例可以是 ../projects.
Key commands for navigating the file system
導航檔案系統的關鍵命令
The following Linux commands can be used to navigate the file system: pwd, ls, and cd
以下 Linux 命令可用於導航檔案系統:pwd、ls 和 cd.
pwd
The pwd command prints the working directory to the screen. Or in other words, it returns the directory that you’re currently in.
pwd 命令會將工作目錄顯示在螢幕上。換句話說,它會返回您當前所在的目錄。
The output gives you the absolute path to this directory. For example, if you’re in your home directory and your username is analyst, entering pwd returns /home/analyst.
輸出會給您該目錄的絕對路徑。例如,如果您在主目錄中,且您的用戶名是 analyst,輸入 pwd 會返回 /home/analyst。
Pro Tip: To learn what your username is, use the whoami command. The whoami command returns the username of the current user. For example, if your username is analyst, entering whoami returns analyst
專家提示:要了解您的使用者名稱,可以使用 whoami 指令。whoami 指令會返回當前使用者的使用者名稱。例如,如果您的使用者名稱是 analyst,輸入 whoami 會返回 analyst。.
ls
The ls command displays the names of the files and directories in the current working directory. For example, in the video, ls returned directories such as logs, and a file called updates.txt.
ls 指令會顯示當前工作目錄中的檔案和目錄名稱。例如,在影片中,ls 返回了像是 logs 這樣的目錄,以及一個名為 updates.txt 的檔案。
Note: If you want to return the contents of a directory that’s not your current working directory, you can add an argument after ls with the absolute or relative file path to the desired directory. For example, if you’re in the /home/analyst directory but want to list the contents of its projects subdirectory, you can enter ls /home/analyst/projects or just ls projects
注意:如果您想返回一個不是當前工作目錄的目錄內容,可以在 ls 後面添加一個參數,使用絕對或相對路徑指向所需的目錄。例如,如果您在 /home/analyst 目錄中,但想列出其 projects 子目錄的內容,可以輸入 ls /home/analyst/projects 或僅輸入 ls projects。.
cd
The cd command navigates between directories. When you need to change directories, you should use this command.
cd 指令用於在目錄之間切換。當你需要更換目錄時,應該使用這個指令。
To navigate to a subdirectory of the current directory, you can add an argument after cd with the subdirectory name. For example, if you’re in the /home/analyst directory and want to navigate to its projects subdirectory, you can enter cd projects
要導航到當前目錄的子目錄,可以在 cd 後面加上子目錄名稱作為參數。例如,如果您在 /home/analyst 目錄中,並想導航到其 projects 子目錄,可以輸入 cd projects。.
You can also navigate to any specific directory by entering the absolute file path. For example, if you’re in /home/analyst/projects, entering cd /home/analyst/logs changes your current directory to /home/analyst/logs
您也可以透過輸入絕對檔案路徑來導航到任何特定目錄。例如,如果您在 /home/analyst/projects,輸入 cd /home/analyst/logs 會將您的當前目錄更改為 /home/analyst/logs.
Pro Tip: You can use the relative file path and enter cd .. to go up one level in the file structure. For example, if the current directory is /home/analyst/projects, entering cd .. would change your working directory to /home/analyst.
專家提示:您可以使用相對檔案路徑並輸入 cd .. 來向上移動一級目錄結構。例如,如果當前目錄是 /home/analyst/projects,輸入 cd .. 會將您的工作目錄更改為 /home/analyst。
Common commands for reading file content
讀取文件內容的常用命令
The following Linux commands are useful for reading file content: cat, head, tail, and less
以下 Linux 指令對於閱讀文件內容非常有用:cat、head、tail 和 less.
cat
The cat command displays the content of a file. For example, entering cat updates.txt returns everything in the updates.txt file.
cat 指令顯示文件的內容。例如,輸入 cat updates.txt 會返回 updates.txt 文件中的所有內容。
head
The head command displays just the beginning of a file, by default 10 lines. The head command can be useful when you want to know the basic contents of a file but don’t need the full contents. Entering head updates.txt returns only the first 10 lines of the updates.txt file.
head 指令預設顯示檔案的開頭部分,通常為 10 行。當你想了解檔案的基本內容但不需要查看全部內容時,head 指令會很有用。輸入 head updates.txt 只會返回 updates.txt 檔案的前 10 行。
Pro Tip: If you want to change the number of lines returned by head, you can specify the number of lines by including -n. For example, if you only want to display the first five lines of the updates.txt file, enter head -n 5 updates.txt
專業提示:如果你想更改 head 返回的行數,可以使用 -n 來指定行數。例如,如果你只想顯示 updates.txt 檔案的前五行,輸入 head -n 5 updates.txt.
tail
The tail command does the opposite of head. This command can be used to display just the end of a file, by default 10 lines. Entering tail updates.txt returns only the last 10 lines of the updates.txt file.
tail 指令的功能與 head 相反。這個指令可以用來顯示檔案的結尾部分,預設為 10 行。輸入 tail updates.txt 只會返回 updates.txt 檔案的最後 10 行。
Pro Tip: You can use tail to read the most recent information in a log file.
專業提示:您可以使用 tail 來閱讀日誌檔案中的最新資訊。
less
The less command returns the content of a file one page at a time. For example, entering less updates.txt changes the terminal window to display the contents of updates.txt one page at a time. This allows you to easily move forward and backward through the content.
less 指令會一次返回一頁的文件內容。例如,輸入 less updates.txt 會將終端視窗更改為一次顯示 updates.txt 的內容一頁。這讓你可以輕鬆地在內容中前後移動。
Once you’ve accessed your content with the less command, you can use several keyboard controls to move through the file:
一旦你使用 less 指令訪問了內容,你可以使用幾個鍵盤控制來瀏覽文件:
Space bar: Move forward one page
空白鍵:向前移動一頁b: Move back one page
b:向後移動一頁Down arrow: Move forward one line
向下箭頭:向前移動一行Up arrow: Move back one line
上箭頭:向上一行q: Quit and return to the previous terminal window
q:退出並返回到先前的終端視窗
Key takeaways 關鍵要點
It’s important for security analysts to be able to navigate Linux and the file system of the FHS. Some key commands for navigating the file system include pwd, ls, and cd. Reading file content is also an important skill in the security profession. This can be done with commands such as cat, head, tail, and less.
對於安全分析師來說,能夠導航 Linux 和 FHS 的文件系統是很重要的。一些導航文件系統的關鍵命令包括 pwd、ls 和 cd。閱讀文件內容在安全專業中也是一項重要技能。這可以通過 cat、head、tail 和 less 等命令來完成。