Unix Basic Tutorial

Command Line Environment File Management

Now that we know how to navigate the file structures and find out what's in directories, let's make and modify some directories and files.

To make a directory, use the "mkdir" command. Let's start by making a directory called "test" in /root

/root # mkdir test

List the contents of our current directory to check that "test" was successfully created. Your results should look like this:

/root # ls dos		hello.c		test /root #

Next, let's put a file into our new directory. To do that, we can copy or move the hello.c file that is in /root. We'll try two ways.

Option one: we can use the "cp" command to copy hello.c into the test directory while naming it hello2, like shown below. Note that we have to use the relative address "test/" to ensure that hello2 is placed where we want it. If we did not specify this, it would be copied into the current directory.

/root # cp hello.c test/hello2

Option two: we can redirect the content of hello.c to a new file named hello3 using the "cat" and redirection commands. Redirecting the output from one command into another file is done with the ">" command. You can think of the greater than sign as a funnel to push contents from the cat command into the container/file on the other end. Try it using the code below:

/root # cat hello.c > test/hello3

Now, navigate to the test directory, and check that hello2 and hello3 have been created.

Question

True or False: the contents of hello2 and hello3 are identical

The correct answer is a.

You can use the "cat" or "more" commands on hello2 and hello3 to verify that they are exactly the same. We copied the file the first time, and then printed all its contents into a new filename the second time, so they should be identical.

Please make a selection.

There is an easy way to truly tell the difference between two files: diff. You can use diff followed by two file names to check the difference between them. The differences will be listed individually. In this case, the command would be:

/root # diff hello2 hello3 /root #

Because the command didn't return any output, the files are exactly the same.

In addition to copying, we can rename or "move" the contents of one file to another filename. Type "mv hello3 hello4" into the simulator from the test directory you created previously. Then, list test's contents again.

Question

How many files should now exist in the test directory?

The correct answer is b.

When you moved hello3 to hello4, it did not create a copy, it simply moved the file from one name to another in your directory. Thus, there should be two files: hello2 and hello4. This is the same thing that you might think of when renaming a file in another computing environment.

Please make a selection.

You should now be comfortable creating, moving and copying files. What about removing files?

To remove a file, we use the "rm" command. Use this command to remove hello4, and be sure to check that it has been completed.

/root/test # ls hello2		hello4 /root/test # rm hello4 /root/test # ls hello2 /root/test #

If you want to remove an entire directory, you can use the rmdir command. Navigate back to the parent directory and try it on the test directory by typing "rmdir test".

Question

Why do you think this didn't work?

The correct answer is b.

The contents had to be removed first. Unix will only allow you to remove empty directories using the rmdir command. It may seem like a hassle, but it does ensure that you really want to delete the contents of a directory, since you have to go through the effort of deleting all other materials first.

Please make a selection.

Removing directories is best done with rmdir for safety's sake. However, you can remove files and directories quickly by recursively removing a directory's contents with "rm -r directory_name". This will remove the directory itself and the files within it. Always be careful when removing like this as there is no recovering from deleting files you meant to keep. If you want to play it safer, you can make "rm" interactive so it will check with you each time to make sure you want to remove that file before proceeding. To make "rm" interactive, use the -i command.

/root # rm -ri test rm: descend into directory 'test'? /root #

You can respond to the query by either typing

  • y or yes to proceed,
  • n or no to skip that file.

Now, try removing hello2, and then remove the entire directory.

Command

Description

Usage

cp

Copy file1 from directory1 to directory2 optionally renaming the file in the process.

cp directory1/file1 directory2/file2

mv

Move (similar to cut) file1 from directory1 to directory2 optionally renaming the file in the process.

mv directory1/file1 directory2/file2

rm

Remove a file from a directory

rm directory1/file1

rmdir

Remove an empty directory

rmdir directory