- How can I create a directory and change my working directory to the new directory?
- Lesson 2. Bash Commands to Manage Directories and Files
- Useful Bash Commands to Manage Directories and Files
- Print Current Working Directory ( pwd )
- Change Current Working Directory ( cd )
- Create a New Directory ( mkdir )
- Print a List of Files and Subdirectories ( ls )
- Delete a File ( rm )
- Delete a Directory ( rm -r )
- Copy a File ( cp )
- Copy a Directory and Its Contents ( cp -r )
- Create a New File Using a Single Command ( touch )
- Practice Your Bash Skills
- Create a Directory for earth-analytics
- Share on
- 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
- 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
- How to Open and Process NetCDF 4 Data Format in Open Source Python
How can I create a directory and change my working directory to the new directory?
If you really want it to be just one command, I suggest adding something like this to your .bashrc :
Entering md foo on the command line will then create a directory called foo and cd into it immediately afterwards. Please keep in mind, that you will have to reload your .bashrc for the changes to take effect (i.e. open a new console, or run source ~/.bashrc ).
mkdir «NewDirectory» && cd «NewDirectory»
- The part behind the && will only execute if the 1st command succeeds.
- It is called a Lists of Commands in the Bash manual.
- There is also a shorthand version:
$ false && echo "yes" $ true && echo "yes" yes
mkdir «NewDir» && cd «$_» works great than mkdir «NewDir» && cd «NewDir» as auto complete doesn’t work. BTW what is «$_» ?
More than that it would be quite handy if we can attach a switch to mkdir to change to new directory created.
@TheKojuEffect $_ see gnu.org/software/bash/manual/bashref.html#Lists Regarding that last one: nobody is stopping you from using an alias or a function inside .bashrc 😉
There’s no built-in function for that, but you can use shell functionality to help you not have to type the argument of the cd command again after running mkdir :
- Type cd , then Esc . (or Alt + . ) to insert the last argument from the previous command.
- cd !$ executes cd on the last argument of the previous command.
- Press Up to recall the previous command line, then edit it to change mkdir into cd .
You can define a simple make-and-change-directory function in your ~/.bashrc :
Reload your .bashrc ( . ~/.bashrc ) or restart bash, and now you can type mkcd new-directory .
This simple version fails in some unusual cases involving weird directory names or .. and symbolic links. Here’s one that does. For explanations, see the Unix & Linux version of this question.
mkcd () < case "$1" in /*) mkdir -p "$1" && cd "$1";; */../*) (cd "./$/.." && mkdir -p "./$") && cd "$1";; ../*) (cd .. && mkdir -p "$") && cd "$1";; *) mkdir -p "./$1" && cd "./$1";; esac >
Lesson 2. Bash Commands to Manage Directories and Files
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
Print Current Working Directory ( pwd )
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.
$ cd documents $ pwd /users/jpalomino/documents
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 .
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.
$ cd documents $ mkdir assignments $ cd assignments $ pwd /users/jpalomino/documents/assignments
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.
Print a List of Files and Subdirectories ( ls )
To see a list of all subdirectories and files within your current working directory, use the command ls .
$ cd ~ $ pwd /users/jpalomino $ ls addresses.txt documents downloads grades.txt
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.
$ cd documents $ ls assignments
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 .
$ cd assignments $ mkdir homeworks $ ls 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.
$ pwd /users/jpalomino $ ls addresses.txt documents downloads grades.txt $ rm addresses.txt $ ls documents downloads grades.txt
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?).
$ cd ~ $ cd documents $ pwd /users/jpalomino/documents $ ls assignments $ rm -r 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.
$ pwd /users/jpalomino/documents $ ls $
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 .
$ cd ~ $ pwd /users/jpalomino $ ls documents downloads grades.txt $ cp grades.txt documents $ cd documents $ ls grades.txt
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 .
$ cd ~ $ pwd /users/jpalomino $ ls documents downloads grades.txt
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.
$ cd ~ $ pwd /users/jpalomino $ ls documents downloads grades.txt $ mkdir pics $ ls documents downloads grades.txt pics $ cp -r pics documents $ cd documents $ ls grades.txt pics
Once again, the original copy of the directory remains in the original directory.
$ cd ~ $ pwd /users/jpalomino $ ls documents downloads grades.txt pics
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.
$ cd ~ $ cd downloads $ pwd /users/jpalomino/downloads $ touch veg-data.txt $ ls veg-data.txt
Practice Your Bash Skills
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.
- Next, change your working directory to the earth-analytics directory, and create a new directory within it called data .
$ cd earth-analytics $ mkdir data
- Last, go back to the home directory and confirm that you can then access the directories you just made.
$ cd ~ $ cd earth-analytics $ ls data
Updated: September 03, 2020
The Intro to earth data science textbook course is subject to the CC BY-SA 4.0 License . Citation DOI: https://doi.org/10.5281/zenodo.3382162
Share on
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.
- © 2022 Earth Lab — All materials on this site are subject to the CC BY-SA 4.0 License.
- Follow:
- GitHub
- Feed