Stop Using the cd Command? When to Drop It
In this article
Yes, you can stop using cd for many everyday tasks, but you shouldn't abandon it entirely. Modern shells and file managers offer faster, safer ways to move around, yet cd remains essential for scripts and certain terminal workflows. This article explains when to replace cd, when to keep it, and how to make the transition without breaking your habits.
Why the Question Comes Up
The cd (change directory) command is one of the first things anyone learns in a Unix-like shell. It has been around since the early days of Unix and is present in every major shell: Bash, Zsh, Fish, PowerShell (as Set-Location or cd alias), and even Windows Command Prompt. For decades, it was the only way to move between directories in a terminal.
But as shells and tools evolved, many users found that typing cd repeatedly slows them down. Developers, system administrators, and power users have developed alternatives that reduce keystrokes, avoid errors, and integrate with modern file managers. The question is not whether cd is broken—it isn't—but whether it's the best tool for every situation.
How cd Works and Its Limitations
cd changes the current working directory of the shell process. When you run cd /var/log, the shell updates its internal state, and all subsequent commands run from that location. This seems simple, but it has limitations:
- Only affects the current shell: Running
cdin a script or subshell does not change the parent shell's directory. - Requires typing paths: Long paths are error-prone and slow to type, even with tab completion.
- No persistent history: You have to remember where you were or use
cd -to go back one directory only. - Breaks in pipelines:
cdis a shell builtin, so it doesn't work naturally in a pipe likefind | cd.
These limitations are why alternatives have emerged. They don't replace the concept of changing directories; they just make it faster and more convenient.
Alternatives to cd for Everyday Navigation
Several tools and shell features can replace cd in common scenarios. Here are the most practical ones:
- Using shell built-in shortcuts:
cd ~goes home,cd -toggles to the previous directory, andcd ..goes up one level. These are stillcd, but they reduce typing. - Zsh's
cdpathandautocd: In Zsh, you can setCDPATHto search common directories, and enableautocdto just type a directory name (e.g.,projects) to change into it, without typingcd. - Fish shell's
cdenhancements: Fish automatically suggests directories as you type, and you can press right arrow to accept a suggestion. - Fuzzy finders like
fzf: You can bindfzfto a key (e.g., Ctrl+T) to interactively search for a directory andcdinto it. This is especially useful for deep or rarely visited paths. - Directory jump tools: Tools like
z,autojump, orfasdlearn which directories you visit most and let you jump with a few letters. For example,z projmight take you to/home/user/work/projectsif that's your most frequent match. - File managers with terminal integration: Some terminal file managers (like
rangerorlf) allow you to navigate with arrow keys and then quit into the selected directory, effectively replacingcdfor visual navigation.
These alternatives shine when you're working interactively. They save keystrokes and reduce the mental load of remembering exact paths.
When You Should Still Use cd
Despite the alternatives, cd remains indispensable in several contexts:
- Scripts and automation: Shell scripts rely on
cdto set the working directory before running commands. Using a jump tool or a fuzzy finder in a script is impractical because they require interactive input. - Portability:
cdis POSIX-standard and works in every shell. If you write a script that others might run, usingcdensures it works everywhere. - Precise control: When you need to ensure you're in a specific directory,
cd /exact/pathis unambiguous. Alternatives likezare heuristic and might not go where you expect. - Non-interactive sessions: In CI/CD pipelines or cron jobs, you can't rely on interactive features.
cdis the only reliable way.
Also, many terminal workflows still start with cd to get to a project root, then use other tools for subdirectories. It's not an either/or situation.
How to Transition Without Losing Productivity
If you want to reduce your reliance on cd, here's a practical approach:
- Start with shell built-ins: Learn
cd -,cd ~, andcd ..thoroughly. They cover many common cases. - Enable
autocdin Zsh or Bash: In Bash, addshopt -s autocdto your.bashrc. In Zsh, setsetopt autocd. Then you can type a directory name to change into it. - Install a jump tool: Try
zorautojump. They require a small learning curve but pay off quickly. For example, after visiting/var/www/htmla few times,z htmlwill take you there. - Add a fuzzy finder: If you use
fzf, bind a key to search directories. A common binding isCtrl+Tto search files, but you can create a custom widget for directories. - Use a terminal file manager for complex navigation: If you're exploring an unfamiliar tree,
rangerorlfcan be more efficient than typing paths.
The key is to keep cd in your muscle memory for scripts and quick absolute paths, while using alternatives for frequent interactive jumps.
Common Problems and Misconceptions
One common misconception is that cd is slow or inefficient. In reality, cd is instantaneous; the slowness comes from typing long paths. Another misconception is that you must use cd to run a program in another directory. You can often run /path/to/program directly without changing directories, or use pushd and popd to temporarily change and then return.
A frequent problem is that cd in a script doesn't affect the parent shell. If you write a script that cds and then runs commands, the script's working directory changes, but your interactive shell stays put. To change your shell's directory from a script, you need to source the script (e.g., source myscript.sh) or use a function.
Another issue is that cd with relative paths can be confusing if you don't know your current directory. Using pwd to check is a good habit, but jump tools often eliminate the need to know exactly where you are.
Comparing cd with Modern Alternatives
Let's compare cd with a jump tool like z in a typical scenario. Suppose you frequently work in /home/user/work/projects/website/src/components. With cd, you'd type that full path (or use tab completion). With z, you might type z comp and it would take you there if that's your most frequent match. The jump tool wins on speed but loses on predictability—it might guess wrong if you have multiple directories with similar names.
Fuzzy finders like fzf offer a middle ground: you see a list of matches and pick one. This is more reliable than a heuristic jump but requires an extra keypress. For deep paths, fzf is often faster than typing.
File managers like ranger are best for visual exploration, but they add a layer of abstraction. If you're comfortable with the command line, you might find them slower for known paths.
Ultimately, the best tool depends on your workflow. If you visit the same few directories repeatedly, a jump tool is ideal. If you often navigate to new or unusual places, cd with tab completion is reliable. If you prefer a visual interface, a file manager is the way to go.
Frequently Asked Questions
Is it bad to use cd in scripts?
No, it's perfectly fine. In fact, it's the standard way to set a working directory in a script. Just be aware that cd in a script doesn't change the parent shell's directory. If you need to change the parent shell's directory from a script, you must source the script or use a function.
What is the fastest way to change directories in the terminal?
For frequent directories, a jump tool like z or autojump is fastest—you type a few letters and press Enter. For one-off paths, typing cd with tab completion is often the most reliable. If you have a visual preference, a terminal file manager can be faster for exploring.
Can I use cd without typing the full path?
Yes. You can use cd ~ for home, cd - for the previous directory, and cd .. to go up. You can also set the CDPATH environment variable to include common base directories, so cd projects would find /home/user/projects if that's in your CDPATH.
Does PowerShell have a different command?
PowerShell uses Set-Location, but cd is an alias for it. So you can still use cd in PowerShell. It also supports cd - to go back, and you can use Push-Location and Pop-Location for stack-based navigation.
Will using alternatives to cd break my shell scripts?
No, because alternatives like jump tools are interactive-only. They don't affect how scripts run. Scripts will still use cd as usual. The only thing to watch is that if you use a tool like z in an interactive shell, it might change your directory unexpectedly, but that's a matter of habit, not a technical break.
Is there a way to go back to a directory I visited earlier without cd -?
Yes. You can use pushd and popd to maintain a stack of directories. Or you can use a tool like z that remembers all your recent directories. Some shells also keep a history of visited directories; for example, Zsh has a dirs command that shows the directory stack.