如何在Linux中不使用CD命令进入目录/文件夹?

系统 Linux
众所周知,如果没有 cd 命令,我们无法 Linux 中切换目录。这个没错,但我们有一个名为 shopt 的 Linux 内置命令能帮助我们解决这个问题。

[[261191]]

众所周知,如果没有 cd 命令,我们无法 Linux 中切换目录。这个没错,但我们有一个名为 shopt 的 Linux 内置命令能帮助我们解决这个问题。

shopt 是一个 shell 内置命令,用于设置和取消设置各种 bash shell 选项,由于它已安装,因此我们不需要再次安装它。

是的,我们可以在启用此选项后,可以不使用 cd 命令切换目录。

我们将在本文中向你展示如何操作。这是一个小的调整,但对于那些从 Windows 迁移到 Linux 的新手来说非常有用。

这对 Linux 管理员没用,因为我们不会在没有 cd 命令的情况下切换到该目录,因为我们对此有经验。

如果你尝试在没有 cd 命令的情况下切换 Linux 的目录/文件夹,你将看到以下错误消息。这在 Linux 中很常见。

  1. $ Documents/
  2. bash: Documents/: Is a directory

为此,我们需要在用户 .bashrc 中追加以下值。

什么是 .bashrc ?

.bashrc 是一个 shell 脚本,每次用户以交互模式打开新 shell 时都会运行该脚本。

你可以在该文件中添加要在命令提示符下输入的任何命令。

.bashrc 文件本身包含终端会话的一系列配置。包括设置和启用:着色、补全,shell 历史,命令别名等。

  1. $ vi ~/.bashrc

加入这一行:

  1. shopt -s autocd

运行以下命令使更改生效。

  1. $ source ~/.bashrc

我们已完成所有配置。简单地对此进行测试以确认这是否有效。

  1. $ Documents/
  2. cd -- Documents/
  3.  
  4. $ daygeek/
  5. cd -- daygeek/
  6.  
  7. $ /home/daygeek/Documents/daygeek
  8. cd -- /home/daygeek/Documents/daygeek
  9.  
  10. $ pwd
  11. /home/daygeek/Documents/daygeek

 

是的,它正如预期的那样正常工作。

而且,它在 fish shell 中工作正常,而无需对 .bashrc 进行任何更改。

 

如果要暂时执行此操作,请使用以下命令(设置或取消设置)。重启系统时,它将消失。

  1. # shopt -s autocd
  2.  
  3. # shopt | grep autocd
  4. autocd on
  5.  
  6. # shopt -u autocd
  7.  
  8. # shopt | grep autocd
  9. autocd off

shopt 命令提供了许多其他选项,如果要验证这些选项,请运行以下命令。

  1. $ shopt
  2. autocd on
  3. assoc_expand_once off
  4. cdable_vars off
  5. cdspell on
  6. checkhash off
  7. checkjobs off
  8. checkwinsize on
  9. cmdhist on
  10. compat31 off
  11. compat32 off
  12. compat40 off
  13. compat41 off
  14. compat42 off
  15. compat43 off
  16. compat44 off
  17. complete_fullquote on
  18. direxpand off
  19. dirspell off
  20. dotglob off
  21. execfail off
  22. expand_aliases on
  23. extdebug off
  24. extglob off
  25. extquote on
  26. failglob off
  27. force_fignore on
  28. globasciiranges on
  29. globstar off
  30. gnu_errfmt off
  31. histappend on
  32. histreedit off
  33. histverify off
  34. hostcomplete on
  35. huponexit off
  36. inherit_errexit off
  37. interactive_comments on
  38. lastpipe off
  39. lithist off
  40. localvar_inherit off
  41. localvar_unset off
  42. login_shell off
  43. mailwarn off
  44. no_empty_cmd_completion off
  45. nocaseglob off
  46. nocasematch off
  47. nullglob off
  48. progcomp on
  49. progcomp_alias off
  50. promptvars on
  51. restricted_shell off
  52. shift_verbose off
  53. sourcepath on
  54. xpg_echo off

此外,我找到了一些其他程序,它们可以帮助我们在 Linux 中比 cd 命令更快地切换目录。

它们是 pushdpopdup shell 脚本和 bd 工具。我们将在接下来的文章中介绍这些主题。 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2017-05-03 15:30:38

LinuxMeld比较文件夹

2023-03-21 09:31:30

Linux命令行文件夹

2018-01-24 15:35:22

Linux隐藏文件管理器

2014-02-12 10:58:05

Linux文件夹文件权限

2019-04-26 09:50:21

Linux压缩文件文件夹

2020-06-15 18:40:15

Ubuntu 20.0文件夹颜色Ubuntu

2018-02-05 09:15:17

Linux命令文件数量

2017-03-09 14:42:21

LinuxsFTP上传下载

2015-08-05 09:40:26

2022-01-01 19:00:17

LinuxLinux命令文件夹

2022-07-21 11:31:28

UbuntuWindowsLinux

2009-07-31 16:34:44

linux cd命令linux cd命令详

2019-12-30 14:06:42

Windows 10文件夹Windows

2018-11-05 10:55:39

2009-07-31 17:02:14

linux cd命令CDPATHlinux cd命令详

2015-06-01 12:19:03

FedoraCentOSSamba

2019-04-18 10:35:55

Windows 10重命名文件夹

2016-12-20 09:47:17

Linux命令复制文件到多个目录

2013-08-23 09:19:57

Cryptkeeper加密

2012-05-11 10:26:09

Linuxcdalias
点赞
收藏

51CTO技术栈公众号