Archive
Basic UNIX Tutorial || Chapter Three
3.1 Redirection
Most processes initiated by UNIX commands write to the standard output (that is, they write to the terminal screen), and many take their input from the standard input (that is, they read it from the keyboard). There is also the standard error, where processes write their error messages, by default, to the terminal screen.
We have already seen one use of the cat command to write the contents of a file to the screen.
Now type cat without specifing a file to read
% cat
Then type a few words on the keyboard and press the [Return] key.
Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.
What has happened?
If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the ‘end of file‘ (^D), copies it to the standard output (the screen).
In UNIX, we can redirect both the input and the output of commands.
3.2 Redirecting the Output
We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type
% cat > list1
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}
What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1
To read the contents of the file, type
% cat list1
Exercise 3a
Using the above method, create another file called list2 containing the following fruit: orange, plum, mango, grapefruit. Read the contents of list2
3.2.1 Appending to a file
The form >> appends standard output to a file. So to add more items to the file list1, type
% cat >> list1
Then type in the names of more fruit
peach
grape
orange
^D (Control D to stop)
To read the contents of the file, type
% cat list1
You should now have two files. One contains six fruit, the other contains four fruit.
We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Type
% cat list1 list2 > biglist
What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist
To read the contents of the new file, type
% cat biglist
3.3 Redirecting the Input
We use the < symbol to redirect the input of a command.
The command sort alphabetically or numerically sorts a list. Type
% sort
Then type in the names of some animals. Press [Return] after each one.
dog
cat
bird
ape
^D (control d to stop)
The output will be
ape
bird
cat
dog
Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type
% sort < biglist
and the sorted list will be output to the screen.
To output the sorted list to a file, type,
% sort < biglist > slist
Use cat to read the contents of the file slist
3.4 Pipes
To see who is on the system with you, type
% who
One method to get a sorted list of names is to type,
% who > names.txt
% sort < names.txt
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 |
For example, typing
% who | sort
will give the same result as above, but quicker and cleaner.
To find out how many users are logged on, type
% who | wc -l
Exercise 3b
Using pipes, display all lines of list1 and list2 containing the letter ‘p’, and sort the result.
% cat list1 list2 | grep p | sort
Summary
| Command | Meaning |
|---|---|
| command > file | redirect standard output to a file |
| command >> file | append standard output to a file |
| command < file | redirect standard input from a file |
| command1 | command2 | pipe the output of command1 to the input of command2 |
| cat file1 file2 > file0 | concatenate file1 and file2 to file0 |
| sort | sort data |
| who | list users currently logged in |
More Post:
UNIX Introduction
What is UNIX?
UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.
UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren’t covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session.
Types of UNIX
There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X.
Here in the School, we use Solaris on our servers and workstations, and Fedora Linux on the servers and desktop PCs.
The UNIX operating system
The UNIX operating system is made up of three parts; the kernel, the shell and the programs.
The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.
The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).
The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.
The tcsh shell has certain features to help the user inputting commands.
Filename Completion – By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.
History – The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.
Files and processes
Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.
Examples of files:
- a document (report, essay etc.)
- the text of a program written in some high-level programming language
- instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);
- a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files.
The Directory Structure
All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )
In the diagram above, we see that the home directory of the undergraduate student “ee51vn” contains two sub-directories (docs andpics) and a file called report.doc.
The full path to the file report.doc is “/home/its/ug1/ee51vn/report.doc”
Starting an UNIX terminal
To open an UNIX terminal window, click on the “Terminal” icon from Applications/Accessories menus.
An UNIX Terminal window will then appear with a % prompt, waiting for you to start entering commands.
Best UNIX/LINUX Tutorial for All Beginners
Introduction to the UNIX/LINUX Operating System
- What is UNIX?
- Files and processes
- The Directory Structure
- Starting an UNIX terminal
Chapter One
- Listing files and directories
- Making Directories
- Changing to a different Directory
- The directories . and ..
- Pathnames
- More about home directories and path names
Chapter Two
- Copying Files
- Moving Files
- Removing Files and directories
- Displaying the contents of a file on the screen
- Searching the contents of a file
Chapter Three
- Redirection
- Redirecting the Output
- Redirecting the Input
- Pipes
Chapter Four
- Wildcard
- Filename Conventions
- Getting Help
Chapter Five
- File system security (access rights)
- Changing access rights
- Processes and Jobs
- Listing suspended and background processes
- Killing a process
Chapter Six
- Other Useful UNIX commands
Chapter Seven
- Compiling UNIX software packages
- Download source code
- Extracting source code
- Configuring and creating the Makefile
- Building the package
- Running the software
- Stripping unnecessary code
Chapter Eight
- UNIX variables
- Environment variables
- Shell variables
- Using and setting variables
Related articles
- Unix Shell Scripting Tutorials (tech-faq.com)
- How to Copy UNIX Files to Windows (tech-faq.com)
- Users and Groups (wiki.archlinux.org)
- Unix Signals (tech-faq.com)
- Where to Download Unix (tech-faq.com)
Thinker
I am currently working as Application & Software Engineer in Huawei Technologies Bangladesh Ltd . My core skills include extensive knowledge and experience of HTML/XHTML & CSS as well as experience in PHP, Mysql, Wordpress and digital graphic design. I try to build web solutions, which evolve with the changing needs of your business. I am also experienced in administration and application of UNIX/Linux operating systems as well as the Windows family of operating systems.
Verified Services
View Full Profile →
Blog Status
- 66,686 Unique Visit
Archives
Categories
Top 5 Posts
My Twitt
- পথ চেনাবে বাংলাদেশি তরুণঃ 'ইয়াহু! হ্যাক ইউরোপ' প্রতিযোগিতার প্রথম স্থান পৃথিবীর নতুন কোনো দেশে পা রেখেছেন।... fb.me/30ciqHcFE 1 day ago
- পথ চেনাবে বাংলাদেশি তরুণ: পৃথিবীর নতুন কোনো দেশে পা রেখেছেন। নতুন স্থান, নতুন পরিবেশ সম্পর্কে কোনো ধারণাই নেই... fb.me/2xrA0XWDH 1 day ago
- চমৎকার একটি Android অ্যাপ্লিকেশান AirDroid ব্যাবহার করে দেখুন , এক কথায় কাজের জিনিস। AirDroid এর বিস্তারিতঃ... fb.me/ISGmcxID 4 days ago
- আরএকটি অসাধারণ মোবাইল ফোনঃ HTC One X+ ১) নতুন এই ফোনটিতে থাকছে ১.৭ গিগাহার্জ এর কোয়াড-কোর এনভিডিয়া টেগ্রা... fb.me/2PT45IBxA 4 days ago
- কিছু মজার মজার তথ্যঃ কম্পিউটার আবিষ্কার করার আগে আমাদের জীবন টা যেমন ছিল :) ১ - Windows ছিল ঘরের জানালা ২ -... fb.me/2qIILxzgZ 1 week ago











