- Shell — Navigate up ‘n’ directories
- 9 Answers 9
- How to go up a Directory in Linux
- Go up a Directory in Linux Through cd Command
- Conclusion
- About the author
- Syeda Wardah Batool
- Linux Command Line Tutorial: Manipulate Terminal with CD Commands
- Why learn Command Line Interface?
- Launching the CLI on Ubuntu
- Present Working Directory
- Changing Directories
- Navigating to home directory
- Moving to root directory
- Navigating through multiple directories
- Moving up one directory level
- Relative and Absolute Paths
- 1. Absolute Path:
- 2. Relative Path:
- Summary:
Shell — Navigate up ‘n’ directories
Does this answer your question? climb up the directory tree faster — personally I like geirha’s answer the best.
@wjandrea I voted to close but then retracted my vote since the linked question specifies [bash] while Zsh and csh solutions can be found on this page.
9 Answers 9
No, there is no existing command for this, but it is trivial to write one. Add these lines to your ~/.bashrc file (thanks to D. Ben Knoble for pointing out that the variable should be local ):
Save the file, open a new terminal, and you can now move N directories up using ncd N . For example:
$ pwd /home/terdon/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9 $ ncd 5 Moving to ../../../../../ $ pwd /home/terdon/dir1/dir2/dir3/dir4 $ ncd 2 Moving to ../../ $ pwd /home/terdon/dir1/dir2
Coming from the Windows world, Alt + Up Arrow navigates to the parent directory in Windows Explorer. So I made something like this in ~/.inputrc :
then pressing Alt + Up Arrow moves to the parent directory in the terminal. You have to press multiple times of course to move higher, but I have found it to be very fast. Also you can change the shortcut to your liking.
This is wonderful. I had trouble getting the arrow keys to bind under Debian, so I used Alt-u instead: «\eu»: «cd ..\n» .inputrc can be immediately reloaded with bind -f ~/.inputrc
You can easily create aliases or shell functions to perform the given task. I personally use these:
alias ..='cd ..' alias . ='cd ../..' alias . ='cd ../../..'
and so on, manually defined for up to many-many dots in my .bashrc .
A shell function could instead easily take a numeric parameter, and execute cd .. in a loop that many times.
Similar to @terdon’s answer, with slightly more natural usage and output:
# go up up() < local count="$" local path=../ while (( --count > 0 )) ; do path="$path"../ done echo "cd -- $path" cd -- "$path" >
This defines a function up , which has the following properties:
- up n will go up n directories
- up will go up exactly one directory (i.e., the default for n is 1)
- output mimics that when using shopt -s autocd (hence the — , which are technically unnecessary)
- as before, OLDPWD is set correctly to the current value of PWD , rather than one of the intermediate directories. This means that up 5; cd — works as expected.
I guess «torek» is me? 🙂 Your addition of local makes a lot of sense, I should have thought of that myself! Then, you are defaulting to cd ../ when no input is passed, which is also good, but you don’t actually explain any of these benefits, so they aren’t obvious to the novice. The — is pointless since you know the argument will always be a variation of ../ . And you could simplify to while (( —count > 0 )) ; do to avoid the needless repetition of (( count— )) . But this is indeed a better version than mine, it’s a shame you don’t point out the benefits.
I admit that a function should declare all variables as local . My point in saying it’s more confusing is — maybe subjective: for is more readable than while with a predecrement. Also, what does $ <1:-1>do? If i did not oversee something, that’s the same as just $1 . I would also quote everything, like path=»$../» .1:-1>
How to go up a Directory in Linux
While working on the system, we always work on different folders to save files or other data. In Linux and Unix-Like systems, these folders are known as directories. Directories are the location where we save our data in the form of different files.
Sometimes, while working on a system, we need to switch our location and move to another directory. As all the work in the Linux system is done through commands. To move our location or change the current working directory, Linux has introduced the change directory command which is famous as the “cd” command in Linux. It is simple to use the cd command whether you want one level up directory, to navigate one level back, multiple directories navigation, or to navigate towards the root directory or home directory.
Go up a Directory in Linux Through cd Command
You need sudo privileges to run the cd command in the system.
Before getting started, keep in mind the syntax of cd command:
When you start a system, your current working location is the home directory.
Let’s try how we can perform different categories to change our working directory in Linux system using cd command:
One Level-up Directory
To level up one directory using cd command, the following command would be used:
Two level-up Directory
Similarly, to jump up two levels from the current directory or /usr directory, you can type:
To Return to Previous Level
If you want to return to the previous working directory, type in terminal:
Navigate to a Specific Folder
To navigate from home directory to specific folder, i-e if want to move towards download directory, the command would be:
Navigate to Home Directory
When you run only the “cd” command in the terminal, you will navigate to the home directory:
Or another way to get back to the home directory is the “cd ..” command:
Navigate to Root Directory
Execute the following command to change current directory to root directory:
Navigate To Directories Having Space in Name
By using the cd command, one can also change the directory having a name with space. The formula is quite simple. Use backlash (\) or double quotes around the name. For example:
Conclusion
We have seen various options like how to jump to the root directory, home directory, one level up directory, two level up directory, one level back directory and navigate towards a specific directory. This article has talked about how we can change our present directory.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.
Linux Command Line Tutorial: Manipulate Terminal with CD Commands
The most frequent tasks that you perform on your PC is creating, moving or deleting Files. Let’s look at various options for File Management.
To manage your files, you can either use
In this tutorial, you will learn-
Click here if the video is not accessible
Why learn Command Line Interface?
Even though the world is moving to GUI based systems, CLI has its specific uses and is widely used in scripting and server administration. Let’s look at it some compelling uses –
- Comparatively, Commands offer more options & are flexible. Piping and stdin/stdout are immensely powerful are not available in GUI
- Some configurations in GUI are up to 5 screens deep while in a CLI it’s just a single command
- Moving, renaming 1000’s of the file in GUI will be time-consuming (Using Control /Shift to select multiple files), while in CLI, using regular expressions so can do the same task with a single command.
- CLI load fast and do not consume RAM compared to GUI. In crunch scenarios this matters.
Both GUI and CLI have their specific uses. For example, in GUI, performance monitoring graphs give instant visual feedback on system health, while seeing hundreds of lines of logs in CLI is an eyesore.
You must learn to use both GUI(File Manager) and CLI (Terminal)
GUI of a Linux based OS is similar to any other OS. Hence, we will focus on CLI and learn some useful commands.
Launching the CLI on Ubuntu
There are 2 ways to launch the terminal.
1) Go to the Dash and type terminal
2) Or you can press CTRL + Alt + T to launch the Terminal
Once you launch the CLI (Terminal), you would find something as guru99@VirtualBox(see image) written on it.
1) The first part of this line is the name of the user (bob, tom, ubuntu, home…)
2) The second part is the computer name or the host name. The hostname helps identify a computer over the network. In a server environment, host-name becomes important.
3) The ‘:’ is a simple separator
4) The tilde ‘~’ sign shows that the user in working in the home directory. If you change the directory, this sign will vanish.
In the above illustration, we have moved from the /home directory to /bin using the ‘cd’ command. The ~ sign does not display while working in /bin directory. It appears while moving back to the home directory.
5) The ‘$’ sign suggests that you are working as a regular user in Linux. While working as a root user, ‘#’ is displayed.
Present Working Directory
The directory that you are currently browsing is called the Present working directory. You log on to the home directory when you boot your PC. If you want to determine the directory you are presently working on, use the command –
pwd command stands for print working directory
Above figure shows that /home/guru99 is the directory we are currently working on.
Changing Directories
If you want to change your current directory use the ‘cd‘ command.
Consider the following example.
Here, we moved from directory /tmp to /bin to /usr and then back to /tmp.
Navigating to home directory
If you want to navigate to the home directory, then type cd.
You can also use the cd ~ command.
Moving to root directory
The root of the file system in Linux is denoted by ‘/’. Similar to ‘c:\’ in Windows.
Note: In Windows, you use backward slash “\” while in UNIX/Linux, forward slash is used “/”
Type ‘cd /’ to move to the root directory.
TIP: Do not forget space between cd and /. Otherwise, you will get an error.
Navigating through multiple directories
You can navigate through multiple directories at the same time by specifying its complete path.
Example: If you want to move the /cpu directory under /dev, we do not need to break this operation in two parts.
Instead, we can type ‘/dev/cpu’ to reach the directory directly.
Moving up one directory level
For navigating up one directory level, try.
Here by using the ‘cd ..’ command, we have moved up one directory from ‘/dev/cpu’ to ‘/dev’.
Then by again using the same command, we have jumped from ‘/dev’ to ‘/’ root directory.
Relative and Absolute Paths
A path in computing is the address of a file or folder.
C:\documentsandsettings\user\downloads
There are two kinds of paths:
1. Absolute Path:
Let’s say you have to browse the images stored in the Pictures directory of the home folder ‘guru99’.
The absolute file path of Pictures directory /home/guru99/Pictures
To navigate to this directory, you can use the command.
This is called absolute path as you are specifying the full path to reach the file.
2. Relative Path:
The Relative path comes in handy when you have to browse another subdirectory within a given directory.
It saves you from the effort to type complete paths all the time.
Suppose you are currently in your Home directory. You want to navigate to the Downloads directory.
You do no need to type the absolute path
Instead, you can simply type ‘cd Downloads’ and you would navigate to the Downloads directory as you are already present within the ‘/home/guru99’ directory.
This way you do not have to specify the complete path to reach a specific location within the same directory in the file system.
Summary:
- To manage your files, you can use either the GUI(File manager) or the CLI(Terminal) in Linux. Both have its relative advantages. In the tutorial series, we will focus on the CLI aka the Terminal
- You can launch the terminal from the dashboard or use the shortcut key Cntrl + Alt + T
- The pwd command gives the present working directory.
- You can use the cd command to change directories
- Absolute path is complete address of a file or directory
- Relative path is relative location of a file of directory with respect to current directory
- Relative path help avoid typing complete paths all the time.