Creating and Changing Folders
On a Windows or Mac desktop, our present location determines what files and folders
we can access. I can “see” my present location visually with the help of the graphic
interface - I could be looking at my Desktop, or the contents of a folder, for example.
In a Linux command-line interface, we lack the same visual queues to tell us what our
location is. Instead, we use a command - pwd (print working directory) - to tell
us our present location. Try executing this command on Lonestar6:
$ pwd
/home1/03439/wallen
This home location on the Linux filesystem is unique for each user, and it is roughly analogous to C:\Users\username on Windows, or /Users/username on Mac.
To see what files and folders are available at this location, use the ls (list) command:
$ ls
I have no files or folders in my home directory yet, so I do not get a response.
We can create some folders using the mkdir (make directory) command. The words
‘folder’ and ‘directory’ are interchangeable:
$ mkdir folder1
$ mkdir folder2
$ mkdir folder3
$ ls
folder1 folder2 folder3
Now we have some folders to work with. To “open” a folder, navigate into that folder
using the cd (change directory) command. This process is analogous to double-clicking
a folder on Windows or Mac:
$ pwd
/home/03439/wallen/
$ cd folder1
$ pwd
/home1/03439/wallen/folder1
Now that we are inside folder1, make a few sub-folders:
$ mkdir subfolderA
$ mkdir subfolderB
$ mkdir subfolderC
$ ls
subfolderA subfolderB subfolderC
Use cd to Navigate into subfolderA, then use ls to list the contents. What do you expect to see?
$ cd subfolderA
$ pwd
/home/03439/wallen/folder1/subfolderA
$ ls
There is nothing there because we have not made anything yet. Next, we will navigate back to the home directory. So far we have seen how to navigate “down” into folders, but how do we navigate back “up” to the parent folder? There are different ways to do it. For example, we could specify the complete path of where we want to go:
$ pwd
/home1/03439/wallen/folder1/subfolderA
$ cd /home1/03439/wallen/folder1
$ pwd
/home1/03439/wallen/folder1/
Or, we could use a shortcut, .., which refers to the parent folder - one level higher
than the present location:
$ pwd
/home1/03439/wallen/folder1
$ cd ..
$ pwd
/home1/03439/wallen
We are back in our home directory. Finally, use the rmdir (remove directory) command to remove
folders. This will not work on folders that have any contents (more on this later):
$ mkdir junkfolder
$ ls
folder1 folder2 folder3 junkfolder
$ rmdir junkfolder
$ ls
folder1 folder2 folder3
A bonus command available on some Linux operating systems is called tree. The tree command
displays files and folders in a hierarchical view. Use another Linux shortcut, ., to indicate
that you want to list files and folders in your present location.
$ tree .
.
|-- folder1
| |-- subfolderA
| |-- subfolderB
| `-- subfolderC
|-- folder2
`-- folder3
Before we move on, let’s remove the directories we have made, using rm -r to remove our parent
folder folder1 and its subfolders. The -r command line option recursively removes subfolders
and files located “down” the parent directory. -r is required for non-empty folders.
$ rm -r folder1
$ ls
folder2 folder3
Which command should we use to remove folder2 and folder3?
$ rmdir folder2
$ rmdir folder3
$ ls
Why could we use rmdir on folder2 and folder3, but not on folder1?
Review of Topics Covered
Command |
Effect |
|---|---|
|
print working directory |
|
list files and directories |
|
list files in column format |
|
make a new directory |
|
navigate into a directory |
|
remove an empty directory |
|
remove a directory and its contents |
|
list files and directories hierarchically |
|
refers to the present location |
|
refers to the parent directory |