The fun begins.
We've got some basic foundation stuff out of the way. Now we can start to play around. To begin with we'll learn to make some files and directories and move them around. Future sections will deal with putting content in them and more interesting manipulation.
Linux organises it's file system in a heirarchical way. Over time you'll tend to build up a fair amount of data (storage capacities are always increasing). It's important that we create a directory structure that will help us organise that data in a manageable way. I've seen way too many people just dump everything directly at the base of their home directory and waste a lot of their time trying to find what they are after amongst 100's (or even 1000's) of other files. Develop the habit of organising your stuff into an elegant file structure now and you will thank yourself for years to come.
Creating a directory is pretty easy. The command we are after is mkdir which is short for Make Directory.
mkdir [options] <Directory>
In it's most basic form we can run mkdir supplying only a directory and it will create it for us.
Let's break it down:
Remember that when we supply a directory in the above command we are actually supplying a path. Is the path we specified relative or absolute? Here are a few more examples of how we can supply a directory to be created
If these don't make sense then review section 2. Navigation
There are a few useful options available for mkdir. Can you remember where we may go to find out the command line options a particular command supports?
The first one is -p which tells mkdir to make parent directories as needed (demonstration of what that actually means below). The second one is -v which makes mkdir tell us what it is doing (as you saw in the example above, it normally does not).
And now the same command but with the -v option
Creating a directory is pretty easy. Removing or deleting a directory is easy too. One thing to note, however, is that there is no undo when it comes to the command line on Linux (Linux GUI desktop environments typically do provide an undo feature but the command line does not). Just be careful with what you do. The command to remove a directory is rmdir, short for remove directory.
rmdir [options] <Directory>
Two things to note. Firstly, rmdir supports the -v and -p options similar to mkdir. Secondly, a directory must be empty before it may be removed (later on we'll see a way to get around this).
A lot of commands that involve manipulating data within a file have the nice feature that they will create a file automatically if we refer to it and it does not exist. In fact we can make use of this very characteristic to create blank files using the command touch.
touch [options] <filename>
touch is actually a command we may use to modify the access and modification times on a file (normally not needed but sometimes when you're testing a system that relies on file access or modification times it can be useful). I know some students have tried using this command to make it look like their assignment files were not modified after the due date (there are means to detect this so it never works but points for being creative). What we are taking advantage of here is that if we touch a file and it does not exist, the command will do us a favor and automatically create it for us.
Many things in Linux are not done directly but by knowing the behaviour of certain commands and aspects of the system and using them in creative ways to achieve the desired outcome.
Remember in the introduction we talked about the command line as providing you with a series of building blocks. You are free to use these building blocks in any way you like but you can really only do this effectively if you understand how they do their function as well as why.
At the moment the file is blank which is kinda boring but in coming sections we'll look at putting data into files and extracting data from them.
There are many reasons why we may want to make a duplicate of a file or directory. Often before changing something, we may wish to create a duplicate so that if something goes wrong we can easily revert back to the original. The command we use for this is cp which stands for copy.
cp [options] <source> <destination>
There are quite a few options available to cp. I'll intoduce one of them further below but it's worth checking out the man page for cp to see what else is available.
Note that both the source and destination are paths. This means we may refer to them using both absolute and relative paths. Here are a few examples:
When we use cp the destination can be a path to either a file or directory. If it is to a file (such as examples 1, 3 and 4 above) then it will create a copy of the source but name the copy the filename specified in destination. If we provide a directory as the destination then it will copy the file into that directory and the copy will have the same name as the source.
In it's default behaviour cp will only copy a file (there is a way to copy several files in one go but we'll get to that in section 6. Wildcards). Using the -r option, which stands for recursive, we may copy directories. Recursive means that we want to look at a directory and all files and directories within it, and for subdirectories, go into them and do the same thing and keep doing this.
In the above example any files and directories within the directory foo will also be copied to foo2.
To move a file we use the command mv which is short for move. It operates in a similar way to cp. One slight advantage is that we can move directories without having to provide the -r option.
mv [options] <source> <destination>
Let's break it down:
Note that again the source and destination are paths and may be referred to as either absolute or relative paths.
Now just as above with the command touch, we can use the basic behaviour of the command mv in a creative way to achieve a slighly different outcome. Normally mv will be used to move a file or directory into a new directory. As we saw on line 4 above, we may provide a new name for the file or directory and as part of the move it will also rename it. Now if we specify the destination to be the same directory as the source, but with a different name, then we have effectively used mv to rename a file or directory.
Let's break it down:
As with rmdir, removing a file is an action that may not be undone so be careful. The command to remove or delete a file is rm which stands for remove.
rm [options] <file>
Like several other commands introduced in this section, rm has several options that alter it's behaviour. I'll leave it up to you to look at the man page to see what they are but I will introduce one particularly useful option which is -r. Similar to cp it stands for recursive. When rm is run with the -r option it allows us to remove directories and all files and directories contained within.
A good option to use in combination with r is i which stands for interactive. This option will prompt you before removing each file and directory and give you the option to cancel the command.
I know I've stressed the point but by now hopefully you can appreciate that whenever we refer to a file or directory on the command line it is in fact a path. As such it may be specified as either an absolute or relative path. This is pretty much always the case so remember this important point. In future sections I will not keep reminding you of this and the given examples will usually not illustrate this. Remember to experiment with both absolute and relative paths in the commands as sometimes they give subtle but useful differences in output. (The output of the command may be slightly different but the action of the command will always be the same.) (I will illustrate an example of this in the section on Wildcards)
We now have at our disposal, various commands to actually interact with the system. Let's put them into practice. Have a go at the following: