Linux bash get current directory

Earth Data Analytics Online Certificate

Intro to earth data science textbook
intro-to-earth-data-science-textbook Home

Lesson 2. Bash Commands to Manage Directories and Files

  • Run Bash commands to complete the following tasks:
    • print the current working directory ( pwd )
    • navigate between directories on your computer ( cd )
    • create new directories ( mkdir )
    • print a list of files and subdirectories within directories ( ls )
    • delete files ( rm ) and directories ( rm -r )
    • copy files ( cp ) and directories ( cp -r ) to another directory
    • easily create new files using a single command ( touch )

How to Run Bash Commands in the Terminal

In the previous section on Terminal Sessions, you learned that the terminal displays a prompt that shows you that Bash is waiting for input.

Recall that depending on your computer’s set-up, you may see a different character as a prompt and/or additional information before the prompt, such as your current location within your computer’s file structure (i.e. your current working directory).

When typing commands (either from this textbook or from other sources), do not type the dollar sign (or other character prompt). Only type the commands that follow it.

Note: In the examples on this page, the indented lines that follow a prompt and do not start with a dollar sign ($) are the output of the command. The results of the commands below on your computer will be slightly different, depending on your operating system and how you have customized your file system.

Useful Bash Commands to Manage Directories and Files

Your current working directory is the directory where your commands are being executed. It is typically printed as the full path to the directory (meaning that you can see the parent directory).

To print the name of the current working directory, use the command pwd .

As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory. The home directory is the default directory that you will be in each time you start a new Bash session.

Windows users: note that the Terminal uses forward slashes ( / ) to indicate directories within a path. This differs from the Windows File Explorer which uses backslashes ( \ ) to indicate directories within a path.

Change Current Working Directory ( cd )

Often, you may want to change the current working directory, so that you can access different subdirectories and files.

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

For example, you can change the working directory to an existing documents directory under your home directory, and then check that the current working directory has been updated.

You can go back to the parent directory of any current directory by using the command cd .. , as the full path of the current working directory is understood by Bash .

Читайте также:  Как снять образ с dvd windows 10

You can also go back to your home directory (e.g. /users/jpalomino ) at any time using the command cd

(the character known as the tilde).

Create a New Directory ( mkdir )

The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd .

Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

For example, you can create a new directory under documents called assignments . Then, you can navigate into the new directory called assignments , and print the current working directory to check the new path.

Notice that mkdir command has no output. Also, because assignments is provided to Bash as a relative path (i.e., doesn’t have a leading slash or additional path information), the new directory is created in the current working directory (e.g. documents ) by default.

Data Tip: Directory vs Folder: You can think of a directory as a folder. However, recall that the term directory considers the relationship between that folder and the folders within it and around it.

Data Tip: Notice that you are creating an easy to read directory name. The name has no spaces and uses all lower case to support machine reading down the road.

To see a list of all subdirectories and files within your current working directory, use the command ls .

In the example above, ls printed the contents of the home directory which contains the subdirectories called documents and downloads and the files called addresses.txt and grades.txt .

You can continue to change your current working directory to a subdirectory such as documents and print a new list of all files and subdirectories to see your newly created assignments directory.

You can also create a new subdirectory under assignments called homeworks , and then list the contents of the assignments directory to see the newly created homeworks .

Delete a File ( rm )

To delete a specific file, you can use the command rm followed by the name of the file you want to delete (e.g. rm filename ).

For example, you can delete the addresses.txt file under the home directory.

Delete a Directory ( rm -r )

To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ).

For example, you can delete the assignments directory under the documents directory because it does not meet the requirement of a good name for a directory (i.e. not descriptive enough — what kind of assignments?).

The rm stands for remove, while the -r is necessary to tell Bash that it needs to recurse (or repeat) the command through a list of all files and sub-directory within the parent directory.

Thus, the newly created homeworks directory under assignments will also be removed, when assignments is deleted.

Copy a File ( cp )

You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ).

For example, you can copy grades.txt from the home directory to documents .

Note that the original copy of the file remains in the original directory, so you would now have two copies of grades.txt , the original one in the home directory and the copy under documents .

Copy a Directory and Its Contents ( cp -r )

Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).

Similar to rm -r , -r in cp -r is necessary to tell Bash that it needs to recurse (or repeat) the command through a list of all files and sub-directory within the parent directory.

Читайте также:  Самый популярный проигрыватель для windows

Once again, the original copy of the directory remains in the original directory.

Create a New File Using a Single Command ( touch )

You can create a new empty file using the single command touch (e.g. touch file-name.txt ). This command was originally created to manage the timestamps of files. However, if a file does not already exist, then the command will make the file.

This is an incredibly useful way to quickly and programmatically create a new empty file that can be populated at a later time.

Project organization is integral to efficient research. In this challenge, you will use Bash to create an earth-analytics directory that you will use throughout this textbook.

