Unix shell 2: Creating and deleting

Creating, updating and deleting files and directories from the command line.

Aim: after working through this article you will be able to create, amend and delete directories and text files. We assume you already know how to start your terminal and navigate around your filesystem.

The commands we will learn are: mkdir, touch, cat and rm.

Lets create a directory for us to use in this tutorial with the mkdir command in your Desktop directory. We will need to give a directory name as the second argument. Let's call it "myTestDir". We can then check it has been created using ls.

                    $ pwd
                    Users/MyName/Desktop      <--- check you are in Desktop
                    $ mkdir myTestDir
                    $ ls
                    fileA.txt   fileB.txt   someDirectory   myTestDir
                    $
                    

Success! We can see that myTestDir has been created.

Move into myTestDir using cd. Lets create some text files using the touch command. We can then display their contents on the terminal using cat.

                    $ touch my_first_file.txt my_second_file.txt
                    $ ls
                    my_first_file.txt   my_second_file.txt
                    $ cat my_first_file.txt
                    $
                    

Notice that cat did not display anything. This is because our files are empty. Usefully, cat can also add text to files using command line redirects >> and >. Notice that > will overwrite any text you have already written and >> will append your new text to the end of what is already there. Once we have pressed enter on our command we can then enter out text onto the next line of the terminal. To save and revert back to a command prompt we need to type Ctrl - C.

                    $ cat >> my_first_file.txt
                    Here is my appended text :)     <--- typed by user
                    $
                    

Now when we look at the contents of that file we will see our added text.

                    $ cat my_first_file.txt
                    Here is my appended text :)
                    $
                    

We have learned how to create and update directories and files. Take a look at your Desktop without using the terminal. You will see your created directory and text files here! Now we want to clean up these artifacts using rm. Note that once something is deleted there is no way to get it back - make sure you are deleting the right thing!

Back on your terminal make sure you are in your myTestDir directory. We will now delete our two text files using rm with the name of the file to be deleted as the second argument. Check it has been deleted using ls.

                    $ rm my_first_file.txt
                    $ ls
                    my_second_file.txt
                    $
                    

Now do the same for my_second_file.txt. We want to also delete our test directory. To do this go to Desktop using cd ... It is not possible to use the same rm command for a directory as we did for the text files - we need to explicitly state that we wish to delete a directory and all of its contents. We can do this using rm -r where the -r is a tag that indicates we want to delete recursively.

                    $ rm -r myTestDir
                    $ ls
                    fileA.txt   fileB.txt   someDirectory
                    $
                    

Well done! You now know how to create, update and delete files and directories from the command line.