-->
Previous Table of Contents Next


Creating Directories

To create a new directory, use the mkdir command. The syntax is mkdir<name>, where <name> is replaced by whatever you want the directory to be called. This creates a subdirectory with the specified name in your current directory:


darkstar:~$ ls

anotherfile    newfile   thirdfile

darkstar:~$ mkdir newdir

darkstar:~$ ls

anotherfile  /newdir    newfile    thirdfile


Tip:  
The mkdir command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you can abbreviate mkdir as md. You might think that md would work in Linux, because, after all, most of the commands we’ve seen have extremely concise names. However, Linux doesn’t recognize md; it insists on the full mkdir.

If you frequently switch between Linux and MS-DOS, you may want to use mkdir for both systems. However, be warned that you could start typing other Linux commands in MS-DOS—for example, typing ls instead of dir!


The mkdir command creates an entry for that subdirectory in Linux’s table of all files and directories, which is called the I-node table. When Linux creates a directory name, it doesn’t actually write anything to the disk drive because a directory name has no physical contents until you save a file to it. Directories are used by Linux as a convenience for the user.

You can use both relative and absolute pathnames with mkdir, for example:


$ pwd

/usr/tparker/temp

$ ls

$ mkdir book1

$ ls

book1

$ mkdir /usr/tparker/temp/book2

$ ls

book1

book2

In the first case we’ve used relative names to create a directory called book1 under the current directory. In the second example, we’ve used absolute addressing to create book2 in the same location. We can use whichever format is more convenient. Both methods accomplish the same result.

Moving and Copying Files

You often need to move or copy files. The mv command moves files (which is the same as renaming them) and the cp command copies files. The syntax for the two commands is similar:


mv <source> <destination>

cp <source> <destination>

As you can see, mv and cp are very simple commands. Here’s an example:


darkstar:~$ ls

anotherfile  /newdir         newfile    thirdfile

darkstar:~$ mv anotherfile movedfile

darkstar:~$ ls

movedfile  /newdir         newfile    thirdfile

darkstar:~$ cp thirdfile xyz

darkstar:~$ ls

anotherfile  /newdir    newfile    thirdfile    xyz

You can use cat (or more or less) at any time to verify that anotherfile became movedfile and that the contents of file xyz are identical to the contents of thirdfile.

It can get more confusing if you’re moving or copying files from one directory to another. This is because a file’s real name includes its absolute path—for instance, /home/fido/newfile. However, Linux lets you leave off parts of the file’s name because it’s more convenient to refer to newfile rather than /home/fido/newfile.

For example, suppose you want to move newfile from your current directory into the newdir subdirectory. If you want the file to keep the same name, you can type


darkstar:~$ mv newfile newdir/newfile

However, it’s much more common to type


darkstar:~$ mv newfile newdir

primarily because it’s a little shorter. In this case, because you have typed a directory name for the destination, Linux assumes that you want the file to be placed in the specified directory with the same name as the existing file.

You can also use cd to change to the directory you want to move the file to:


darkstar:~$ cd newdir

darkstar:~newdir$ copy ../newfile .

This example is a bit less intuitive than the first two. You specify that the source is ../newfile which means “the file newfile in the current directory’s parent directory.” The destination you simply specify as “.”, which is short for “the current directory.” In other words, you’re telling mv to “go up one level, grab newfile, and move it to right here.” Because this is less intuitive, you might find yourself automatically pushing a file from your current directory to another directory rather than pulling a file from another directory into your current directory.

You can also change the name of the file while moving or copying it to another directory by specifying the new name you want. The following is just one possible way:


darkstar:~$ cp newfile newdir/anothername

This creates a copy of newfile in the directory newdir and names the copied file anothername.


Warning:  
When moving or copying files between directories, you should always double-check that the file’s destination directory exists and verify the directory’s name. Otherwise, the results of your command can be unexpected, as the following two examples show.

If in the example just shown, let’s say you mistyped newdir as mv newfile mewdir—you wind up with a file called mewdir in your current directory and no file newfile in the newdir subdirectory!

Another way you get an unexpected result is to type cp newfile newdir if you didn’t realize that the directory newdir already existed. In this case, you are expecting to create an identical file called newdir in your current directory. What you are actually doing is creating a copy of newfile, called newfile, in the subdirectory newdir.


The mv command is much more efficient than the cp command. When you use mv, the file’s contents are not moved at all; rather, Linux makes a note that the file is to be found elsewhere within the file system’s structure of directories.


Previous Table of Contents Next