You will then create a data directory within the earth-analytics directory to save all of the data that you will need to complete the homework assignments and follow along with the course.

Create a Directory for earth-analytics

Begin by creating an earth-analytics directory (or folder) in your home directory. Recall that this is the default directory in which the Terminal opens.

  • Create a new directory called earth-analytics .
  • Next, change your working directory to the earth-analytics directory, and create a new directory within it called data .
  • Last, go back to the home directory and confirm that you can then access the directories you just made.

Updated: September 03, 2020

The Intro to earth data science textbook course is subject to the CC BY-NC-ND 4.0 License . Citation DOI: https://doi.org/10.5281/zenodo.3382162

Share on

Twitter Facebook Google+ LinkedIn

Leave a Comment

You May Also Enjoy

Plot Data With Matplotlib

Calculate Seasonal Summary Values from Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python

Learn how to calculate seasonal summary values for MACA 2 climate data using xarray and region mask in open source Python.

Calculate Summary Values Using Spatial Areas of Interest (AOIs) including Shapefiles for Climate Data Variables Stored in NetCDF 4 Format: Work With MACA v2 Climate Data in Python

Climate datasets stored in netcdf 4 format often cover the entire globe or an entire country. Learn how to subset climate data spatially and by time slices u.

How to Open and Process NetCDF 4 Data Format in Open Source Python

Historic and projected climate data are most often stored in netcdf 4 format. Learn how to open and process MACA version 2 climate data for the Continental U.

Источник

Bash Get Current Directory

The Linux system responds by providing information against each input request. The achieved output is standard and printed to the shell prompt. In this tutorial, we will dig deep into the ways of accessing the current working directory and how users can switch from one directory or location to another, followed by relevant examples. The command used for accessing the current working directory will help them access any location in their system anytime, as per their requirements.

Requirements

Following system requirements are mandatory to run the commands in the bash to get directory:

Recommended OS: Linux Mint 20 or Ubuntu 20.04
User account: A user account with sudo rights

The tutorial assumes that users already have the latest Linux Mint OS on their computer systems. For bash, get the current directory in Linux Mint 20, open up the Terminal from the main menu on the bottom left of your screen, and then select the Terminal option.

To interact with the terminal, type bash and then press enter.

It will display a prompt, which shows that Bash is waiting for the value of the input.

Note: It all depends on the user’s computer system that they might get a different prompted character (The current location in the file structure of the computer system including the working directory that is currently running on the system). While entering the commands, don’t type $ or any other character before the command. Also, notice that in the examples mentioned in this tutorial, the lines that have a prompt in them, and don’t begin with $ character, are the outputs of each command.

Читайте также:  Где находятся гаджеты для windows 10

PWD (Print working directory)

The current working directory is the directory where all of the commands are being executed. You need to get the name of the current working directory printed. Type PWD command and then click enter. It will show the complete directory in the output, as shown below:

The above output shows that we are currently in the user’s directory, i.e., /home/aqsa. The command used here is PWD, a print working directory, and once typed, the Linux Mint 20 system is requested to display the current location. The default directory is the home directory that will appear when the users start a new Bash session.

Note: To exit the directory by one level, type cd .. and then click enter. You will be returned in one directory.

Whereas, if you want to exit all directories, simply type cd, then click enter. You will reach the default directory.

CD (Change current working directory)

Sometimes users want to switch from one directory to another to access the relevant locations and files in another directory. For this, they need to use the CD command, then followed by a location or a directory, e.g., Documents, Home, etc.

Simply type the CD directory name and then click enter. You can print your directory to check this new path. The working directory can be changed to the existing one, and the current working directory will be updated, as shown in the example below. Here, we have reached the home directory.

You can also move further in any directory by typing the CD Directory Name and then hit enter. This will further take you to the location, which is looking-for. Users can try entering the whole path as well in one go, e.g., cd /home/documents/test.docx; this will save them from trying multiple steps and will help them in reaching the location in one go.

Note: You can also see the list of all files present in the location in which you are currently present. It can be completed by simply typing ls, then, you can press enter to see the output.

Display or list all directories

Knowing the list of all directories is one important thing while working on Linux systems. The users can check out different options based on the directories they are currently working in and would want to switch between them, so they can make use of these locations.

To display all directories from a particular location, try the command as below:

Here, in the example below, the user is in its home directory, so it will display the relevant directory, which is named as “aqsa listed” and “currently in use”.

Note: You can also use a combination of ls and grep commands that will list down the directory names. For this, users can use the find command. Following are a few commands that can also be used in place of the command mentioned above:

Conclusion

In this tutorial, we explored different options to get the current directory using Bash in Linux Mint 20. In this way, users can access the current directory in Linux or Ubuntu based on the system they are using. The various command-line options are discussed to let users know how to get the current directory they are working in. The current working directory is the directory from which users invoke different kinds of commands from their terminal or console line. They can access different locations by simply typing these easy commands in one go and then perform relevant actions in the locations they tend to work in.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

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