Categories
Interior

How to open the terminal to a specific directory in linux

Often while working with the files on your Mac, you may need to see the path of the current directory. There could be various reasons as to why you want to do so; maybe you wish to work with the current directory using Terminal and you need the full path, or maybe someone just wants to know the path of a particular file on your Mac so that he or she can access it.

By default, the Finder app on Mac does not show the full path in its title bar. All you see there is the name of the folder you are in. However, you do have a way to change the title bar from showing the folder name to showing the full directory path.

Showing the Current Path in Finder

1. Open a Finder window on your Mac. You can do that by opening any folder, and it will launch in the Finder app.

2. Once a Finder window launches, click on “View” on the top and then select “Show Path Bar.”

3. As soon as you click the option in the above step, you will see the current directory path in the bar at the bottom of the Finder window. It gives you the full path to where you currently are on your machine.

In my case it shows nested folders, as I am inside a folder which is inside another folder, and so on.

4. If that bar is not how you would like to see the paths in your Finder windows, you have another option to to try. Click on Launchpad in your dock, search for and click on “Terminal,” and it will launch for you.

5. Type the following command into Terminal and press Enter. It will make the title bar in your Finder windows show the full path of the current directory you are in.

6. Once the command has been executed, you will see that the title bar in your Finder window now shows the full path of the directory. It does not only show the folder name but also shows where actually you are on your machine.

7. Should you ever wish to revert back to the default and want Finder to only show the folder name, you can do that by running the following command in Terminal.

8. You should now be back to the default settings.

Conclusion

If you often require full paths to the directories on your Mac, you can use the above method to see full paths of every directory that you access.

Our latest tutorials delivered straight to your inbox

Lori Kaufman is a technology expert with 25 years of experience. She’s been a senior technical writer, worked as a programmer, and has even run her own multi-location business. Read more.

By default, new Terminal windows open to your Home directory. However, if there’s another directory you use often that you want immediate access to when you open the Terminal, there’s an easy way to set this up.

The .bashrc file in your Home directory contains commands that run when you open a Terminal window. So, we can add a cd command to change to a specific directory as soon as the Terminal window opens. To do this, press Ctrl+Alt+T to open a Terminal window. As we mentioned, you are in your Home directory by default, which is where you want to be right now.

We’re going to edit the .bashrc file, so type the following command to open it. You can use whichever text editor you want to use, but we’re going to use gedit in our example.

Scroll to the bottom of the .bashrc file and add the following command.

The tilde character (

) is a shortcut for your Home directory, which in our example, is /home/lori . So, the full path for the directory in the above command is /home/lori/HTGArticles .

/HTGArticles with the directory you want to open when you open a Terminal window.

