File manipulations
In this section, you will learn how to create files and directories and move them around.
Creating directory
Linux organizes its file system hierarchically. Over time, you build up a fair amount of data, and storage capacities are constantly increasing. To help you organize that data in a manageable way, create a proper directory structure.
To create a directory, use mkdir, short for make directory.
mkdir [options] <Directory>
In its most basic form, you can run mkdir, supplying only a directory, and the command creates it for you.
Example 1:
user@bash: pwd
/home/ryan
user@bash:
user@bash: ls
bin Documents public_html
user@bash:
user@bash: mkdir linuxtutorialwork
user@bash:
user@bash: ls
bin Documents linuxtutorialwork public_html
user@bash:
Let's break down Example 1 above:
- Line 1 is to make sure you are where you think you should be. In Example 1 above, it is the home directory.
- Lines 2 does a quick listing, so you know what is already in your directory.
- In line 7, you run
mkdir, creating thelinuxtutorialworkdirectory where you can put further work related to the tutorial.
Remember that when you supply a directory in the above command, you are providing a path. Below, you can find a few more examples of how you can supply a directory to be created:
mkdir /home/ryan/foomkdir ./blahmkdir ../dir1mkdir \~/linuxtutorialwork/dir2
There are a few useful options available for mkdir:
-ptellsmkdirto create parent directories as needed, which is demonstrated in Example 2 below.-vmakesmkdirtell you what it is doing. As you saw in Example 1 above, it normally does not.
Example 2:
user@bash: mkdir -p linuxtutorialwork/foo/bar
user@bash: cd linuxtutorialwork/foo/bar
user@bash: pwd
/home/ryan/linuxtutorialwork/foo/bar
user@bash:
Below is the same command but with the -v option:
user@bash: mkdir -pv linuxtutorialwork/foo/bar
mkdir: created directory 'linuxtutorialwork/foo'
mkdir: created directory 'linuxtutorialwork/foo/bar'
user@bash: cd linuxtutorialwork/foo/bar
user@bash: pwd
/home/ryan/linuxtutorialwork/foo/bar
user@bash:
Removing directory
note
Whereas Linux GUI desktop environments typically provide an undo feature, the command line does not. So, be careful with what you are removing.
To remove a directory, use the rmdir command, short for remove directory.
rmdir [options] <Directory>
Also, pay attention to the following considerations:
- Similarly to
mkdir, thermdircommand supports the-vand-poptions. - A directory must be empty before you remove it.
user@bash: rmdir linuxtutorialwork/foo/bar
user@bash: ls linuxtutorialwork/foo
user@bash:
Creating blank file
Many commands that involve manipulating data within a file have the nice feature that they create a file automatically if you refer to
it and it does not exist. In fact, you can make use of this feature to create blank files with the touch command.
touch [options] <filename>
user@bash: pwd
/home/ryan/linuxtutorialwork
user@bash: ls
foo
user@bash:
user@bash: touch example1
user@bash:
user@bash: ls
example1 foo
The touch command is used to change the access and modification times on a file. This can be useful when you're testing a system relying on the file access or modification time parameters.
In this case, however, if you apply the touch command to a file and it does not exist, the command automatically creates it for you.
Many things in Linux are not done directly but by knowing the behavior of certain commands and aspects of the system and using them in creative ways to achieve the desired outcome.
Copying file or directory
There are many reasons why you may want to make a duplicate of a file or directory. Often before changing something, you may wish to create a
duplicate so that if something goes wrong, you can easily revert back to the original. The command you use for this is cp, which stands for copy.
cp [options] <source> <destination>
There are quite a few options available to cp. Some of them are listed below. Also, explore the manual page for cp to see what else is available.
user@bash: ls
example1 foo
user@bash:
user@bash: cp example1 barney
user@bash:
user@bash: ls
barney example1 foo
Note that both the source and destination are paths. This means you can refer to them using both absolute and relative paths. Here are a few examples:
cp /home/ryan/linuxtutorialwork/example2 example3cp example2 ../../backupscp example2 ../../backups/example4cp /home/ryan/linuxtutorialwork/example2 /otherdir/foo/example5
When we use cp, the destination can be a path to either a file or directory. If it is to a file, such as in examples 1, 3, and 4 above, the command creates a copy of the source but names the copy using the filename specified in the destination.
If you provide a directory as the destination, it copies the file into that directory, and the copy has the same name as the source. In its default behavior, cp only copies a file. There is a way to copy several files in one go. For more information, refer to Wildcards.
Using the -r option, which stands for recursive, you can copy directories. Recursive means that if you 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.
user@bash: ls
barney example1 foo
user@bash:
user@bash: cp foo foo2
cp: omitting directory 'foo'
user@bash: cp -r foo foo2
user@bash:
user@bash: ls
barney example1 foo foo2
In the above example, any files and directories within the foo directory are also copied to foo2.
Moving file or directory
To move a file, use the mv command, which is short for move. It operates in a way similar to cp. One slight advantage is that you can
move directories without having to provide the -r option.
mv [options] <source> <destination>
user@bash: ls
barney example1 foo foo2
user@bash: mkdir backups
user@bash: mv foo2 backups/foo3
user@bash: mv barney backups/
user@bash: ls
backups example1 foo
Let's break down the example above:
- In line 3, you create a new directory called
backups. - In line 4, you move the
foo2directory into thebackupsone and rename it asfoo3. - In line 7, you move the file barney into
backups. As you did not provide a destination name, it kept the same name.
Note that the source and destination are paths and can be referred to as either absolute or relative.
Renaming files and directories
Just as above with the touch command, we can use the basic behavior of the mv command in a creative way to achieve a slightly different
outcome.
Normally, mv is used to move a file or directory to a new directory. As you saw on line 4 above, you can provide a new name for the file or directory and, as part of the move, the command also renames it. Now if you specify the destination to be the same directory as the source but with a different name, you have effectively used mv to rename a file or directory.
user@bash: ls
backups example1 foo
user@bash: mv foo foo3
user@bash: ls
backups example1 foo3
user@bash: cd ..
user@bash: mkdir linuxtutorialwork/testdir
user@bash: mv linuxtutorialwork/testdir /home/ryan/linuxtutorialwork/fred
user@bash: ls linuxtutorialwork
backups example1 foo3 fred
Let's break down the example above:
- In line 3, you rename the
foofile to befoo3. Both paths are relative. - In line 6, you move to the parent directory. This is done only so that, in the next line, you can illustrate that you can run commands on files and directories even if you are not currently in the directory they reside in.
- In line 8, you rename the
testdirdirectory tofred. The source path is relative, and the destination is an absolute path.
Removing files
As with rmdir, removing a file is an action that cannot be undone, so be careful. The command to remove or delete a file is rm, which stands for remove.
rm [options] <file>
user@bash: ls
backups example1 foo3 fred
user@bash: rm example1
user@bash: ls
backups foo3 fred
Removing non-empty directories
Like several other commands introduced in this section, rm has several options that alter its behavior. Below, only the -r option is illustrated, allowing you to remove directories and all files and sub-directories contained within.
To explore the rest of the available options, use Manual pages.
user@bash: ls
backups foo3 fred
user@bash: rmdir backups
rmdir: failed to remove 'backups': Directory not empty
user@bash: rm backups
rm: cannot remove 'backups': Is a directory
user@bash: rm -r backups
user@bash: ls
foo3 fred
A good option to use in combination with -r is -i, which stands for interactive. The option prompts you before removing each file and directory and gives you the option to cancel the command.
One final note
By now, hopefully, you can appreciate that whenever you refer to a file or directory on the command line, it is a path. As such, you can specify it as either absolute or relative. This is pretty much always the case, so remember this important point.
Remember to experiment with both absolute and relative paths in the commands, as sometimes they give subtle but useful differences in output.
Activities for practicing
At your disposal, you have various commands to interact with the system. Let's put them into practice. Have a go at the following:
- Start by creating a directory inside your home directory for experimenting.
- In the directory, create a series of files and directories (and files and sub-directories in those).
- Rename a few of those files and directories.
- Delete one of the directories that has other files and directories in them.
- Move back to your home directory. From there, copy a file from a sub-directory into the initial directory you created.
- Move the file back into another directory.
- Rename a few files.
- Next, move a file and rename it into the process.
- Finally, have a look at the existing directories in your home directory. You probably have
documents,Downloads,Music,Imagesdirectories, and so on. Think about other directories to help keep your account organized and start setting this up.