Unix is an operating system that is capable of handling multiple tasks from multiple users at the same time. This OS is widely used in all forms of computing systems.
Some people might confuse Unix with Linux. Linux has several features similar to Unix but still has some major differences. Linux is a clone of Unix. Even though Linux behaves like Unix, the code written to develop Linux is different from that of Unix.
Before Linux and Windows came into the limelight, the computing world was pretty much dominated by Unix. If you want to learn about the differences between Unix and Linux in detail, check out this article.
In this article, we will learn the basics of the Unix operating system, its architecture, basic commands, and file system. Let’s dive right in.
The Unix operating system has a 4-layered architecture. It consists of hardware, kernel, shell (system call interface), and application programs.
- Hardware is the innermost layer in the Unix architecture.
- The kernel is the heart of the operating system. The kernel controls the hardware and handles tasks such as memory management, task scheduling, file management, etc. Kernel interacts directly with the hardware of the system.
- The shell acts as the interface between the kernel and the application programs. Users can interact with the shell using shell commands. Shell processes your commands or requests and calls the required program to execute the specific tasks that you want. There are more than 250 shell commands.
- Application programs consist of software available in the system for users to perform tasks easily.
Unix File System
All data in Unix operating system is organized into files, which are then further organized into directories. Those are further organized into the file system, which is a tree-like structure.
In Unix, a hierarchial file-system structure is used, which is like an upside-down tree, with root ( represented using “/” symbol) at the base of the file system, and the other directories spreading from the root.
If you have a Linux system, you can execute the commands inside the terminal. If you have a Windows/Mac system, you can download software like GitBash, or Cygwin to execute Unix commands.
Absolute Pathname and Relative Pathname
In relative pathname, the file location is determined with respect to the current directory.
Suppose you are inside the Desktop folder, and you want to move to the directory called “demo” within Desktop folder using relative pathname. Since you are inside Desktop, you can simply write the following command.
But, if you wanted to move to the same folder using absolute pathname, you have to specify the entire path starting from the root itself.
That is the difference between absolute pathname and relative pathname in Unix.
Basic Unix Commands
Let’s look at some of the basic Unix commands that are used for managing files and directories.
This command is used to display text on the console.
On a Unix system, you can use the vi editor to create ordinary files.
- This command will open a new file with the name “myfile”.
- Press the “i” key to go to the edit mode.
- Now, type the file content.
- Press the “esc” key to come out of the edit mode.
- Press Shift+Z twice together to come out of the file completely.
Once you do all these, you’ll have a file created with the given filename in the current directory.
Display Contents of a File
You can use the cat command followed by the filename to display the contents of a file.
If you want to display the content of the file with line numbers, you can use -b option along with the cat command.
cat -b myfile
Counting Words in a File
If you want to count the number of words in a file, you can use the wc command followed by the filename.
In the output, you can see that it displays 4 columns.
- The first line represents the total number of lines in the file.
- The second column represents the number of words in the file.
- The third column represents the number of characters in the file.
- And the fourth column represents the filename.
If you want to make a copy of a file, you can use the cp command followed by the original filename and the new filename.
cp myfile myfile2
If you want to rename an existing file, you can use the mv command followed by the old name and then the new name.
mv myfile2 mynewfile
To delete a file, you can use the rm command followed by the filename.
If you want to delete multiple files at the same time, you can specify the filenames separated by space.
rm myfile mynewfile
Home directory is the directory in which you find yourself when you first login. You can go to your home directory anytime using the following command.
If you want to go to any other user’s home directory, you can add the username after the ~ symbol.
If you want to go to the previous directory, use the following command.
Present Working Directory
You can use the pwd command to determine where you are within the file system hierarchy at any time.
Listing Files in a Directory
You can use the ls command to list the files in a directory.
If you want to list the files in the present directory, you can simply type in ls.
If you want to move from one directory to another, you can use the cd command.
You can create a new directory using the mkdir command.
You can use the rmdir command to remove or delete a directory.
To rename directories, you can use the mv command, just like you use it with files.
mv old_name new_name
Types of Files
The files in Unix systems can be classified into 3 major types, which are:
- Ordinary files – Files that contain data, text, or program.
- Directories – These are the folders in the file system.
- Special files – These are real physical devices connected to the computer for input/output operations like printer, tape drive, etc.
In Unix, there are certain restrictions or permissions associated with each file. Why do we need this in Unix? Well, unlike other operating systems, Unix is a multi-user operating system. The actions of one user shouldn’t crash the computer or interfere with the files belonging to another user. Hence, file permission is necessary for Unix OS.
There are different types of permissions in Unix. The following are the permissions for files and directories.
|Read||This permission grants the capability to read the content of a file.||It allows looking at the filenames inside the directory.|
|Write||This permission allows to modify or remove the content of a file.||It gives the capability to add or delete files from the directory.|
|Execute||This permission grants the ability to run the file as a program.||It allows running a search on the directory.|
Each file in the file system is associated with a set of identifiers that are used to determine who can access the file.
- User ID (UID) – It specifies the user that owns the file
- Group ID (GID) – It specifies the user-group that the file belongs to.
- User permission – It specifies the level of access given to the user (matching the file’s UID).
- Group permission – It specifies what action a member of the group can perform on the file.
- Other(world) permission – It specifies what action all other users can perform on the file.
A UNIX String of Information
All the permissions and other details associates with files and directories can be displayed if you type in the following command.
You can see that it prints some strings of text which we don’t understand. Let’s see how we can get al the details about the file or directory from these strings.
- The initial ” – ” means that the entry is a file. If it is the letter “d”, it means that the entry is a directory.
- The first 3 positions after “-” or “d” indicate the permissions of the owner. ( r stands for read, w stands for write and x stands for execute).
- The next 3 positions indicate the permissions of the group.
- The next 3 positions indicate the permissions of others.
- The number after the permissions shows the levels of directories.
- After that, the user name is given.
- Next to the user name, the group name is given.
- The number after that indicates the file size.
- Next to the file size, the date and time when the file was last modified are given.
- And finally, the file name or directory name is mentioned.
Now, let’s see some commands that are used to set permissions.
You can use the chmod command to change the permissions of a file or directory.
Given below is the syntax of the chmod command:
chmod [ugoa][[+-=][mode]] filename
- The first optional parameter indicates who needs the change in permissions. It can be the user (u), group (g), others (o), or all (a).
- The next parameter indicates the opcode. We can use either + (for adding permissions), – (for removing permissions), or = (for assigning permissions).
- The third parameter is the mode, which can be either r (read), w (write), or x (execute).
chmod ugo+w file1
This command adds write permission to user, group, and others.
This command gives read access to all.
There is also another way to use the chmod command. A number can be used to specify each set of permissions for a file.
chmod 755 file1
Here, you can see a 3-digit number in the command. The first digit represents the user, the second digit represents the group, and the third digit represents others.
The digit 4 is used to represent read access, 2 for write access and 1 for execute access. In the above example, the first digit (for the user) is 7, which is 4+2+1. It means that user has read, write and execute access. The second digit (for group) is 5, which means the group has read and execute access (4+1). Others also have the same access permissions (4+1).
You can use the chown command to change the ownership of a file or directory.
chown user2 file1
In the above example, the ownership of the file1 is given to the user2.
If you want to change the ownership of the group, use the chgrp command.
chgrp group2 file1
Pipes and Filters
The pipe lets you use multiple commands such that the output of one command serves as the input to the next one. The “|” symbol is used to denote a pipe.
Filters are programs that can take plain text as standard input (the text stored in a file or produced by some other program), and transforms the input into a meaningful format, and then returns it as standard output.
The filtered output of the first command is given to the next command when you pipe two commands.
Let’s see an example. Let’s say we have a file with some text. We want to highlight only the lines that do not contain the character “a”, and we want the result in reverse order. In this case, we can pipe three commands cat, grep and sort to obtain the desired result.
cat sample | grep -v a | sort -r
Some Important Unix Commands
head and tail
The head command displays the first n lines of the file. By default, n has the value 10.
head -5 sample.txt
Similarly, the tail command displays the last n lines of the file.
tail -5 sample.txt
The uniq command removes consecutive duplicate lines from the file.
If the duplicate lines are not consecutive, it will not remove those lines. In that case, you can sort the lines and then use the uniq command.
sort myfile | uniq
This command is the reverse of the cat command. It displays the content of a file from lines n to 1.
This command is used to number the lines of the text file.
The grep command is used to scan the file for the desired information and present the result in a format that you want.
For example, suppose you want to scan the file for the string “Apple” and print the results.
cat sample.txt | grep Apple
This command is used to sort out the contents of a file in an alphabetic order.
You can use several options available in Unix to change the functionality of a command.
For example, you can use the following options along with the sort command to change it’s functionality.
- -r : reverses sorting
- -n : sorts numerically
- -f : case-insensitive sorting
There are several other options associated with each command.
Unix is a very important operating system in the computing world. Every programmer should at least know some basics about the Unix OS. I hope this article was helpful in getting an overview of the Unix operating system.
If you are interested in learning shell scripting in Unix, I have written a beginner’s guide to shell scripting. You can check it out here.
Thanks for reading. Happy coding!