The first in our student FAQ’s series; a number of students have asked for more information on navigating in the terminal. All students use the terminal for various tasks during the course, from installation scripts to docker commands. But what about the basics?

pwd – Print Working Directory

One of the most simple commands: pwd, prints the current directory’s path. This can be incredibly useful when you open a terminal to give you your starting point.

mkdir – Make Directory

Creating a folder or directory on the Desktop is a simple as right-clicking and selecting ‘create a new folder’. In the terminal, it is even easier! With the command: mkdir you can create a new directory. In the example below, we follow the mkdir command with the text newFolder to create a new directory on the Desktop called newFolder.

As you can see above there is no print out to show the directory has been created. So how can we check?

ls – List

The ls command lists all files and folders within the current directory. (We can of course check our current directory using our pwd command.) The list will appear in our terminal as demonstrated in the image below.

As you can see we only have one folder in the Desktop directory, which we created with our mkdir command. So how can we access our new directory?

cd – Change Directory

Moving from a parent directory into a child is usually as simple as ‘double-clicking’ on the folder. We can do this in the terminal by using cd and following it with the name of the child folder we want to access. In this case, we access the newFolder by using the command cd newFolder.

You can now see in the terminal that the folder we are in has changed from Desktop to newFolder. We can also use cd .. to move back into the parent folder of our current directory.

Tip: You can use the tab key to autocomplete names in the directory. For example we could have typed cd newF and then used the tab key to autocomplete. This can be a useful tool when folders have long names or have complicated syntax.

touch – Create a file

The simplest way to create a file in the terminal is by using the touch command. Similar to the mkdir command it needs to be followed by a file name and file type suffix, for example: touch index.html.

Again, no printout is shown in the terminal so to check our touch command has been successful, we can use our ls command.

We can now see that in our Desktop folder we have our newFolder directory and our index.html file. However, we have made a mistake! Our index.html file should be inside our newFolder. We can’t ‘drag and drop’ in the terminal so how would we move it?

mv – Move File/Folder

Using a mv command we can move files and folders to different locations. In this case, we use mv index.html newFolder. We follow the mv with the name of the file or folder we wish to move, then succeed that with the destination folder.

You can see now that index.html resides within our newFolder. All we need now is a few extra files in our newFolder. Can we speed it up?

&& – And

We can chain together multiple commands. For example, we could say change directory to newFolder and create a new file inside it called styles.css. cd newFolder && touch styles.css.

We can see we were able to create two new files in one command usig &&. We then used ls to check this.

Conclusion

We have covered some basic terminal commands to navigate around our folders, create new ones and move them around. Although this is sometimes easier to do in a graphical user interface (GUI) it is always good to be able to use the terminal, as our students know it is the best way to manage our containers in docker!