lab4(1) (1)
-
Upload
yusuf-mohamed -
Category
Documents
-
view
218 -
download
0
Transcript of lab4(1) (1)
-
7/30/2019 lab4(1) (1)
1/7
Unix Labs
Unix Lab 4b
Files Moving, Copying and removing files
In the last lab we showed how you could create an empty file with thetouch
command: touch
file1
Renaming a file is done by moving it from one name to another:
$mv file1 precious
To make a copy of the file the cp command:use
$cp precious precious.old
creates a duplicate copy of precious precious.old
To remove some of the files you have created from above
$rm precious file1
Unix is case sensitive so Junk is not the same as junk.
******************************************************************************
Some file processing commands:
grep, sort, wc, cut, paste
First create the following text using the text editor in and save it to your directory as poem .
Great fleas have little fleas
upon their backs to bite em
And little fleas have lesser fleas,
and so ad infinitum.
And the great fleas themselves, in turn,
have greater fleas to go on;
While these again have greater still,
and greater still, and so on.
$wc poem the output will be : 8 46 263 poem
which tells you , poem has 8 lines, 46 words and 263 characters.
The wc command options, when no option is specified the default is all options.
1
-
7/30/2019 lab4(1) (1)
2/7
Unix Labs
OPTION OPERATION
-l Reports the number of lines
-w Reports the number of words
-c Reports the number of characters
You can also specify more than one file name in the argument:
$wc file1 file2
will print out the word count and number of lines for files file1 and file2.
grep: You can use the grep command to search for a specified pattern in a file or list of files. The
pattern used by the grep command is called regular expression, whence the strange name
of the command ( Global Regular Expression Print). grep is a file searching and selection
command. You specify the filename and the pattern to be looked for in the file and when
grep finds a match, the line containing the specified pattern is displayed.
Suppose you wish to look for the word fleas in poem.
$ grep fleas poem
grep will also look for lines that dont match the pattern when the option v is used.
grep v fleas poem
grep is useful for displaying only those lines in a large file that you are interested in. For example
if your login name is mohanlo to show the details in the password file for this user:
grep mohanlo passwd
Understanding fields in /etc/passwd
The /etc/passwd contains one entry per line for each user (or user account) of the system. All
fields are separated by a colon (:) symbol. Total seven fields as follows.
Generally, passwd file entry looks as follows
2
http://files.cyberciti.biz/ssb.images/uploaded_images/passwd-file-791527.png -
7/30/2019 lab4(1) (1)
3/7
Unix Labs
1. Username: It is used when user logs in. It should be between 1 and 32 characters in
length.
2. Password: An x character indicates that encrypted password is stored in
/etc/shadow file.
3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reservedfor root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999
are reserved by system for administrative and system accounts/groups.
4. Group ID (GID): The primary group ID (stored in /etc/group file)
5. User ID Info: The comment field. It allow you to add extra information about the
users such as user's full name, phone number etc. This field use by finger command.
6. Home directory: The absolute path to the directory the user will be in when they log
in. If this directory does not exists then users directory becomes /
7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this
is a shell. Please note that it does not have to be a shell.
Task 1: To find the number of students from ft281 that have student accounts on
the system. Usegrep to list all the entries for students from this class in the
passwd file to a new file called list (use the redirection > symbol to redirect the
output from the grep command to the new file). Then use wc command on temp
tofind the number of students in your class.
grep options
Option OPeration
-c Displays only the count of the matching lines in
each file that contains the match
-i Ignores the distinction between lowercase and
uppercase letters in the search pattern
-l displays the names of the files with one or more
matching lines, not the lines themselves
-n Displays a line number before each output line.
-v Displays only those lines that do not match the
pattern
3
-
7/30/2019 lab4(1) (1)
4/7
Unix Labs
sort - sorts its input into alphabetic order line by line. Try this for the file poem:
sort poem
Sorting is line by line, but the default sorting order puts blanks first, then upper case letters, then lower
case, so its not strictly alphabetical.
sort has many options to control the order of sorting ( man sort) but the most common are:
sort r Reverse normal order
sort -n Sort in numeric order
sort nr Sort in reverse numeric order
sort f Fold upper and lower case together
sort +n Sort starting at the n+1 field
sort u suppresses all but one of each group of lines that are identical in the sort field
Task 2 We want a list of all the students logins as well as the students full name in ft281-2 sorted
alphabetically and stored in a file called ft281-, this information will be extracted from the passwd
file2. You will need a further command cut (see below) to obtain this list. Note the file should only
have the login name and fullname of the student in it.
You can use cut command to cut out specific columns or fields from files. Many files are
collections of records, each record consisting of several fields. You might be interested in some
of the fields or columns contained in a file.
Often you have to tell cut what character delimits each field (tab is the default delimiter). For
instance in the passwd file the : delimits each field. so if we wanted to display just the first field
of every entry in the passwd file:
cut f1 d: passwd
If we wanted to show the home directories of all users we :
cut f6 d: passwd
We can also display two fields simultaneously:
cut f1,6 d: passwd
cut options
Option Operation
-f specifies the field position
-c specifies the character position
4
-
7/30/2019 lab4(1) (1)
5/7
Unix Labs
-d Specifies the field seperator
paste You can use paste command to join files together line by line, or you can create new files
by pasting together fields from two or more files.
Use the grep command to find all the entries in the passwd file with ft281-2 references and
redirect the output to a file ft281-2.dat.
use the cut command to extract the first field:
cut f1 d: ft281-2.dat > usernames.dat
Similarily extract the fifth field with:
cut f5 -d: ft281-2 > fullnames.dat
We now have two files both of which have the same number of lines and consist of a single
column. We can now paste the two files together.
paste usernames.dat fullnames.dat > studentnames.dat
Task3 From task 1 the wc command would have given you more than the number of students in
the class use the cut command to obtain the number of students. (i.e. the final output
should be just 4 )
Pipes
All of the examples above rely on the same method: putting the output of one program into the
input of another via a temporary file. But the temporary file is a clumsy way of operating. A way
to avoid using a temporary is to employ a pipe. A pipe is a way to connect the output of one
program to the input of another program without any temporary file.
$ who | sort Print sorted list of users
$who | wc -l Counts the number of users logged in
Task4
Use the pipe to :
Count the number of files in your directory.
Count the number of files in the /etc directory.
Count the number of users in the passwd file.
You should also be able to obtain the correct output for task1,2,and 3 above with a single
command line using pipes (see below for use of multiple pipes).
5
-
7/30/2019 lab4(1) (1)
6/7
Unix Labs
You can have as many programs in a pipeline as you wish:
To find how many times a user is logged on (try andreas1)
who | grep andreas1 | wc -l
Task 5 Produce a sorted list of all the student groups from the password file. There should be one
line for each group. A group should not be list twice.
Summary of commands
cat filename write contents of filename to the screencd directory_name change current directory to directory_name
grep phrase filename search filename for phrase and print matching lines
grep phrase read from the standard input, and print lines matching phrase
grep -v phrase print all lines not matching phrase
ls List files in current directory (not including files that start with a dot.
ls directory_name List files in directory_name
ls -l List files with permissions, ownership and last modified date
ls -a List all files, including files that start with a dot (like the .netscape
directory)
ls -r List contents recursively, that is , list contents of subdirectories.
man command Show online documentation/help about command
man -k keyword Show commands related to keyword. Handy if you need help, but
don'tknow the name of the command
mkdir directory_name Makes a directory called directory_name
pwd Prints the name of the current directory you are inls -a List all files, including files that start with a dot
rm filename Removes filename
rm -f f is for "force". With this option, files are removed without confirmation.
rm -r directory_name recursively delete directory_name and its contents.
6
-
7/30/2019 lab4(1) (1)
7/7
Unix Labs
rmdir directory_name Removes a directory called directory_name. Only works on empty
directories. to remove a nonempty directory, see rm -rf
wc filename print the number of lines, words, and characters in filename. If No
filename is given, stdin is used.
7