Creating Directories and Files
Today we begin learning how to work with the file system. We will cover how to create directories and files.
The command to create directories
NAME
mkdir – make directories
SYNOPSIS
mkdir [OPTION] DIRECTORY…
Lets first change our working directory to the temporary directory /tmp:
$ cd /tmp
Lets now create directory called “level1″:
$ mkdir level1
Lets now check to see if our directory was created:
$ ls level1
Lets now create a directory “level2″ inside the directory “level1″ but from our current working directory of /tmp by specifying the relative path:
$ mkdir level1/level2 $ ls level1 level2
Lets now create a directory inside the directory “level1″ by specifying an absolute path. If you specify an absolute path it doesn’t matter what your current working directory is:
$ mkdir /tmp/level1/level2_2 $ ls level1 level2 level2_2
Declaring the absolute path is useful when you want to create a directory but do not want to change your current working directory to the directory where you want to create the directory.
If we want to create a directory structure that is a few directories deep and don’t want to create each sub-directory manualy like:
mkdir subdir1
cd subdir1
mkdir subdir2
cd subdir2
mkdir subdir3
We can use the mkdir -p option to accomplish this:
$ mkdir -p subdir1/subdir2/subdir3
If we want to create more then one directory at the same time we execute mkdir and list each directories absolute or relative path seperated by spaces:
$ mkdir directory1 directory2 /tmp/directory3
The first two “directory1″ and “directory2″ are relative paths and the third “/tmp/directory3″ is an absolute path. This demonstrates that you can use either in the same command.
Lets now see the result:
$ ls directory1 directory2 directory3 level1 subdir1
The command to create files
NAME
touch – change file timestamps
SYNOPSIS
touch [OPTION]… FILE…
To create a file you use the “touch” command followed by the name of the file you want to create. Lets create the file “file1″ :
$ touch file1
Note that unlike windows files do not need to have file extensions like .txt, .jpg, specified. You can create a file without any file extension. This is perfectly normal. Good practice is to place extensions on files making it easier to identify files and also for programs to identify file types automatically.
$ touch file1.txt
In Linux unlike windows you can have multiple “.” in your file names. Because there is no definite file extension defined by the name of the file. This is very useful for when you are changing files and want to specify original or backup versions. You can easily identify these files or search for them. Just like with mkdir command you can create more then one file at the same time by separating them by spaces.
$ touch file1.txt.org file1.txt.bak
$ ls directory1 directory3 file1.text file1.txt.org subdir1 directory2 file1 file1.txt.bak level1
You may be wondering how to tell the difference between directories and files if you can create a file without a file extension. The answer is that most new Linux terminals display directories and files with different colors.
Now that you know how to create directories and files the next step is to learn how to move, rename, and delete them.


