File Navigation Basics Exercise
For help, use the course powerpoint or http://www.upscale.utoronto.ca/GeneralInterest/Harrison/LearnLinux/Module2.html
upon which this exercise is based
You will submit all this in one script file. You will start a script command
that will trap all everything on the terminal into a file. Feel free to put
more work than exactly these commands in your file.
- First, experiement with the script file:
- Create a script file:
-
Type: script fileNav
- Type ls
- Type who
- Type exit to close the
script file
- Look at the script file you created:
- more fileNav to see the
script file. See what you typed (ls, who) and its results in the file
- Add to the script file
- If you are interrupted, and need to continue from where you left off:
- script -a fileNav
- Type some command
- To stop the scripting, type exit.
- Look at the script file you created and see how the new script was
appended:
- more fileNav to see the
script file. See what you typed (ls, who) and its results in the file
- Start a script file to collect your work for Exercise 1, 2 and 3 below:
- Exercise 1
From your home directory execute pwd
and note the name of the parent of your home directory. Below we shall
call that directory /home but your system may be configured differently.
- List all the fles in your current directory using find
. -maxdepth 1 -type f (You are asking linux to find all files in
your current directory (.) that are actually in that directory (maxdepth
2) that have a type of file.
- List all the directories inside your current directory using find
. -maxdepth 1 -type d (d for directory)
Use cd / to go to the root directory.
View its contents.
Change to the /home directory and view
its contents using ls. Typically these are
the home directories of all users except for the master user root.
- Change to the /etc directory and view
its directories using ls. Typically these are
the directories of tools.
- List all the directories inside your current directory in alphabetical
order using find . -maxdepth 1 -type d | sort (d
for directory; you are piping | the find command output to the sort command)
Change to your home directory using cd ~ and
confirm that that is where you are using pwd.
- Note: pepper removed the instructions about info
password
Use the hostname command, with no options,
to print the hostname of the machine you are using.
Use man hostname to display some documentation
on the hostname command. Find out how to make it print the aliases for the
machine. You will need to scroll down the manpage to the Options
section.
- Exercise 2
Create a text file in your home directory called shakespear, containing
the following text: use cat to create it:
- Type cat > shakes.txt
- Shall I compare thee to a summers day?
Thou art more lovely and more temperate.
<ctrl > d
- Rename it to sonnet-18.txt. using mv shakes.txt
sonnet-18.txt
Make a new directory in your home directory, called poetry. using
mkdir poetry
Move the poem file into the new directory. mv
sonnet-18.txt poetry (How does this compare to the last mv command.
Why does the file stay the same name? )
The * wildcard on its own is expanded by the shell to a list of
all the files in the current directory. Use the echo command (echo
*) to see the result
Use quoting to make echo print out an actual * symbol.
echo '*'
In the poetry directory create another file, sonnet-29.txt: cd poetry
; cat > sonnet-29.txt << end-of-msg
When in disgrace with Fortune and mens
eyes,
I all alone beweep my outcast state,
end-of-msg
(See how the << created end-of-msg as the
marker for ending the message
Use the cat command to display both of the poems, using a wildcard.
cat son*
- Use the grep command to display only those lines with 'all' in them.
grep all son*
Copy the sonnet-29.txt to backupPoem.txt - cp
sonnet-29.txt backupPoem.txt
Use the ed sonnet-29.txt command and then change a line using substitution
to change men to people
- ed sonnet-29.txt
- 1 (to print the first line so you can
see it)
- s/men/people
- 1,$p (to see the change)
- w (to write the change)
- q (to quit)
Use the cmp command and the diff command to compare sonnet-29.txt
and backupPoem.txt
- cmp sonnet-29.txt
backupPoem.txt (see how it only shows the first difference. Is
it right about the difference?)
- cmp sonnet-29.txt backupPoem.txt -l (see
all the differences, showing the position of the differences)
- diff sonnet-29.txt backupPoem.txt
Create a directory in your 271 directory named tmp cd
~; cd 271; mkdir tmp (this assumes you have a 271 directory already.
If not, create it.)
Copy all the files in the poetry directory into the tmp one.
- cd poetry ( get to the poetry directory)
- pwd to ensure you are in the poetry
directory.
- cp * ../tmp
Return to your home directory (cd ~ )
to list all the contents of your directories (ls
*)
From your home directory, create a new file named poem3.txt inside
tmp. You will need to indicate the relative path (without a / at the beginning)
cat > 271/tmp/poem3.txt whateverYouWantToWrite
<ctrl> d
change to your tmp directory and rename (mv) one poem. cd
271/tmp ; mv poem3.txt poem3new.txt
copy the renamed poem back to your poetry directory. (you will need
to remember that .. means up one directory) cp poem3new.txt
../poetry
Finally, use the rm command to delete the tmp directory and the
poems in it. cd ../tmp; rm poe*; cd .. rmdir tmp
- End your script file:
- Type exit to close the script
file
- Type more fileNavHwk
to see the resulting file. It can have too much information but should
show all the commands you just executed in exercise 1, 2 & 3.
- Transfer your script file to your own pc so you can upload it to moodle.
- Start Filezilla
- file / site manager / new site / panther / panther.adelphi.edu &
SFTP & your user and pwd / connect
- click desktop on left
- click until you find your file on right
- drag from right to left (panther to your computer)
- The file is now on your desktop
File exercises credited to http://www.linuxtraining.co.uk/download/new_linux_course_modules.pdf
David M. Harrison.