The goal of this lab is to become proficient with system commands for copying, moving, renaming, creating and removing files within your home directory.
Log on to the
opus server so that you have a command line shell at your service. Be sure you are in your home directory to start this lab. We are going to reorganize the files in our home directory. This will involve making new subdirectories and moving files around. The questions asked during this procedure are for your clarification only. You will be graded on correctly performing the procedure. At the end of the lab you will submit your work using the submit command.
Part I - Making Directories¶
Display a listing of the files in your home directory using the
Now let’s make some new directories using the
mkdircommand. Make a new directory named
editsfor keeping our file edits using the following command:
View the new directory’s contents using the
-aoption of the
lscommand. Do you see the two hidden files that were created with the directory?
You can make more than one new directory at a time by supplying two arguments to the
mkdircommand. Make two new directories, one called
docsthe other called
Verify that they were made in your home directory.
Now try to make a subdirectory in a directory that doesn’t yet exist by entering the following command:
What happens? Try the same command using the
mkdir -p class/labs
Verify that it worked this time.
Now that the
classdirectory is made, you can make a second subdirectory called
tests. Use the command:
Notice how you did not need the
-poption. Why not?
Part II - Renaming Files¶
Now that you have new subdirectories made, let’s rename some of the existing directories. To do this, we use the
Let’s rename the
Miscellaneousdirectory to just plain old
mv Miscellaneous misc
Notice how the syntax is:
mv oldname newname
Does it work?
Use the same command to change the name of
poemswith a lowercase
You can also rename a file using the same command. Unix doesn’t distinguish between renaming directories and renaming ordinary files. Try renaming
MarkTwain. Did it work?
Part III - Moving Files¶
Now we will actually move some files from one directory to another, renaming some of them as we do it.
To move a file from one directory to another, we use the same syntax:
$ mv filename directory
The filename will be moved to the specified directory, keeping its same name. Let’s try it: Use the
ls -icommand to look at the inode number of
Now move that file to the
docsdirectory we made earlier:
mv MarkTwain docs
Change directory to the
docsdirectory and look at the inode number of
What do you notice? The inode number is the same; the file hasn’t moved, only the name has been moved from one directory to another.
Now change back to your home directory, and let’s move
docsdirectory and change its name to
magna_cartaat the same time.
mv proposal2 docs/magna_carta
By specifying a new name after the destination directory, we can rename the file at the same time we move it.
See if you can do the same thing by moving the file,
timecal, to your
binsubdirectory and renaming it to
(Make sure you are in your home directory.)
docsdirectory renaming it to
Now let’s put a couple commands together to accomplish the following task:
Change directories to your
Make a new subdirectory called
Anon, (for Anonymous).
Move the three files,
twisterinto that new directory.
Verify that you have done this, and change directory back to your home directory.
Good job! Did you try using one
mvcommand for all three files?
You can. The
mvcommand has a third syntax:
mv file1 file2 file3 ... fileN directory
The last argument must be a directory name, and all the preceding files will be moved into that directory - keeping their original names. Let’s try it. Move the files:
text.err, text.fxd, small_town, and
editsdirectory using the command:
mv text* small_town spellk edits
Check the results by listing the contents of the edits directory. Pretty fancy?
What happens when you move a file onto a file that already exists?
Let’s try it. Enter the command:
ls -l letter bigfile
Notice the sizes of the two files. (You may want to head them to remind you what they are.)
mv letter bigfile
Did you get any error message? Any kind of message?
bigfilenow. How big is it? What are its contents?
Don’t worry; we’ll get
Now try doing the following moves:
miscdirectory, keeping the same name.
etcdirectory, changing its name to
Don’t panic on this one, but try moving the file
better_town, which is in the
miscdirectory to the
editsdirectory, keeping the same name.
Part IV - Copying Files¶
Copying works just like moving except that a second file is created, the contents of which is exactly like the original.
Copying files is useful for making backup copies. Let’s make a backup of your
letterfile by typing the following command:
cp letter letter.bak
Notice that it has the same syntax as the mv command, but now you have two files.
ls -licommand with arguments of
Notice the sizes are the same, but they each have their own inode and their own data.
Another reason to use the
cpcommand is to copy a file from somewhere else on the system to your directory. In this case, the filename stays the same, but the second argument is a new directory into which we put the file. Try it with:
cp /etc/hosts etc
You have just copied the file
/etcdirectory to your own personal
etcdirectory. Verify that the file is in your
Now use the
cpcommand to copy the file
/home/cis90/depotdirectory to your
Shakespearedirectory. (Notice that your
Shakespearedirectory doesn’t have a
cpcommand is also destructive. If the target file exists, it will be destroyed, and copied over. If you want the
cpcommand to warn you about destroying a target file, then you must use the
-ioption. Try this in your home directory:
cp -i letter letter.bak
Notice how it asked you whether you wanted to overwrite the
cpcommand may also be used to copy multiple files to a single directory in the same way you used the
More useful is copying an entire directory. Let’s copy the entire
Shakespearedirectory to a directory called
poemsdirectory. Now issue the following command:
cp -r Shakespeare Sonnets
Verify that it worked using the command:
-rstands for recursive.)
Part V - Removing Files and Directories¶
Removing files is inherently destructive in nature. Unix does not give any warnings or opportunity to un-remove a file. So be careful in using this command.
Make sure you are back in your home directory. What command did you use?
The remove command is dangerous in it’s simplicity. It takes one or more filenames as arguments. Try removing your
emptyfile with the command:
Try listing the file; it’s gone - for good - no warning - poof.
If you would like the remove command to ask you if you’re sure, use the
rm -i letter.bak
Answer with anything but a ‘y’ or ‘yes’ and it will not remove the file. Go ahead and remove it.
For some fun, change your directory to
Lab2.0and run the
Try removing the file
Do you see why spaces in file names is not a good idea? How could you remove that file?
Finally, try this command:
rm -i *
Answer y to all questions. Are all the files gone? Change back to your home directory.
From your home directory, try to remove the directory,
The remove command doesn’t work on directory files. For that we have the
rmdircommand. Try it:
Why didn’t it work?
List the entire contents of the
Lab2.0directory using the
Remove the hidden file
.junk, and then try the
The lesson here is that
rmdirremoves only empty directories. There is a way to remove an entire directory and its contents. This is obviously a very dangerous command. From your home directory, run the following command:
rm -r Lab2.1
Notice that the directory and all its contents are gone, no warnings.
Be careful when using this recursive option to the
Sonnetsdirectory and its contents.
There’s a program to help you check your work called
check5. Run it and it tells you what steps you did correctly and which ones need fixing. Here’s how it looks:
$ check5 ================================ Making-Directories-Step-2 Output from ls -F . follows: bin/ class/ edits/ Lab2.1/ mail/ newscript* butt dead.letter etc/ letter mbox poems/ cis90.contribution docs/ Hidden/ letter.bak misc/ TestFile Directory /home/cis90/simben90/edits was found --- OK --- Directory /home/cis90/simben90/docs was found --- OK --- Directory /home/cis90/simben90/etc was found --- OK --- You should see edits, docs, etc directories Continue?
Enter key to continue. If you see something that needs fixing, exit the program with
Ctrl-C. After you fix the problem restart the checker. When you finish you’ll see a screen like the following:
============================================================== Submit this tar file on Canvas! ============================================================== I have created a tar file of your home directory in: /tmp/files-simben90-3o340p.tar
A different file will be created for you every run. Submit the file on Canvas.