Linux return to previous directory

How to go to the previous working directory in terminal?

In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd command? For example, I’m in /opt/soft/bin and I cd into /etc/squid3 and I want to get back to the first directory.

7 Answers 7

Probably since the old shells didn’t display the name of the current directory in the prompt, it was helpful to see the name of the directory when you typed cd — . For example when you type cd /usr/local you know that you are in /usr/loal , but when you type cd — you don’t always remember from which directory you came from. So it saves you from typing cd -; pwd . But this is all speculation.

Or you can use aliasing: Set up alias as alias — -=’cd -‘ then use — (1 char) instead of cd — (4 char). Faster 😀

@AndréKuhlmann You can do cd — twice. Do cd /; cd /usr; cd -; cd — you should be in /usr . But maybe I miss understood your question.

The other answers are definitely complete in the direct answer sense. cd — and cd $OLDPWD are definitely the main choices for this. However, I often find that getting into a workflow with pushd and popd works better.

Long story short, if you are moving into a directory with the ultimate intent of coming back to where you started, use pushd / popd .

Extended example

The major difference is easily shown by an example.

At this point, you have a directory stack that is dir2, dir1 . Running pushd with no arguments will put you back in dir1 with the stack now as dir1, dir2 . popd would do the same, but would leave you with an empty directory stack. This is not much different than how you would have been with the cd — workflow.

However, now you can now change directories multiple times and get back to dir1 . For example,

$ cd dir1 $ pushd dir2 $ cd dir3 

If you run popd at this point, you will go back to dir1 .

Источник

6 Effective Ways to Go Back to a Previous Directory on Linux

Learn how to navigate directories effectively on Linux. Discover 6 effective ways to go back to a previous directory on Linux, including the cd command, bash shortcuts, history, aliases, C programs, and the MacOS terminal.

  • The cd Command
  • Bash Shortcuts
  • How To Go to previous directory In Linux Or Ubuntu Step
  • History
  • Aliases
  • C Program
  • MacOS Terminal
  • Other helpful code examples for going back to a previous directory on Linux
  • Conclusion
  • How do I go back to a previous path in Linux?
  • How do I go back to previous directory?
  • How do I go back a directory in Unix?
Читайте также:  Get stack trace linux

Navigating directories is an essential task in Linux, and there are several ways to go back to a previous directory. The cd command is the primary method to navigate directories in Linux, and several other commands and techniques can be used to achieve this. In this article, we will discuss the various ways to go back to a previous directory on Linux.

The cd Command

The cd command is used to change the current working directory in Linux, and it can be used to navigate directories. To go back to the previous directory, the command cd — can be used. This command takes you back to the previous directory you were at. Additionally, the command cd .. can be used to move to the parent directory, and cd ../.. can be used to move up two levels.

Relative paths are also important to navigate directories, and tab completion is a useful feature that can save time and typing effort. For example, if you want to go to the Downloads directory, you can type cd Dow and press tab , and the terminal will complete the path for you.

Bash Shortcuts

Bash shortcuts like pushd and popd can also be used to navigate directories. The pushd command saves the current directory in a stack, and the popd command takes you back to the previous directory in the stack. This technique is useful when you need to switch between directories frequently.

To use the pushd command, type pushd /path/to/directory in the terminal. This command saves the current directory in a stack and switches to the new directory. To go back to the previous directory, type popd . This command takes you back to the previous directory in the stack.

How To Go to previous directory In Linux Or Ubuntu Step

History

The history of the last 10 directories can be accessed and displayed using the command cd — . This command displays the history of the last 10 directories you have visited. The history feature is useful when you need to switch between directories frequently and cannot remember the exact path.

To use the history feature, type cd — in the terminal. This command displays the history of the last 10 directories you have visited. To go back to a previous directory, type the number next to the directory in the history list. For example, to go back to the directory you visited third from the last, type cd -3 .

Aliases

Adding an alias like — to the .bashrc file can make the cd — command work in Bash. Aliases are shortcuts that can save time and typing effort. However, they can be difficult to remember.

To add an alias to the .bashrc file, type nano ~/.bashrc in the terminal. This command opens the .bashrc file in the nano text editor. Add the line alias —=’cd -‘ to the file, and save and exit the editor. To activate the alias, type source ~/.bashrc in the terminal.

C Program

A C program can also be used to set the current directory. This technique is useful when you need to automate directory navigation tasks.

Here is an example C program that changes the current directory to ~/Documents :

Compile the program using the command gcc -o program program.c in the terminal. This command creates an executable file called program . To run the program, type ./program in the terminal.

MacOS Terminal

In the MacOS terminal, entering a subshell before using the cd command can affect the ability to go back to the previous directory. This issue can be resolved by using the exit command to exit the subshell and return to the previous directory.

Читайте также:  Changing user permission in linux

To enter a subshell in the MacOS terminal, type bash in the terminal. This command starts a new subshell. To exit the subshell and return to the previous directory, type exit .

Other helpful code examples for going back to a previous directory on Linux

In shell, return to previous directory terminal code example

