. 2) Commands like head, tail, less, more, and wc can be used to view file contents. head shows the first few lines, tail the last, and less/more page through. wc counts words. 3) Sorting and piping commands like sort, cat, and who are demonstrated. Pipes | connect outputs as inputs for efficient processing. 4) Wildcards like * and ? can be used to">. 2) Commands like head, tail, less, more, and wc can be used to view file contents. head shows the first few lines, tail the last, and less/more page through. wc counts words. 3) Sorting and piping commands like sort, cat, and who are demonstrated. Pipes | connect outputs as inputs for efficient processing. 4) Wildcards like * and ? can be used to">
CSC314 Lab4
CSC314 Lab4
CSC314 Lab4
Semester 391
Introduction
In this lab, you will use the Linux command line to manage files and folders and perform some
basic administrative tasks.
Recommended Equipment
A computer with a Linux OS, either installed physically or in a virtual machine
b. To access the command line, click Dash, and type terminal in the search field and press Enter. The
default terminal emulator opens. OR press Ctrl – Alt + T to open the terminal.
c. In the current directory, use the mkdir command to create three new folders: ITEfolder1,
ITEfolder2, and ITEfolder3. Type mkdir ITEfolder1 and press Enter. Create ITEfolder2 and
ITEfolder3.
d. Copy both the files test1 and test2 into ITEfolder2 from the current folder. Use ls command to check the
content of ITEfolder2 without moving to ITEfolder2.
e. Appending Standard Output with Redirection Operator >>: Appends in existing file with ‘>>‘ (double
greater than) symbol. Here, contents of test1 file will be appended at the end of test2 file.
a. Type cat test2.txt to see the content of the test2.txt. You won’t be able to see the full content of
the file as the terminal scrolls down and shows only the last part of the file.
b. To see the content of a file page by page, use command more. Type cat test2.txt | more.
You can also use these keys to move through the output:
[Space] - scrolls the display, one screenful of data at a time
[Enter] - scrolls the display one line
c. You can also append at the end of a single text file using the >> operator as follows:
Use the cat command to check the content of test1.txt. Using >> operator, merge the files
test1.txt and test2.txt again.
Step 3:
i. less command:
The command less writes the contents of a file onto the screen a page at a time. Type
Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.
As you can see, less is used in preference to cat for long files.
ii. head
The head command writes the first ten lines of a file to the screen. First clear the screen then type
Now, type
head -3 test2.txt
What difference did the -3 do to the head command? (Displays only the first three lines of the file).
The command does take any positive number as argument/option.
iii. tail
The tail command writes the last ten lines of a file to the screen. Clear the screen and type
The three numbers shown above are 45 (number of lines), 49 (number of words) and 322 (number of bytes)
of the file. To do a word count on test2.txt, type
Step 4:
v. Redirecting the Input:
i. We use the < symbol to redirect the input of a command. The command sort
alphabetically or numerically sorts a list. Type
ii. Using < you can redirect the input to come from a file rather than the keyboard. For
example, to sort the list of country names, type
Step 5:
Pipes
i. To see who is on the system with you, type
who
ii. One method to get a sorted list of names is to type,
This is a bit slow and you have to remember to remove the temporary file called names when
you have finished. What you really want to do is connect the output of the who command
directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe
is the vertical bar |.
iii. The following command will give you same result as above, but quicker and cleaner.
iv. One more example, if you want to list all the files and folders in your current directory and get them
sorted, Type:
v. Display the number of lines in your test1_sort.txt file using the pipe and cat command as follows:
vi. Sort the content of test2.txt and display the first 10 lines of the file
Step 6:
The * wildcard
i. The character * is called a wildcard, and will match against none or more character(s) in a file
(or directory) name. For example, in your ITEfolder1 directory, type
The above command is displaying all the files in the current directory that starts with “test…….”.
ii. Try typing
This will list all files in the current directory ending with .txt
iii. The ? wildcard
The character ? will match exactly one character. So ?ouse will match files like house and mouse, but
not grouse. Try typing