The following line is a comment we added above the cd command, explaining what the command is doing. You don’t have to add a comment, but it helps make the .bashrc file easier to understand. The pound sign (#) at the beginning of a line indicates that line is a comment.

Once you’ve added the command to the file, click “Save” in the upper-right corner of the window to save the file.

Close the .bashrc file by clicking the “X” button in the upper-left corner of the window.

You must restart the Terminal window for this change to take effect. To close the Terminal window, either type exit at the prompt and press Enter, or click the “X” button in the upper-left corner of the window.

Now, press Ctrl+Alt+T to open the Terminal again. You are immediately in the directory you specified in the .bashrc file. You can still navigate to any other directory, but this is helpful if you work in this directory the most.

Account Information

Share with Your Friends

Linux 101: How to quickly open a terminal in a specific directory

Linux 101: How to quickly open a terminal in a specific directory

If you’re looking for as many shortcuts as you can find, Jack Wallen has one that’ll make working with the Linux terminal on a desktop a bit easier.

Unlike when working with a server, when using the Linux desktop, I spend more time with a GUI than I do the command line. That doesn’t mean I never touch the command line from within a desktop environment. In fact, I do so on a daily basis. I also try to use it as efficiently as possible.

When I know I have to dive deep into the file system hierarchy, I don’t always want to open a terminal window and then type something like:

It’s not a terribly challenging path to remember or type, but when you’re trying to work with a modicum of speed and efficiency, the less typing you have to do the better. What if you can’t always remember exactly where the directory is? For that, you might find it much easier to look from within a GUI file manager.

How do you combine these two into a much easier route to opening a terminal in a specific directory? Easy. Let me show you how.

How to open a terminal in a specific directory

  1. Open your file manager on the Linux desktop and navigate to the directory you need to work in.
  2. Once in that directory, right-click on an empty space in the file manager and then select Open In Terminal.
  3. A new terminal window should open, already in the current working directory of the file manager.

You can start working from the terminal, without having to first navigate to the folder you need to use. This isn’t a deal maker or breaker, but it certainly does make it much easier to get to those directories without having to type as much, or strain your memory to its limits.

And when you’re doing this all day, any help you can get can bring a bit of much-needed ease.

Open Source Weekly Newsletter

You don’t want to miss our tips, tutorials, and commentary on the Linux OS and open source applications.

Work with folders and files often in the Command or Terminal window in Windows, Mac or Linux? You’ll save time by opening it directly to a specific folder.

To go to a specific folder on the command line, generally, you must use the cd command and know the exact complete path to the folder you want. But there’s an easier way. You can open a PowerShell window, Command Prompt window, or a Terminal window to a specific folder from within the file manager. Today, we’ll show you how to do this in Windows, Mac, and Ubuntu Linux.

Windows 10

On Windows, you can press the Shift key and right-click on a folder to open a PowerShell window directly to that folder.

The article linked to above also shows you how to add the “Open command window here” option to the context menu when you’re in a folder.

A Command Prompt (or PowerShell) window opens directly to the selected folder.

macOS

To open a Terminal window to a specific folder from Finder, you must make sure the New Terminal at Folder and New Terminal Tab at Folder services are activated.

To activate these two services, select System Preferences from the Apple menu.

Click Keyboard on the System Preferences dialog box.

On the Keyboard dialog box, click the Shortcuts tab at the top and then select Services on the left.

Under Files and Folders on the right, check the New Terminal at Folder and New Terminal Tab at Folder boxes.

You can add shortcuts to these two services to make it faster to open a Terminal window to a folder. Click None to the right of the service you want to add a shortcut to.

Click Add Shortcut and then press the shortcut you want to use for the service. Make sure you select a shortcut not used by the system. We found it hard to find a unique shortcut because there are so many shortcuts already set up in macOS.

Also, you may have to click Add Shortcut again and then press Enter to get your shortcut to stick.

To open a Terminal window from within a folder in Finder, navigate to the folder you want. Do not go into the folder.

Select the folder, right-click on it, and go to Services > New Terminal at Folder to open a new Terminal window to the selected folder. If you want to open a new tab in an open Terminal window, go to Services > New Terminal Tab at Folder on the right-click menu. Opening a new tab on an existing Terminal window requires the window to be active, not minimized.

If you set up shortcuts for the two options, select the folder and press the shortcut for the option you want to use.

A new Terminal window opens directly to the selected folder if you selected the New Terminal at Folder option.

Selecting the New Terminal Tab at Folder option opens a new tab on the active Terminal window directly to the selected folder.

To remove the New Terminal options from the Services menu, go back to Apple menu > System Preferences > Keyboard > Shortcuts > Services and uncheck the two options on the right.

Linux

Here I’m using Ubuntu to open a Terminal window to a specific folder in Linux. The procedure may vary a bit on other Linux distros like Linux Mint, Zorin, and Fedora.

Open the Files app (which used to be called Nautilus), from the left sidebar. Go to the folder you want to open in a Terminal window, but don’t go into the folder.

Select the folder, right-click on it, and then select Open in Terminal.

A new Terminal window opens directly to the selected folder.

Save Time Navigating to Folders in a Command or Terminal Window

You can use the cd command to navigate to a folder on all three systems. Or, you can copy a full path to a folder and paste it into the Command or Terminal window. But if you do that, remember to insert backslashes in front of all the spaces in the path.

But the options we showed you here are easier and faster. And if you work with folders and files often in the Command or Terminal window, you’ll save time and be more productive.

Credit: Adobe Article

Qualcomm announces two major RFFE developments

Image: Funtap/Adobe Stock Article

Get one year of this leading VPN for just $30

Image: Michail Petrov/Adobe Stock Article

Managing the element of risk in projects

Credit: Adobe Article

How traditional security tools fail to protect companies against ransomware

Account Information

Join or sign in

Register for your free TechRepublic membership or if you are already a member, sign in using your preferred method below.

Join or sign in

We recently updated our Terms and Conditions for TechRepublic Premium. By clicking continue, you agree to these updated terms.

Welcome back!

Invalid email/username and password combination supplied.

Reset password

An email has been sent to you with instructions on how to reset your password.

Back to TechRepublic

Welcome to TechRepublic!

All fields are required. Username must be unique. Password must be a minimum of 6 characters and have any 3 of the 4 items: a number (0 through 9), a special character (such as !, $, #, %), an uppercase character (A through Z) or a lowercase (a through z) character (no spaces).

Linux 101: Open a terminal quickly in a specific directory

Linux 101: Open a terminal quickly in a specific directory

Length: 51:00 | July 1, 2021

Jack Wallen shows you a shortcut that will make working with the Linux terminal on a desktop easier.

Account Information

Share with Your Friends

Linux 101: Open a terminal quickly in a specific directory

Related

How to easily transfer files between Linux desktops with Warp

How to set a static IP address on Debian server

How to install Podman support in Cockpit on AlmaLinux 8

How to set a default gateway with the new Netplan method

How to use the Firefox tagging system

How to install the Simple Tab Groups add-on for Firefox

How to install the MongoDB GUI Compass and connect to a remote server

How to scan your websites for malware with ISPProtect

Most Recent

Top 5 alternatives to Google Authenticator

Tom Merritt shares the best alternatives to Google’s two-factor authentication app. Also check out the article at: https://www.techrepublic.com/article/top-alternatives-google-authenticator/

How to filter Asana by project

Jack Wallen guides you through filtering your Asana tasks by project or across multiple projects and apply additional filters. For the full step-by-step guide, head to: https://www.techrepublic.com/article/how-to-filter-asana-project/

How to add Portainer to Docker Desktop and connect it to a remote environment

Jack Wallen shows you how to extend the Docker Desktop feature set by adding Portainer into the mix. For the full step-by-step article, head to: https://www.techrepublic.com/article/add-portainer-docker-desktop-connect-remote/

How to easily transfer files between Linux desktops with Warp

Jack Wallen shows you what might be the easiest method of transferring files between Linux desktops on the market. For the full step-by-step article, head to: https://www.techrepublic.com/article/transfer-files-linux-desktops-warp/

How to install Docker Desktop and enable Kubernetes support

Jack Wallen shows you how to install Docker Desktop and extend it with Kubernetes and Portainer support. For the full step-by-step article, head to: https://www.techrepublic.com/article/how-to-install-docker-desktop-kubernetes-support/

Top 5 ways to bypass Internet shutdowns

If your access to the Internet is in danger, Tom Merritt has five tips to regain free connections. Be sure to also check out Tom’s article: https://www.techrepublic.com/article/top-ways-bypass-internet-shutdowns/

WWDC 2022 Keynote Recap: Apple’s newest exciting software, hardware and more

TechRepublic’s Mackenzie Burke and Brian Stone along with CIO Insight’s Kaiti Norton recap the latest software, hardware and more announced at Apple’s WWDC 2022 keynote.

Top 5 things to know about stablecoins

Tom Merritt shares the most important information you need to remember about stablecoin cryptocurrencies. Read the associated article at: https://www.techrepublic.com/article/top-know-about-stablecoins/

TechRepublic Premium

Industrial Internet of Things: Software comparison tool

IIoT software assists manufacturers and other industrial operations with configuring, managing and monitoring connected devices. A good IoT solution requires capabilities ranging from designing and delivering connected products to collecting and analyzing system data once in the field. Each IIoT use case has its own diverse set of requirements, but there are key capabilities and .

How to recruit and hire an Operations Research Analyst

Recruiting an Operations Research Analyst with the right combination of technical expertise and experience will require a comprehensive screening process. This Hiring Kit provides an adjustable framework your business can use to find, recruit and ultimately hire the right person for the job.This hiring kit from TechRepublic Premium includes a job description, sample interview questions .

Quick glossary: Industrial Internet of Things

The digital transformation required by implementing the industrial Internet of Things (IIoT) is a radical change from business as usual. This quick glossary of 30 terms and concepts relating to IIoT will help you get a handle on what IIoT is and what it can do for your business.. From the glossary’s introduction: While the .

Software Procurement Policy

Procuring software packages for an organization is a complicated process that involves more than just technological knowledge. There are financial and support aspects to consider, proof of concepts to evaluate and vendor negotiations to handle. Navigating through the details of an RFP alone can be challenging, so use TechRepublic Premium’s Software Procurement Policy to establish .

Published: April 14, 2022, 5:00 PM PDT Modified: April 16, 2022, 1:00 PM PDT Read More See more TechRepublic Premium

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 8 years ago .

How can I open a folder in a dir?

say I change my current directory to: cd /root/dir/

then I list all the files there: ls folder1 folder2 folder3

Now I want to open folder1. If I try the “./” I get: ./folder1 bash: ./folder1: Is a directory

How can I do so without having to type cd again ie: cd $(pdw)/folder1

5 Answers 5

If you want to open the folder via the nautilus file manager, you can move to the wanted directory like you’ve mentioned cd /root/dir/ , check the folders under that path using ls and then if you want to open folder1 type:

./ is used to execute file (Not to open directory).

(In)CLI Method: You can open folder in terminal by cd folder1 or dir folder1 or ls folder1 .

(To)GUI Method: If you want to open with file-manager (ex:nautilus) then type nautilus folder1 (for Ubuntu nautilus is default file-manager)

I have found that simply typing gnome-open “any-oject” opens any folder or file in the default program on Ubuntu. If this happens to be a folder, it uses your default folder-explorer 🙂

zsh shell can do that with the AUTO_CD option.

Just put setopt AUTO_CD in your .zshrc file (start zsh one time first to create the zsh environment files). You can invoke directly zsh at the terminal prompt to start a zsh session or you can change your default shell to be zsh with the chsh command.

Btw this is not a strange feature, crossable directories do have the “execute” attribute so it makes sense to able to execute a directory like any standard commands.

CC BY-SA Seth Kenlon

To navigate through the directories of your computer in a graphical interface, you’re probably used to opening a window to get “into” your computer, and then double-clicking on a folder, and then on a subfolder, and so on. You may also use arrow buttons or keys to back track.

The Linux Terminal

  • Top 7 terminal emulators for Linux
  • 10 command-line tools for data analysis in Linux
  • Download Now: SSH cheat sheet
  • Advanced Linux commands cheat sheet
  • Linux command line tutorials

To navigate through your computer in the terminal, you use the

cd command. You can use cd .. to move one directory back, or cd ./path/to/another/folder to jump through many folders into a specific location.

The concept of a URL, which you use on the Internet already, is actually pulled directly from POSIX. When you navigate to a specific page on some website, like http://www.example.com/tutorials/lesson2.html , you are actually changing directory to /var/www/imaginarysite/tutorials/ and opening a file called lesson2.html . Of course, you open it in a web browser, which interprets all that weird-looking HTML code into pretty text and pictures. But the idea is exactly the same.

If you think of your computer as the Internet (or the Internet as a computer, more appropriately), then you can understand how to wander through your folders and files. If you start out in your user folder (your home, or

for short) then everywhere you want to go is relative to that:

This requires some practise, but after a while it becomes far faster than opening and closing windows, clicking on back buttons and folder icons.

Auto-completion with Tab

The Tab key on your keyboard auto-completes names of directories and files you’re starting to type. If you’re going to cd into

/Documents , then all you need to type is cd

/Doc and then press Tab. Your shell auto-completes uments . This isn’t just a pleasant convenience, it’s also a way to prevent error. If you’re pressing Tab and nothing’s being auto-completed, then probably the file or directory you think is in a location isn’t actually there. Even experienced Linux users try to change directory to a place that doesn’t exist in their current location, so use pwd and ls often to confirm you are where you think you are, and that your current directory actually contains the files you think it contains.

Have you ever been in a situation where you wanted to SSH to a remote server and immediately cd into a directory? Yes? Well, you’re on the right track! This brief tutorial describes how to directly SSH into a particular directory on Linux. Meaning – we can automatically change to a particular directory when log in to a remote system via SSH. Not just SSH into a specific directory, it is even possible run any command immediately right after connecting to the remote server over SSH.

SSH Into A Particular Directory Of A Remote Linux System

Before I knew this method, I would usually first SSH into the remote remote system using command:

And then cd into a directory like below:

That’s how I change in to a directory in a remote system. Not anymore! No need to use two separate commands!!

For example, the following command allows me to SSH into a remote system (192.168.225.52), and automatically cd into a directory called “dir1”:

The above command will SSH into a remote system (192.168.225.52) and immediately cd into a directory named ‘/home/ostechnix/dir1’ directory and finally leave you at the remote system’s shell.

Here, the -t flag is used to force pseudo-terminal allocation, which is necessary for an interactive shell. If you don’t specify this flag, then no prompt will appear. And also if you don’t add ” bash” at the end of the above command, the connection will get terminated and return control to the local system.

Here is the sample output of the above command:

SSH Into A Particular Directory Of A Remote Linux System

Here are a few more example commands to SSH into a particular directory:

Here, the –login flag sets “bash” as login shell.

Here, the -l flag sets “bash” as login shell.

In the above examples, I have explicitly mentioned bash as login shell. Because, I know that Bash is is the default shell in my remote system. If you don’t know the shell type on the remote system, the use the following command:

Create a directory on remote system and automatically SSH into it

If you try to SSH into a directory that doesn’t exists in the remote system, you will see the following message upon successful authentication:

As you can see, the directory named “dir2” is not available in the remote system. If you want to create a directory in a remote system and automatically cd into it over SSH, simply run the following command from your local system:

Create a directory in a remote system and automatically cd into it over SSH

Making the changes permanent

If you don’t want to type the above commands manually every time, just add the command(s) you wanted to run after connecting to an SSH server on the remote system’s

Edit bashrc file:

Add the command(s) one by one. In my case, I am adding the following line:

Edit bashrc file

Please note that you should add this line on the remote system’s .bash_profile or .bashrc or .profile file, not in your local system’s. Press Ctrl+O and hit ENTER to save and press Ctrl+X to close the file.

Finally, run the following command to update the changes:

From now on, whenever you login (either via SSH or direct), the cd command will execute and you will automatically land inside the “/home/ostechnix/dir1” directory.

SSH Into A Particular Directory On Linux

Execute commands over SSH on remote Linux systems

Like I already said, this trick is not just to cd into directory after connecting to a remote system. You can also use this method to run other commands as well.

For example, the following command will land you inside ‘/home/ostechnix/dir1’ directory and then execute ‘uname -a’ command.

Execute commands over SSH on remote Linux systems

More examples on executing commands over SSH on remote systems are given in the following guide.

Introduction to Find Directory Linux

In Linux, everything is considered as a file that also includes directories. One thing a Linux user will do in common is searching for a directory or a file. There are different ways to search a file or directory in Linux. Commands such as Locate, find and which are used for searching a file or directory. However, the utility command is used only for locating the command. In this article, we are going to learn about Find Directory Linux and will see how to search for files and directories using various command-line options.

Using Locate Command

The locate command is much faster than find command since the locate command does not search through the real file system. It makes use of the database which is previously built. This command will return a list of path names that matches the search criteria.

Web development, programming languages, Software testing & others

The locate command usually searches all the files in the system starting from the root and displays the results that matches all or some part of the criteria.

Syntax:

# locate [option] [search pattern]

Let us assume that we are looking for a directory called kgf in the present working directory.

Use the command shown below.

$ locate –basename ‘\kgf’

The above command searches all the files or directory name which matches kgf. The following output is produced.

Output

From the output, it can be seen that the locate command will start searching from the root directory. Therefore files that contain the same name in different directories are matched and displayed on the screen.

To overcome this issue find command is used.

Find Command

The find command helps us to look up for files for which we know only the approximate names. In simple words, the find command searches for a file in the current working directory and recursively through the subdirectories that matches the given search criteria. You can search for files by name, extension, group, modification date, permissions, etc.

To display all files which are present in the current working directory use the following command.

The dot (.) symbol indicates the current working directory.

Output

Common Syntax of Find Command

$ find [options] [starting path] [expression]

  • The find command’s behavior and optimization methods are controlled by the options attribute.
  • The starting path attribute will specify the directory from where the find command should begin the search.
  • The expression attribute will control the test that searches the directory hierarchy.

Options Used in Find Command

-name option

It specifies the base of the file name that matches the shell pattern.

iname

This option ignores the case and performs a search which is case insensitive.

-print

This will print the full name of the file in the output screen.

-ls option

This option will display the current file in the ls -dils format in the output screen.

-type d

This option will list only folders and directories.

-type f

This will only search for files.

-not

This will search for files or folders which don’t match the test criteria.

maxdepth X

This will search the current directory and subdirectories upto X levels deep.

Optimization options [-01, -02, -03]

The find option uses optimization for enhancing performance. There are three optimization levels 01, -02, and -03

  • -01: This is the default optimization and the search is based on the file name.
  • -02: This one prioritizes the filename search and then runs the file type search.
  • -03: This level allows the find command to rearrange the search tests based on efficiency and the likelihood of success.

How to Find Directory in Linux?

Suppose you want to find a directory named apk in the root file system, use the following command.

$ find / -type d -name “apk”

$ sudo find / -type d -name “apk”

Output

If the output shows the permission denied message, add 2>/dev/null at the end of the command.

Example

$ find / -type d -name “apk” 2>/dev/null

How to Find Directory in Linux Named Projects in the Home Directory?

To search for Projects directory present in the $HOME directory, use the following command.

$ find $HOME -type d -name Projects

Output

How to get a Detailed List of Files and Directories?

Use the -ls option to list the present file in the ls output format.

Syntax:

$ find / -name “apk” –ls

Output

How to List only Directories?

If you want to search only the directories and skip the file names use the -type d option as shown below.

$ find / -type d -name “apk” -ls

How to Perform Case Insensitive Search?

By default, the search is case sensitive. To ignore case sensitive search use the -iname option as shown below.

$ find / -type d -iname “apk” -ls
$ find / -type d -iname “apk”

Finding Files by Modification Time

Find command can also be used to search a file based on its last modification time.

Example

$ find / -name “*png” -mtime -5

$ find /home/john/ -name “png” -mtime -4

The first command searches all the files in the files system and returns files that end with characters “png” which is modified in the last 5 days. The second command searches the home directory of the user named john and returns the files that end with characters “png” which is modified in the last 4 days.

Other Common Examples of Finding a file Using Find Command

$ find . -name samplefile.txt

The above command searches for a file named samplefile.text in the current directory and its sub-directories.

$ find /home -name *.png

This command will search for all files with the extension .png in the /home directory and directories beneath it.

$ find . -type f -empty

This will search for an empty file in the current directory.

Using which Command

This command will display the absolute path of the executable file or program. This is useful for creating a shortcut for the program in the desktop or in the desktop file manager.

Example

Output

Recommended Articles

This is a guide to Find Directory Linux. Here we discuss how to find a directory or a file in Linux by using the command-line options. You may also have a look at the following articles to learn more –

All in One Software Development Bundle (600+ Courses, 50+ projects)

Change Directory Linux

Change Directory (Navigate or Go to specific directory) in linux computer system can be performed via terminal using a simple command line interface. While directory or folder also can be created, modified, deleted, renamed, grown, and shrunk (truncated). In most cases, software programs that are executed on the computer handle these operations, but the user of a computer can also modify directories if necessary.

For instance, directories are normally can be changed by program in response to user commands, but the user can also change these directories directly by using a file manager program such as Windows Explorer (on Windows computers) or via shell or terminal emulator with command (CLI) on linux system.

How To Change Directory in Linux?

You can easily change (or go to specific) directory in linux via terminal on your system using the command line with the following methods cd, and Information.

Linux Command To Change Directory

“cd” command is used to changes your current working directory. In other words, it moves you to a new directory or folder in the file system. This is one of the most common administrative tasks you can perform when working on terminal with the linux command line.

To change (or go to specific) directory in linux:

  1. Open “Terminal” on your linux system.
  2. Use “cd” command with option as alternative and specify the “directory-name” or path where you want to go to.
  3. The syntax for using the “cd” command as shown below.
    • Example change a working directory in the current directory.

This will move or navigate you to “Documents” as new current working directory.

Example change a directory with relative path of directory name.

If the path starts with a slash “/” it is the absolute path to the directory.

Example change a “directory name” with space.

Surround the path with quotes or use the backslash (\).

Example navigate directory one level up.

Suppose you are currently in the “/user/Documents/local”, it’ll switch to the “/user/Documents” directory

  • Example navigate directory two level up.
  • Example navigate to previous directory.
  • Example navigate to home directory.
  • Example change a directory in simple relative path.

    Assuming “Documents” is inside your home directory.

  • Done, now you’ve already changed the directory.
  • “cd” command related information can be obtained with “–help”.

    cd: cd [-L|[-P [-e]] [[email protected]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR. The default DIR is the value of the HOME shell variable.

    The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars’ is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR.

    1. -L force symbolic links to be followed: resolve symbolic links in DIR after processing instances of `..’
    2. -P use the physical directory structure without following symbolic links: resolve symbolic links in DIR before processing instances of `..’
    3. -e if the -P option is supplied, and the current working directory cannot be determined successfully, exit with a non-zero status
    4. [email protected] on systems that support it, present a file with extended attributes as a directory containing the file attributes

    The default is to follow symbolic links, as if `-L’ were specified.

    `..’ is processed by removing the immediately previous pathname component back to a slash or the beginning of DIR.

    Exit Status: Returns 0 if the directory is changed, and if $PWD is set successfully when -P is used; non-zero otherwise.

    Table of contents

    Introduction

    This guide is intended to teach you the basics of navigating files and folders on a terminal. As you follow these instructions, keep in mind that your computer’s files and folders likely will be different from the samples. If you already have a lot of experience with the terminal, check out the commands on the homepage for quick reference material.

    Prerequisites

    In order follow this guide, you will need:

    • Access to a Unix terminal on any Linux or a macOS environment.
    • To know how to open a terminal window. If you are not sure, visit the instructions for macOS or Linux (coming soon).

    Let’s get started!

    Start by opening your terminal.

    When using terminal, you do work from inside of a specific folder on your computer. You can always access items from other folders on your computer, but terminal will keep track of the folder you are currently inside of. This is known as your working directory.

    As soon as you open terminal, you will be inside of a working directory. Type pwd in the terminal and press Enter. pwd stands for “print working directory”. The output from this command tells you which folder is your current working directory.

    In the sample screenshot below, our working directory is a folder called examples:

    Next, you can explore the contents of the working directory. Type ls into your terminal and press Enter. ls stands for “list”. This command lists all the files and folders in your working directory. In our sample, it shows all of the files and folders in examples:

    This is similar to opening your file browser application and examining at the contents of your working directory folder:

    You can list all the contents in folders that aren’t your working directory by typing ls [FOLDER_NAME] . This is like taking a peek inside another folder, without changing our working directory. In the example below, we are examining at the contents of folder1. We can observe that folder1 has only one file called file3.pdf:

    This is similar to examining at the contents of a folder within your working directory in your file browser application:

    The ls command has additional options that can change how it functions.

    Try typing ls -a and pressing Enter to display all files, including hidden files. On many computers, these files begin with a period and are hidden by default. Common examples of these files include things like “.DS_Store” (on macOS), “.profile” (on Linux), and “.gitignore” (when using Git for your projects).

    Note how we can now observe .hidden-file, which wasn’t being displayed with the previous ls command:

    Another option for listing files is typing ls -l and pressing Enter. This option generates a long listing. This means that it displays additional details for the files and folders. In addition to the names of files and folders, it will also show attributes, such as when they were last modified and their size:

    Similar to many terminal commands, you can combine these two options. To do this, type ls -la and press Enter, which will list all items in long format:

    Navigating folders

    You may have noticed that our working directory has multiple parts in the name. In general, the / (slash) character means that you are within a folder. So, when you observe /home/user/examples as your working directory, that means that:

    • You are inside of the examples folder, which is your working directory.
    • The examples folder is inside of the user folder.
    • The user folder is inside of the home folder.
    • The home folder is inside of your computer file system and there is nothing beyond it.

    Now we can change our working directory. In our example, we saw two folders when using ls to list everything (folder1 and folder2).

    Type cd [FOLDER_NAME] and press Enter. Your working directory is now changed. cd stands for “change directory”. You can type pwd again and press Enter to verify that you changed your working directory. In our sample, we changed our working directory to folder1:

    This is similar to clicking into a folder in your file browser application. You have completely switched the folder you are in:

    If you wanted to go “up” to our previous folder, you can type cd .. and press Enter. This will bring you up one folder level:

    This idea of going “down” into a folder or “up” out of a folder is very common when using terminal. When terminal commands, you use the names of folders when going “down” a level and .. to mean going “up” a level.

    Viewing file contents

    You are probably excited to start working with files with terminal. This can depend on the type of file, since viewing a picture file is different from reading a document file.

    For text files, a useful command when working in terminal is typing cat [FILE_NAME] and pressing Enter. The command cat is short for “concatenate” and it has multiple uses.

    One of the most common uses is to display the contents of a text file in the terminal. In the screenshot below, we are displaying the contents of hello.txt in our working directory:

    Conclusion

    Well done! You now know the essentials of navigating files and folders in your terminal. This includes changing your working directory, listing files and folders, and viewing the contents of a text file.

    In a future guide, you will learn how to create and modify files and folders.

    Open a Mac Terminal in your current folder location is easy if you have enabled the right Preferences option.

    Often while working with local files you may need to open a Terminal window in your current folder location. While doing that is as easy as pressing a button and clicking an option in Windows, things are different on Mac.

    You are required to first enable an option in your Preferences panel, and then you will have the option to launch an instance of Terminal in any folder of your choice – this was as true five years ago as it is on Big Sur today.

    Here’s how you can launch Terminal in the current folder location on your Mac.

    Launch Terminal Window in the Current Folder on Mac

    You do not need a third-party app to get the job done. All you need to do is visit the Preferences panel, tweak a few settings here and there, and you will be all set.

    1. Click on the Apple logo in the top-left corner on your Mac, and select “System Preferences…” You will be taken to the Preferences panel on your Mac.
    1. Click on “Keyboard” in the Preferences panel.
    1. Once in the Keyboard panel, click on the “Shortcuts” tab.
    1. In the Shortcuts panel, click on “Services” in the left-hand menu. Scroll down in the right-hand menu, and select the options that say “New Terminal at Folder” and “New Terminal Tab at Folder.” These options should be next to each other.

    Click on “none” next to “New Terminal at Folder,” and press a key combination on your keyboard to assign a new shortcut key to the feature. That way you will be able to launch a Terminal window using a shortcut key instead of pulling up the menu and selecting the option to launch one.

    1. Once you have enabled the options and assigned a keyboard shortcut, you can close the Preferences panel.
    1. Open the parent directory where your folder is located. Then single-click on the folder where you wish to launch a Terminal window, click on “Finder” followed by “Services,” and select “New Terminal at Folder.” Or you can simply press the keyboard shortcut that you assigned before.
    1. A new Terminal window should launch in the current folder location allowing you to play around with the local files in that folder.

    Launching a local instance of Terminal should now be easy for you. Should you ever wish to disable the feature, you can do so from the Preferences panel by just unchecking the boxes that you selected in the above steps.

    Frequently Asked Questions

    1. Can I drag-and-drop folders into the Terminal?

    Yes, if you keep the Terminal shortcut in your Dock, then you can open a specific folder in the Terminal by drag-and-dropping that folder from the Finder window onto the Terminal icon.

    Alternatively, you can drag-and-drop the folder from Finder into an open Terminal window.

    2. How do I navigate to a folder within the Terminal?

    If you want to use the Terminal to navigate to folders, simply type cd followed by the directory you want to navigate to. For example cd Desktop or cd Desktop/Downloads

    To see the current directory you’re in in the Terminal, type pwd , which stands for “print working directory”.

    If your work revolves around working with local files using Terminal, and you do not want to go through the hassle of providing full paths to the files, you can simply use the above workaround to have Terminal launched keeping your current folder as its current working directory.

    If you want to do the opposite, then see our guide on opening any folder from the Mac Terminal. We can also show you how to repair your macOS hard drive using fsck.

    Our latest tutorials delivered straight to your inbox

    How to extract Tar file to a specific directory in Linux:

    To extract the Tar file to another directory, follow the below-mentioned syntax:

    The “-x” flag tells the Tar utility to extract the file mentioned in the argument after “-f.” Whereas, “-C” flag is used to set a specific directory to extract the file. Alternatively, you can explicitly mention the total words to extract files using tar. In my opinion, using flags is a much quicker way to extract files than typing the entire word. Let’s do an example; I have a file by the name of “my_documents.tar,” which I want to extract to a directory “files/tar_files,” and to do that command would be:

    If you want to monitor the progress of extraction of the file in the terminal, then use the “-v” (verbose) flag:

    How to extract “tar.gz/tgz” files to a specific directory in Linux:

    As discussed above that tar files can be compressed by using the “gzip” utility. To extract such files to a specific directory, the procedure is pretty much similar; an additional flag “-z” will be added in the command to deal with “tar.gz” or “tgz” files:

    How to extract “tar.bz2/tar.bz/tbz/tbz2” files to a specific directory in Linux:

    Before we learn the extracting method, lets understand what “tar.bz2,tar.bz,tbz,tbz2” files are. These are the file extensions of the tar files compressed by either the “bzip” or “bzip2” utility in Linux. To extract files with any of these extensions, we will add the “-j” flag:

    For verbose output use:

    Conclusion:

    Tar is a widely used utility in Linux and UNIX-based operating systems to make backup archives. Tar utility also comes with a feature to extract tar files to a specific directory. Files can be extracted using the “-C” flag with the specified folder path. Moreover, using the Tar utility, you can also extract the specific files from the archived files. This all-in-one utility has a lot to explore and to learn more about Tar utility execute “man tar” in the terminal.

    About the author

    Sam U

    I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
    I hardly watch movies but love to read tech related books and articles.

    The tar utility is one of the utilities that you can use to create a backup on a Linux system. It includes many options that one can use to specify the task to achieve.

    Extract Linux Tar Files Different or New Directory

    One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory. You can read more about tar backup utility with many different examples in the following article, before proceeding further with this article.

    In this guide, we shall take a look at how to extract tar files to a specific or different directory, where you want the files to reside.

    The general syntax of tar utility for extracting files:

    Note: In the above first syntax, the -C option is used to specify a different directory other than the current working directory.

    Let us now look at some examples below.

    Example 1: Extracting tar Files to a Specific Directory

    In the first example, I will extract the files in articles.tar to a directory /tmp/my_article . Always make sure that the directory into which you want to extract tar file exists.

    Let me start by creating the /tmp/my_article directory using the command below:

    You can include the -p option to the above command so that the command does not complain.

    To extract the files in articles.tar to /tmp/my_article , I will run the command bellow:

    Img 01: Extract Tar Files to Different Directory

    In the above example I used the -v option to monitor the progress of the tar extraction.

    Let me also use the –directory option instead of -c for the example above. It works just in the same way.

    Img 02: Extract Tar Files to Specific Directory

    Example 2: Extract .tar.gz or .tgz Files to Different Directory

    First make sure that you create the specific directory that you want to extract into by using:

    Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory.

    Img 03: Extract tar.gz or .tgz Files to Different Directory

    Example 3: Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory

    Again repeating that you must create a separate directory before unpacking files:

    Now we will be unpacking the documents.tbz2 files to /tmp/tar.bz2/ directory.

    Img 04: Extract tar.bz2 Files to Different Directory

    Example 4: Extract Only Specific or Selected Files from Tar Archive

    The tar utility also allows you to define the files that you want to only extract from a .tar file. In the next example, I will extract specific files out of a tar file to a specific directory as follows:

    Img 05: Extract Specific Files From Tar Archive

    Summary

    That is it with extracting tar files to a specific directory and also extracting specific files from a tar file. If you find this guide helpful or have more information or additional ideas, you can give me a feedback by posting a comment.

    If You Appreciate What We Do Here On TecMint, You Should Consider:

    TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

    If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

    We are thankful for your never ending support.

    When installing Subsystem for Linux and Ubuntu from store on his development machine I can switch (or start) to Ubuntu shell by simply

    But the Ubuntu shell start in /home/techsupp folder by default. Is it possible to force it to start in same folder than the one I use my Ubuntu command?

    So in my example I should be in

    What I already tried:

    Please vote for it.

    11 Answers 11

    You can now choose to sort by Trending, which boosts votes that have happened recently, helping to surface more up-to-date answers.

    Trending is based off of the highest score sort and falls back to it if no posts are trending.

    I’m on Windows 10 Home with May Update and have Ubuntu 18.04 for WSL installed, I can open the console in any folder with Shift + Right Click and selecting the Open Linux shell here option

    If you check in Task Manager how the explorer “Open Linux shell here” option opens wsl, you can see that there’s a “–cd” option.

    run
    Run the provided command line in the current working directory. If no
    command line is provided, the default shell is launched.

    As far as I can tell the effect is the same as running ubuntu , except that it starts in the current directory.

    This command works even as a right click open here command in explorer. Adapting from this article:

    • RegEdit to HKEY_CLASSES_ROOT\Directory\Background\shell and create a new key
    • edit (Default) to “Open Ubuntu here” or similar.
    • add a key named command
    • edit (Default) to ubuntu run

    Now you have a functional “Open Ubuntu here” right click menu in explorer.

    The problem I have with wsl is that the wsl bash window doesn’t have the Ubuntu icon, even if it starts the Ubuntu distribution.