In a shell, we use symbol "<" to redirect standard input, and ">" to redirect standard output, and "2>" to redirect standard error.
The following example sends the output from program
ls
to file
foo
:
%
ls -l >
foo
Type the above command, and then check the content of file
foo
with command
cat foo
.
Our next example shows that the command
sort
reads lines from file
foo
and sort them in alphabetical order and displays the
results in the standard output (terminal screen):
%
sort <
foo
5.
Archive and Extract Files with
tar
We often need to archive files and extract files from an archive. The Unix command for this task is
tar
. The command
tar
stands
for Tape ARchive, since in the old days archives were stored on agnetic tapes. The command has numerous options. Check its
manual page with
man tar
.
One commonly used option is to create an archive file:
tar -cvf
your_tar_filename
.tar
directory_name
to create a tar file
your_tar_filename.tar
from files in the directory
directory_name
. For example the following command:
%
tar
-cvf
~/lab02.tar
~/lab02
The above command would create tar file
lab02.tar
containing all files under directory
lab02
. The tar file is stored under your
home directory.
Another commonly used option is to extract all files from tar file (often called tar ball):
%
mkdir tmp
%
cd tmp
%
tar -xvf ~/lab02.tar
The first command creates a new directory
tmp
. The second command sets your current directory to
tmp
. The last command
extracts all files in directory
tmp
.
Try the above examples. Try to create archives from other directories and extract the tar files (in a different directory).
6.
File Compression and Decompression with
gzip
and
gunzip
To compress a file, say
lab02.tar
, use command
%
gzip
lab02.tar
this will create a compressed file named
lab02.tar.gz
. To uncompress it, just type the command
%
gunzip lab02.tar.gz
Try the two commands on a file
Note that on Linux system, compression and uncompression are added to the tar command. For example, you can archive files
and then compress the archive in a single command using option
z
, such as
%
tar
-zcvf
~/lab02.tar.gz
~/lab02
Theory Exercises
Stallings (8th and 9th edition): Problems 2.1 and 2.4
Dashboard
|
myMurdoch Advice

/
C Programming Exercises
Note, all exercises must be done via the terminal commands, not GUI tools. Do not use IDE to build your programs. Do not use GUI
file managers to manage your directories and files. Also each exercise should be in a separate directory, which itself, is under a
directory called
lab02
1.
Formatted Output with
printf
Last week, we used the
printf
function to send output to the standard output. This function allows you to construct complex
strings consisting of strings, integers, floats, chars, etc, in a very flexible way. For example, the following code constructs the

You've reached the end of your free preview.
Want to read all 3 pages?
- One '14
- Shell, compress