Classwork 1: More working with Linux
[Next Classwork]
This assignment will introduce the script command, and let you gain experience with different ways of creating, moving, and renaming both files and directories.
Complete the following:
- Ensure the machine is booted into Linux
- Login to Linux
- Open a termimal from the top left Applications menu -> System Tools -> Terminal
- Start a script, this will create a log, a file that will contain all of
the commands you run, and the output you see while doing this assignment. This
command will start running and continue running until you type exit at
the end of this classwork.
linux1[17]% script Script started, file is typescript
- Move into the csmc104 directory.
linux3[4]% cd csmc104
- Create a directory to keep all of the work for this assignment
linux1[1]% mkdir cw01
- Type ls and verify your cw01 directory is there.
linux1[2]% ls cw01 hw01 hw02 hw03 hw04 hw05 hw06 hw07 hw08 hw09
- Find out where you currently are with the pwd command.
linux3[4]% pwd /afs/umbc.edu/users/d/s/cgrasso/home
- Move into the cw01 directory you just created
linux3[5]% cd cw01
- After moving into your new cw01 directory, use pwd again to
verify your new location
linux3[6]% pwd /afs/umbc.edu/users/d/s/cgrasso/home/cw01
- Use echo to write a line of text to a file by entering the following
command:
linux3[7]% echo All work and no play ... > file1.txt
- Use ls to verify the file exists, and then use cat to
display the contents of the file as shown below.
linux3[8]% ls file1.txt linux3[9]% cat file1.txt All work and no play ...
- Create a copy of the file by using the cp command. cp takes two
arguments (arguments are the term used to refer to the set of options we pass
to the commands in Unix - Note that arguments are separated by spaces, so you
will normally not use spaces in unix commands or filenames. It can be done, it
is just more difficult). The first argument is the file you are copying FROM,
the second is the file you are copying TO. So to copy from file1.txt to
file2.txt we do:
linux3[10]% cp file1.txt file2.txt
- We can now verify that both files exists with ls and verify
file2.txt has the same content as file1.txt with cat
linux3[11]% ls file1.txt file2.txt linux3[12]% cat file2.txt All work and no play ...
- We will now make another directory, inside our cw01 directory
called mydir
linux3[13]% mkdir mydir
- Once again, ls allows us to make sure we know what files are in
our current directory
linux3[14]% ls file1.txt file2.txt mydir
- Lets create a file3.txt that will have much better contents then either
file1.txt or file2.txt, once again we'll use echo
linux3[15]% echo I voted for Bambi > file3.txt
- ls lets us verify that our file was created
linux3[16]% ls file1.txt file2.txt file3.txt mydir
- Now we will rename file3.txt to have a more appropriate name using the
mv command. Like cp, this command takes two arguments the first
is the file to move (or rename) and the second argument is the new location or
name of the file. If the second argument is a directory name, then the file
will be moved into the directory. If the second argument is a filename (or the
name is something that doesn't exist) the file will be renamed.
linux3[17]% mv file3.txt bambi.txt
- After renaming the file again use ls to verify the new directory
contents.
linux3[18]% ls bambi.txt file1.txt file2.txt mydir
- You need to be careful, lets now overwrite our file2.txt by renaming
bambi.txt to have the name file2.txt. You will notice that the mv
command will overwrite an existing file without asking, so if you are not
careful you can lose work. So we'll rename bambi.txt and the use cat
and ls to verify the files in our directory and their contents as seen
below.
linux3[22]% mv bambi.txt file2.txt linux3[23]% cat file2.txt I voted for Bambi linux3[24]% ls file1.txt file2.txt mydir
- Now lets move file2.txt to the mydir directory we created a while ago.
linux3[25]% mv file2.txt mydir linux3[26]% ls file1.txt mydir
- We can move into mydir with the cd command and then use ls again
to make sure file2.txt is now in there, and use pwd to make sure of
where we are.
linux3[28]% cd mydir linux3[29]% ls file2.txt linux3[30]% pwd /afs/umbc.edu/users/d/s/cgrasso/home/cw01/mydir
- And we can thengo back up a directory, to our cw01 directory, using
cd and passing the special argument .. which indicates the
parent
linux3[32]% cd .. linux3[33]% pwd /afs/umbc.edu/users/d/s/cgrasso/home/cw01
- Now lets try to remove the directory we created using the rm
command
linux3[34]% rm mydir rm: cannot remove `mydir': Is a directory
- The rm command is kind enough to remind us that is isn't normally
used to remove directories. The rmdir command should be used, so let's
try that.
linux3[35]% rmdir mydir rmdir: failed to remove `mydir': Directory not empty
- The rmdir command is being careful, and tells us that it won't
normally remove a directory that still contains files. So now I want you to
use cd to go back into the mydir directory, and then use mv to
move the file2.txt from the mydir directory and into the parent directory
(remember, the parent directory is always called ..)
See if you can figure out the commands to use, ask for help if you need it
- Once the file is moved, use ls and pwd to verify where you are and what files are there. You should end up with file1.txt and file2.txt in the cw01 directory
- Once file2.txt is in the right location, also try renaming file2.txt, call it final.txt
- Also go and cleanup the mydir directory now that it is empty. Use the rmdir command to remove the now empty mydir directory.
- Once file1.txt and final.txt are in the same directory, lets end the
script command we were running at the start of this exercise. We use
the exit command to end a script session. The log will let me see what
commands you ran. So execute the following commands:
linux3[37]% exit exit Script done, file is typescript linux3[11]% ls cmsc104 Downloads Music pub typescript bin Desktop java Pictures Public Videos
- Now lets use mv to move AND rename the log, currently called
typescript, that was created. Run the following command to do so:
linux3[12]% mv typescript cmsc104/cw01/log.txt linux3[13]% cd cmsc104/cw01/ linux3[14]% ls file1.txt final.txt log.txt
- I want you to submit three files for this classwork, file1.txt,
final.txt, and your log.txt. Do this by typing the following command
linux3[26]% submit cs104_grasso cw01 file1.txt final.txt log.txt
- After submitting your files, you can verify your file was submitting by using the submitls command.
- MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!