>如果您經常在Linux上使用命令行,則可能希望採用更快的方法來導航目錄和查看其內容。
通常,這涉及運行CD以更改目錄,然後ls ls列出文件。在將這些命令組合到一個命令中似乎是一個整潔的想法,但在處理包含許多文件的目錄時可能會引起問題。
>在本指南中,我們將向您展示如何使用
>自動列出目錄目錄
>>為什麼默認情況下,當您使用CD更改目錄,FISH(或任何其他外殼)時,為什麼將CD和LS?組合在一起。每次分別運行LS都可以是>重複
。結合這些命令使您更容易看到目錄的內容,而無需額外的步驟。但是,有一個問題:列出具有數百萬個文件的目錄的內容可以使您的外殼變成hanghang,消耗過多的資源,並使您的終端不響應。在以下步驟中,我們將提供所有可能的方法來組合CD和LS命令。
>方法1:一次使用命令
cd /path/to/directory; and ls
績效問題:列出數百萬個文件可能會花費很長時間,並使用CPU和存儲器 :您的終端可能在嘗試列出文件時可能會凍結。 >不必要的輸出:有時,您無需在導航後立即看到目錄的內容。 ,我們需要避免這些問題來避免使用這些問題,我們需要組合CD和LS ls。
創建自定義魚類功能,超時命令允許您運行具有時間限制的命令。如果命令在指定的時間內未完成,則將終止。這是防止LS懸掛在具有太多文件的目錄中的完美。
>以下是如何創建使用超時的CD和LS的魚殼函數。
cd /path/to/directory; and ls
nano ~/.config/fish/config.fish
function cdls # Change to the specified directory builtin cd $argv[1] and begin # List directory contents with a timeout of 1 second echo "Changed to directory: $PWD" timeout 1s ls -l end end
echo“更改為目錄:$ pwd”
:打印當前目錄的清晰目錄路徑。function cdls cd $argv; and timeout 1s ls -l end
source ~/.config/fish/config.fish
cdls /path/to/directory
步驟2:保存並重新加載配置
>保存文件並重新加載魚殼配置以應用更改:cdls enlightenment/sources/e26/
Changed to directory: /home/ostechnix/enlightenment/sources/e26 total 56 drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:13 ecrire drwxrwxr-x 11 ostechnix ostechnix 4096 Jan 17 19:13 edi drwxrwxr-x 18 ostechnix ostechnix 4096 Jan 17 19:02 efl drwxrwxr-x 12 ostechnix ostechnix 4096 Jan 17 19:14 eflete drwxrwxr-x 11 ostechnix ostechnix 4096 Jan 17 19:10 enlightenment drwxrwxr-x 8 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-forecasts drwxrwxr-x 8 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-penguins drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-places drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:14 entice drwxrwxr-x 9 ostechnix ostechnix 4096 Jan 17 19:13 enventor drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:12 ephoto drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:13 evisum drwxrwxr-x 7 ostechnix ostechnix 4096 Jan 17 19:13 express drwxrwxr-x 6 ostechnix ostechnix 4096 Jan 17 19:13 rage
>
>function cd builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
>
自定義超時>您可以調整超時時間以適合您的需求以適合您的需求。例如:>使用0.5s進行較短的超時:0.5S ls -l >使用2S用於更長的超時時間:超時2S ls -ls -l只需在函數中修改函數中的超時值。 to the default cd command, redefine cd in your Fish shell configuration:Now, every time you use cd, it will automatically list the directory contents with a 1-second timeout.Method 3: Use an AbbreviationFish shell supports abbreviations, which expand into full commands when you type them.要為CD創建一個包含LS的縮寫,請運行:>此方法很有用,因為它可以保持原始CD命令,同時自動運行ls,以防止掛起。如果您經常與大型目錄一起使用,請考慮使用EXA,這是LS的現代替代方案。 EXA更快且功能更豐富,使其更適合使用許多文件來處理目錄。
這是修改函數以使用EXA的方法:
cd /path/to/directory; and ls
如果您不想再使用FISH功能CDL,請簡單地刪除在魚類配置文件中添加的線條。刪除這些行後,使用命令重新加載魚類構型:
nano ~/.config/fish/config.fish
如果您添加了魚殼中CD的縮寫,如方法3所示,則可以使用命令使用命令:
function cdls # Change to the specified directory builtin cd $argv[1] and begin # List directory contents with a timeout of 1 second echo "Changed to directory: $PWD" timeout 1s ls -l end end
>,您可能沒有遇到過,我將其保存在此fillioragration File中,以保存在此圖表中。儘管這起作用,但這並不是最好的方法。
添加更多功能會使配置文件混亂,從而使其更難管理。要保持清潔,請將每個魚功能存儲在其自己的文件中,以在單獨的目錄中。有關更多詳細信息,請閱讀以下指南:
> >>
在魚殼中組合CD和LS是簡化Linux中命令線導航的絕佳方法。通過使用超時命令,您可以執行自動目錄列表,而無需危險性能問題或無反應性的外殼。以上是如何更改目錄並在魚殼中的一個命令中列出文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!