Basic Unix Commands

What is Unix Command?

The below article gives an overview of Unix commands. An OS providing both command line interface (CLI) and graphical user interface (GUI) based interaction integrated by Dennis Ritchie, Douglas Mcllroy, Joe Ossanna Brian Kernighan, and Ken Thompson at Bell laboratory in 1970 called a multi-tasking operating system permitting two or more users to simultaneously operate on the operating system and offers commands for the users for interacting with the application from command line interface, such as sudo command, chmod command, su command, mv command, rm command, vi command, cat command, rmdir command, mkdir command, clear command, and ls command, which can be used to implement complex tasks.

Introduction to Unix

Unix is an OS that provides both CLI and GUI-based interaction. It was developed by Dennis Ritchie in the C language. Unix operating system is multitasking, which also gives an opportunity for two or more users to use its benefits. In other words, it is a multi-user OS. Ubuntu OS is a Unix version that enables us to do every work that Unix is supposed to do.

Hence, it's recommended by professionals who operate with servers; it's also recommended to learn how the command-line-based operating system works. Many large and complex applications that utilize Unix to execute because of its aspect to handle the processes easily. It's a bit faster and provides a nice user experience when compared with the Windows operating system.

Several testing activities, such as performance and installation testing, depending on OS knowledge. Almost every web server is Unix based nowadays. So, knowledge of Unix is essential for testers. If we are unfamiliar with Unix, then learning Unix commands can be a great start. One of the best ways to understand these commands is to practice and read them simultaneously on Unix OS.

Basic Commands in Unix

  • cal: This command is used to show the calendar.

Syntax:

Examples: If we need to show the calendar for March 2018, we can use the following Unix command:

  • date: It is used to show the system time and date.

Syntax:

Examples: If we need to show the date in dd/mm/yy format, we can use the following Unix command:

  • banner: It is used to show a large banner on a standard output.

Syntax:

Example: We can use the below command to print "Example" as a banner:

  • who: This command is used to show the user's list logged in currently.

Syntax:

Example: We can use the following command to list every user logged in currently:

  • whoami: This command is used to show the user id logged in currently.

Syntax:

Example: We can use the following command to list the users logged in currently:

  • touch: It creates a new file or upgrades its timestamp.

Syntax:

Example: We can use the following command to make empty files:

  • cat: This command is used to concatenate files and display them on stdout.

Syntax:

Example: We can use the following command to make a file along with entered content:

  • copy: This command is used to copy files.

Syntax:

Examples: We can use the following command to copy the contents of text1 and text2, and the contents of text1 are retained:

  • mv: This command is used to rename files or move files.

Syntax:

Examples: We can use the following command to make empty files known as text1 and text2:

  • rm: This command is used to remove directories and files.

Syntax:

Example: We can use the following command to delete text1:

  • mkdir: It creates a directory.

Syntax:

Example: We can use the following command to make a directory:

  • mkdir: It removes a directory.

Syntax:

Example: We can use the following command to make empty files:

  • cd: It changes the directory.

Syntax:

Example: We can use the following command to change our working directory:

  • pwd: It prints the current working directory.

Syntax:

Example: We can use the following command to print "direct1" if our current working directory is "direct1":

What is the Unix process?

A program runs in a process. Each time when a program or command is run, a fresh process is established. The process is running for as long as the command is in an active state. For instance, if we are running the command, i.e., cat, the cat process is generated.

The kernel assigns a special identification number known as the PID or process identification number, which ranges between 0 to 32,767 every time a fresh process is established. Other process properties include their GID (group related to the process), UIS (user id owns the process), TTY (controlling terminal through where they're launched), and PPID (the parent PID).

A process has a hierarchical relationship in Unix, where a parent process generates the child process. The process, i.e., init, is the grandfathering process of every other process. In a few cases, the child is known as an orphan process.

Process Types in Unix

  • Foreground Process: A foreground process is launched through a terminal and denies further commands until it ends. By default, the stdout and stdin are associated with the terminal in such a process.
  • Background Process: This process is launched through a terminal but is executed in the background. Hence, permitting further commands while it executes. The stdout and stdin should be typically redirected, so they do not interfere with another foreground process.
  • Daemon Process: It's a process that's not related to the terminal session. Usually, such processes are released for system services like printing and networking.

Control Commands

The control commands are a two-key combination in which a letter is simultaneously pressed using the 'Ctrl' key.

  • Control-Z: It suspends the currently active foreground process to the background.
  • Control-D: It removes the currently active terminal or login session.
  • Control-C: It removes the currently active foreground process.

Other commands:

  • ps: It shows a snapshot of every current process.

Syntax:

Example,

The above command will display all running processes formatted as a table.

  • top: It shows a live current process status.

Syntax:

Example,

The above command will display a live view of every current process.

  • bg: It regains a background suspended a job.

Syntax:

Example,

The above command will continue executing a job that was suspended in the background previously.

  • fg: It brings a job to the foreground from the background.

Syntax:

Example,

The above command will bring a previous job to the foreground from the background.

  • clear: It clears the terminal screen.

Syntax:

Example,

The above command will clear every prior text through the terminal window.

  • history: It prints the command history in the current active session.

Syntax:

Example,

The above command will display the list of old commands that were enrolled.

  • ls: It lists the contents of the directories.

Syntax:

Example,

The above command will list every content of a directory in a long way sorted by time.

  • which: It locates the commands.

Syntax:

Example,

The above command will list every path from where 'cat' can execute.

  • man: It is an interface to work with online reference manuals.

Syntax:

Example,

The above command will display the manual page for 'cat'.

  • su: It modifies user-id and becomes a super or root user.

Syntax:

Example,

The above command will modify the specified user-id.

  • sudo: It runs the command as other super-users or users.

Syntax:

Example,

The above command will get the file listing of the unlisted directory.

  • find: This command is used to find directories and files.

Syntax:

Example,

The above command will search for character device files of the 'text' name in the '/usr' folder.

  • du: It estimates the disk usage blockage.

Syntax:

Example,

The above command will display the total blocks utilized by files within the current directory.

  • df: This command displays the total free blocks for a mounted file system.

Syntax:

Example,

The above command will show the total free blocks inside the local file systems.