-->
Previous | Table of Contents | Next |
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 weve seen have extremely concise names. However, Linux doesnt 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-DOSfor example, typing ls instead of dir!
The mkdir command creates an entry for that subdirectory in Linuxs table of all files and directories, which is called the I-node table. When Linux creates a directory name, it doesnt 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 weve used relative names to create a directory called book1 under the current directory. In the second example, weve used absolute addressing to create book2 in the same location. We can use whichever format is more convenient. Both methods accomplish the same result.
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. Heres 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 youre moving or copying files from one directory to another. This is because a files real name includes its absolute pathfor instance, /home/fido/newfile. However, Linux lets you leave off parts of the files name because its 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, its much more common to type
darkstar:~$ mv newfile newdir
primarily because its 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 directorys parent directory. The destination you simply specify as ., which is short for the current directory. In other words, youre 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 files destination directory exists and verify the directorys name. Otherwise, the results of your command can be unexpected, as the following two examples show.If in the example just shown, lets say you mistyped newdir as mv newfile mewdiryou 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 didnt 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 files contents are not moved at all; rather, Linux makes a note that the file is to be found elsewhere within the file systems structure of directories.
Previous | Table of Contents | Next |