While moving and renaming directories is done the same as with files, creating directories is different. In this video you'll learn how to do all three.
Creating Folders with mkdir
Creating a new directory [or folder] is done using the "mkdir" command [which stands for make directory.] I'll create a new directory named "OtherStuff".
The command I'll use is this: "mkdir OtherStuff"
When I type "ls", we'll see the new folder in our list. That's really all there is to it!
Renaming Folders with mv
The "mv" command works exactly the same with directories as it does with files. For example I'll rename the directory to "OtherThings".
The command I'll use is this: "mv OtherStuff/ OtherThings"
[Remember, the "/" is automatically added when I press the Tab key to autocomplete, and doesn't affect the command. It works the same with or without the "/"]
Now, we press Enter, and then when we use the "ls" command, we'll see that the file name has been changed.
Moving Folders with mv
And if I wanted to move the "OtherThings" directory down inside the "VeryImportant" directory we can do that too.
The command I'll use is this: "mv OtherThings/ VeryImportant/"
Now when we type "ls" the "OtherThings" directory is gone. However, if we type "ls VeryImportant/" [to list the files inside the "VeryImportant" directory], we'll see the "OtherThings" directory here.
In the previous section on Terminal Sessions, you learned that the terminal displays a prompt that shows you that
$ cd documents
$ pwd
/users/jpalomino/documents
0 is waiting for input.$ bash
$
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 [$ cd documents
$ pwd
/users/jpalomino/documents
1]
$ cd documents
$ pwd
/users/jpalomino/documents
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
$ cd documents
$ pwd
/users/jpalomino/documents
1.$ pwd
/users/jpalomino
As this is the first command that you have executed in
$ cd documents
$ pwd
/users/jpalomino/documents
0 in this session, the result of the $ cd documents
$ pwd
/users/jpalomino/documents
1 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 $ cd documents
$ pwd
/users/jpalomino/documents
0 session.Windows users: note that the
$ cd ..
$ pwd
/users/jpalomino
6 uses forward slashes [$ cd ..
$ pwd
/users/jpalomino
7] to indicate directories within a path. This differs from the Windows File Explorer which uses backslashes [$ cd ..
$ pwd
/users/jpalomino
8] to indicate directories within a path.Change Current Working Directory [$ cd documents
$ pwd
/users/jpalomino/documents
2]
$ cd documents
$ pwd
/users/jpalomino/documents
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 documents
$ pwd
/users/jpalomino/documents
2 followed by the name of the directory [e.g. $ cd ~
$ pwd
/users/jpalomino
1]. 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
$ cd ~
$ pwd
/users/jpalomino
2 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 ~
$ pwd
/users/jpalomino
3, as the full path of the current working directory is understood by $ cd documents
$ pwd
/users/jpalomino/documents
0.$ cd ..
$ pwd
/users/jpalomino
You can also go back to your home directory [e.g.
$ cd ~
$ pwd
/users/jpalomino
5] at any time using the command $ cd ~
$ pwd
/users/jpalomino
6 [the character known as the tilde].$ cd ~
$ pwd
/users/jpalomino
Create a New Directory [$ cd documents
$ pwd
/users/jpalomino/documents
3]
$ cd documents
$ pwd
/users/jpalomino/documents
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 documents
$ pwd
/users/jpalomino/documents
2.Then, use the command
$ cd documents
$ pwd
/users/jpalomino/documents
3 followed by the name you would like to give the new directory [e.g. $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
0].For example, you can create a new directory under
$ cd ~
$ pwd
/users/jpalomino
2 called $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2. Then, you can navigate into the new directory called $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2, and print the current working directory to check the new path.$ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
Notice that
$ cd documents
$ pwd
/users/jpalomino/documents
3 command has no output. Also, because $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 is provided to $ cd documents
$ pwd
/users/jpalomino/documents
0 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. $ cd ~
$ pwd
/users/jpalomino
2] 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 [$ cd documents
$ pwd
/users/jpalomino/documents
4]
$ cd documents
$ pwd
/users/jpalomino/documents
To see a list of all subdirectories and files within your current working directory, use the command
$ cd documents
$ pwd
/users/jpalomino/documents
4.$ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
In the example above,
$ cd documents
$ pwd
/users/jpalomino/documents
4 printed the contents of the home directory which contains the subdirectories called $ cd ~
$ pwd
/users/jpalomino
2 and $ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
2 and the files called $ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
3 and $ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
4.You can continue to change your current working directory to a subdirectory such as
$ cd ~
$ pwd
/users/jpalomino
2 and print a new list of all files and subdirectories to see your newly created $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 directory.$ cd documents
$ ls
assignments
You can also create a new subdirectory under
$ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 called $ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
8, and then list the contents of the $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 directory to see the newly created $ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
8.$ cd assignments
$ mkdir homeworks
$ ls
homeworks
Delete a File [$ cd documents
$ pwd
/users/jpalomino/documents
5]
$ cd documents
$ pwd
/users/jpalomino/documents
To delete a specific file, you can use the command
$ cd documents
$ pwd
/users/jpalomino/documents
5 followed by the name of the file you want to delete [e.g. $ cd documents
$ ls
assignments
3].For example, you can delete the
$ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
3 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 [$ cd documents
$ pwd
/users/jpalomino/documents
6]
$ cd documents
$ pwd
/users/jpalomino/documents
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
$ cd documents
$ pwd
/users/jpalomino/documents
6 followed by the name of the directory you want to delete [e.g. $ cd documents
$ ls
assignments
7].For example, you can delete the
$ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 directory under the $ cd ~
$ pwd
/users/jpalomino
2 directory because it does not meet the requirement of a good name for a directory [i.e. not descriptive enough - what kind of assignments?].$ pwd
/users/jpalomino
0The
$ cd documents
$ pwd
/users/jpalomino/documents
5 stands for remove, while the $ cd assignments
$ mkdir homeworks
$ ls
homeworks
1 is necessary to tell $ cd documents
$ pwd
/users/jpalomino/documents
0 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
$ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
8 directory under $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 will also be removed, when $ cd documents
$ mkdir assignments
$ cd assignments
$ pwd
/users/jpalomino/documents/assignments
2 is deleted.$ pwd
/users/jpalomino
1Copy a File [$ cd documents
$ pwd
/users/jpalomino/documents
7]
$ cd documents
$ pwd
/users/jpalomino/documents
You can also copy a specific file to a new directory using the command
$ cd documents
$ pwd
/users/jpalomino/documents
7 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. $ cd assignments
$ mkdir homeworks
$ ls
homeworks
8].For example, you can copy
$ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
4 from the home directory to $ cd ~
$ pwd
/users/jpalomino
2.$ pwd
/users/jpalomino
2Note that the original copy of the file remains in the original directory, so you would now have two copies of
$ cd ~
$ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
4, the original one in the home directory and the copy under $ cd ~
$ pwd
/users/jpalomino
2.$ pwd
/users/jpalomino
3Copy a Directory and Its Contents [$ cd documents
$ pwd
/users/jpalomino/documents
8]
$ cd documents
$ pwd
/users/jpalomino/documents
Similarly, you can copy an entire directory to another directory using
$ cd documents
$ pwd
/users/jpalomino/documents
8 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. $ pwd
/users/jpalomino
$ ls
addresses.txt documents downloads grades.txt
$ rm addresses.txt
$ ls
documents downloads grades.txt
5].Similar to
$ cd documents
$ pwd
/users/jpalomino/documents
6, $ cd assignments
$ mkdir homeworks
$ ls
homeworks
1 in $ cd documents
$ pwd
/users/jpalomino/documents
8 is necessary to tell $ cd documents
$ pwd
/users/jpalomino/documents
0 that it needs to recurse [or repeat] the command through a list of all files and sub-directory within the parent directory.$ pwd
/users/jpalomino
4Once again, the original copy of the directory remains in the original directory.
$ pwd
/users/jpalomino
5Create a New File Using a Single Command [$ cd documents
$ pwd
/users/jpalomino/documents
9]
$ cd documents
$ pwd
/users/jpalomino/documents
You can create a new empty file using the single command
$ cd documents
$ pwd
/users/jpalomino/documents
9 [e.g. $ pwd
/users/jpalomino
02]. 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.
$ pwd
/users/jpalomino
6Practice Your Bash Skills
Project organization is integral to efficient research. In this challenge, you will use
$ cd documents
$ pwd
/users/jpalomino/documents
0 to create an $ pwd
/users/jpalomino
04 directory that you will use throughout this textbook.You will then create a
$ pwd
/users/jpalomino
05 directory within the $ pwd
/users/jpalomino
04 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
$ pwd
/users/jpalomino
04 directory [or folder] in your home directory. Recall that this is the default directory in which the Terminal opens.