>如果您经常在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中文网其他相关文章!