In shell, how to go back to the last directory in linux code example

/ * File & Directory Commands *To navigate into the root directory, use */ "cd /" /* To navigate to your home directory, use */ "cd" /*or*/ "cd ~" /* To navigate up one directory level, use*/ "cd .." /* To navigate to the previous directory (or back), use */ "cd -"
cd - #go to the previous directory

Conclusion

Navigating directories is an essential task in Linux, and there are several ways to go back to a previous directory. The cd command is the primary method, and other commands and techniques like bash shortcuts, history, aliases, C programs, and the MacOS terminal can be used to achieve this. Remember to use best practices like relative paths and tab completion when navigating directories, and always be careful to avoid using absolute paths accidentally. Use the techniques described in this article to become a more efficient and effective Linux user.

Frequently Asked Questions — FAQs

What is the cd command, and how does it work on Linux?

The cd command is used to change the current working directory in Linux. To go back to the previous directory, the command «cd -» can be used. This command takes you back to the previous directory you were at. Additionally, the command «cd ..» can be used to move to the parent directory, and «cd ../..» can be used to move up two levels.

What are bash shortcuts, and how do they help with directory navigation on Linux?

Bash shortcuts like «pushd» and «popd» can also be used to navigate directories. The «pushd» command saves the current directory in a stack, and the «popd» command takes you back to the previous directory in the stack. This technique is useful when you need to switch between directories frequently.

What is the history feature, and how can it be used for directory navigation on Linux?

The history of the last 10 directories can be accessed and displayed using the command «cd —«. This command displays the history of the last 10 directories you have visited. The history feature is useful when you need to switch between directories frequently and cannot remember the exact path.

What are aliases, and how can they be used for directory navigation on Linux?

Adding an alias like «—» to the «.zshrc» file can make the «cd -» command work in zsh. Aliases are shortcuts that can save time and typing effort. However, they can be difficult to remember.

Can a C program be used for directory navigation on Linux?

Yes, a C program can also be used to set the current directory. This technique is useful when you need to automate directory navigation tasks.

How can the issue of subshell affect directory navigation in the MacOS terminal, and how can it be resolved?

In the MacOS terminal, entering a subshell before using the «cd» command can affect the ability to go back to the previous directory. This issue can be resolved by using the «exit» command to exit the subshell and return to the previous directory.

Источник

Читайте также:  Get java version in linux

How can I quickly return back to the last working directory? [duplicate]

All too often I find myself cycling between two directories to perform different tasks. The directories don’t always have an easy-to-use relative path between them. As a result it is quite cumbersome to have to specify a directory path that is essentially the previous working directory. Is there a command by which I can easily return back to the previous working directory?

2 Answers 2

Bash utilizes a shell variable $OLDPWD which (as the name suggests) stores the old value of the present working directory $PWD .

Also, the man page entry for Bourne shell built-ins has this to say about cd :

An argument of — is equivalent to $OLDPWD .

Which means that one could simply type

You can use cd — or cd $OLDPWD . But I suggest to use pushd and popd .These utilities keep what is called a directory stack.

pushd dir :You will change directory to dir and save dir in the stack. If you do another pushd dir4 , then dir4 will be added to the directory stack.

popd :It allows you to undo the directory changes. By giving a popd command, the last directory pushed in the stack (which is your current directory) will be removed, you will change directory to the second directory which becomes the top of the stack.

dirs -v : To see the contents of the directory stack.

If you know the stack concept then it’ll be very much easy to understand.

Linked

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

‘cd’ option to change to the previous directory

I remember there was a way to execute the cd command, automatically returning to the previous directory (without an explicit cd . ). Is it possible?

Just to be curious, why that vote as «not a real question»? :S Please leave a comment and I’ll try to clarify.

4 Answers 4

If you just want to go back to the last directory, you can use cd — .

If you need more places to go back to, try pushd (instead of cd ) and then you can go back with popd .

Accepting. Even if I’m starting a couple of sub-shells in my own script, this sounds as the best alternative.

Found! I can execute it as a sub-shell.

A command list embedded between parentheses runs as a subshell.

Do note that running it in a subshell means that things like setting environment variables to be used outside won’t work.

Strictly speaking you don’t «return» to the current directory since you never left it in the scope of the parent shell.

Also, pushd and popd can come in very handy — they represent operations on a stack of directory locations — allowing you to «travel back in time».

You can also try the cdargs package. There is a tutorial.

yum -y install compat-libstdc++-296 wget http://www.skamphausen.de/downloads/cdargs/cdargs-1.31-1.i386.rpm rpm -ivh cdargs-1.31-1.i386.rpm updatedb source `locate cdargs-bash.sh` # You can use any method you like for locating this file and running it # Also, you need to put it in the ~/.bash_profile file to run it every time you log in echo source `locate cdargs-bash.sh` >> ~/.bash_profile cdb 

Mark a directory and move around:

cd /etc/conf/httpd/ext/ mark apaconf cd /usr/local/src/ cdb apaconf 

See the link for other commands or search the Internet.

Источник

Оцените статью
Adblock
detector