The following exercises are intended to familiarise you with some basic GNU/Linux command line. This familiarity is necessary as you will be using the ssh command line utility to gain remote access to remote wireless servers. Refer to http://www.google.com and the on-line manual pages (see below) for extra help. Throughout these notes the courier font represents a terminals output, or a program name, or a filename whereas the bold courier font represents keyboard input. The bold italic font represents keyboard input requiring some thought and modification.
Basic file and directory commands
Try out the echo command as follows:
$ echo "hello, world"
Make a note of the output produced.
This command can be used to display environment variables defined by the operating system and can also be used to create files as we shall see shortly.
$ echo $OSTYPE
and make a note of the result:
$ echo $PATH
and make a note of the result:
The PATH environment variable defines a list of directories which are searched when a program is required to be executed, . is the current directory.
Create a directory using the command
$ mkdir dir1
Change to this directory by typing
$ cd dir1
Note that you can change back to your previous directory by typing
$ cd ..
The command to list the contents of a directory is
Try this command after you have changed to dir1. Make a note of the output:
$ echo "first text string" > file1
and use the
command again. Make a note of the output:
The contents of the file you have just created using the output redirection operator > can be displayed using the catenate command. Type
$ cat file1
The > operator overwrites the specified file each time it is used. The >> operator adds to the end of the specified file.
Task - Create two other files in dir1 called file2 and file3 only making use of the echo command and output redirection operators. file2 should contain two lines of text and file3, three lines. Make a note of the commands used to create the files and the command used to verify the contents of each file:
Further information about files in a directory can be obtained by modifying the ls command. Type
$ ls -l
in the dir1 directory and make a note of the output:
The ls command has a wide variety of options which can be applied to it. To find out more about this command consult the online manual pages using the man command ie. type
$ man ls
Press h to get help about how to move through the pages and how to quit viewing the man pages. Use the man command on the other commands used so far ie. echo, mkdir, cd, man. What does the -k option on man do? Make a note of your answer?
Also access the man pages for the following commands - pwd, cp, rm, rmdir. Write a one-line synopsis of each one:
$ ls -lt
and make a note of the difference in the output produced without the t option. What does this option do?
$ touch file2
This command updates the timestamp for the specified file. Verify the answer using an appropriate command. Write the command down:
Task - You are interested in discovering which GNU/Linux commands can be used to manipulate files. Which command would you use to discover this information? Rather than displaying the information to the screen, you are would like to write the output of this command to a file called fileinfo. Write down the GNU/Linux command to perform this operation. Try out the command.
Find out about the head and tail commands using the on-line manual pages. Make a note of the operation they perform:
Commands in GNU/Linux can be connected together with the pipe operator |. This operator sends the output of one command into the input of the next.
Describe the operation of the piped commands cat fileinfo | head by trying it out first:
$ cat fileinfo | tail
and describe it’s operation:
Find out about the more and less commands using the on-line manual pages. Make a note of the operation they perform:
Task - Finally try out both more and less on the file fileinfo. Which major feature of these commands distinguishes them from each other? Use h while executing these commands for help on moving around the file and quitting. Where have you seen one one of these help screens before? Try and discover why this is the case.
The GNU/Linux environment is famous for the large number of software tools which are associated with it. In this lab we will investigate some of these tools.
Searching and sorting tools
The command grep which stands for ’get regular expression and print’ is used to search for character strings in files. Try the command
$ grep "sort" fileinfo
which searches the file fileinfo for occurrences of the string sort, and make a note of the output produced (you do not have to write down lines which are repeated).
A seasoned GNU/Linux hacker would probably have searched for the same information by executing this command:
$ cat fileinfo | grep "sort"
$ man -k files | grep "sort"
What is the major difference in this last command to the other ways in which you obtained the same information? Write your answer below:
The sort command sorts a specified file - the key used is the value of a specified field (default is first field) of each line of the file. Now create another file in your dir1 directory with a few characters in it (see last weeks lab) calledfile4 and then try the following command, making sure you are in your home directory
$ cd $ ls -l dir1 | sort +4
Make a note of the output and write down the function the command performs. What does the +4 option do? Try leaving it out or try other values eg. +7. Write your answer below:
The find command is used to identify files with specified characteristics. Try the command
$ find $HOME -name ’file*’ -print
and make a note of the output:
This command searches, starting at your home directory, including all subdirectories, for files starting with the string ’file’ followed by any other character (*). Note the use of the environment variable $HOME and the single quotes placed around the search string. What command would you use to verify the value of the environment variable. Write down the command and make a note of its output:
Now try out this extremely useful variation of the find command:
$ find $HOME -mtime -1 -print
Make a note of the output produced. What operation does this command perform? Use the man pages to find out. Note that -1 in this context means "less than 1".
Often when undertaking activities, it is useful to maintain a record of what you typed on a machine to complete a given task. A useful GNU/Linux command which will copy all keyboard and screen characters to a file is script. Firstly type man script and read the description. Now type:
$ script scriptinfo $ man -k files | grep "sort" $ find $HOME -mtime -1 -print $ ls -l dir1 | sort +4 $ exit
Now display the contents of the file scriptinfo. What does it contain? This technique will be used to effect in your next assignment.