Basic UNIX Commands Everyone Should Know

No matter whether you are working on a stand-alone project or a web project, knowledge of operating systems and networking is must for testers and developers. For UNIX beginners, learning basic UNIX commands is a good start. This article will explaina  few of the basic UNIX commands. The best way to lear the following commands is to read and simultaneously practice them on a UNIX operating system.

What is UNIX?

UNIX is an operating system developed in the 1960s and has been under constant development ever since. Operating system is a suite of programs which makes our computer work properly.

With the help of a graphical user interface (GUI) similar to windows, UNIX systems also provides an easy to use environment. However, knowledge of UNIX is required for operations. UNIX has been a popular OS because of its multi-user, stability, multi-tasking environment and a very powerful networking capabilities.

Here, I am using Fedora 22. Below is the user login screen for the same.

 

Types of UNIX

There are many different versions of UNIX which share common similarities. The most popular types of UNIX OS are Sun Solaris, Linux.

Features of UNIX

  • Multi-user: More than one user can use the machine at a time supported via terminals
  • Multitasking: More than one program can be run at a time.
  • UNIX is highly portable OS which can be ported to a variety of hardware platforms.
  • Hierarchical structure of directory to support the organization and maintenance of files.

Commands

  • ls
  • cd
  • pwd
  • mkdir
  • rmdir
  • cp
  • echo
  • cat
  • who
  • du

ls command

ls commandis  used to see the list of files on our UNIX system. It will show the files/directories in your current directory, where directories are denoted in blue color and files are denoted in white color. You will find similar color schemes in different flavors of Linux.

Syntax of this command is to use ‘ls’ without single quotes.


cd command

cd command is used to change directory. It changes the current directory to the directory specified as an argument.

Syntax: cd abhi


pwd command

pwd command is used to find out your current directory where pwd stand for present working directory.

Syntax is: pwd


mkdir command

Like in DOS, directories can be created with mkdir command. This command will create a directory or a subdirectory in your present working directory. The command is followed by the name of the directories.

Syntax: mkdir directory_name


Below is the screenshot where ‘csharp’ folder is created.


We can also create a number of subdirectories with one mkdir command.

Syntax: mkdir directory1 directory2 directory3


Here is the screenshot where multiple directories are created:


We can create a chain of directories using a single command of mkdir.

Syntax: mkdir directory1 directory1/directory2 directory1/directory3


Here is the screenshot where directories chain is created in ‘csharp’ folder:


rmdir command

This command is used to remove an empty directory on a Unix-system.

Syntax: rmdir directory_name


Here is the screenshot where dir2 of ‘csharp’ folder is removed:


rmdir can also delete more than one directory in one shot. A reverse logic has to be applied on deleting a directory and sub directories.

Syntax: rmdir directory/subdirectory directory


Here is the screenshot after deleting multiple directories:


cp command

The cp (copy) command is used to copy a file from one place to another. It creates an exact image of the file on the disk. The original file remains unchanged and the new file may have the same or a different name. The syntax requires two filenames as an argument in the command.

Syntax: cp source_file destination_file


Before copy command:


If the destination_file doesn’t exist, it will first be created before copying takes place, else it will be overwritten without any warning.


cp can also be used to copy more than one file in a single invocation of the command.

Syntax: cp file1 file2 file3 folder // will copy all three files, file 1 file 2 and file 3 in a directory named as folder


Below is the screenshot where files, test and testnew are copied in ‘abhi’ folder:


echo command

This is a command in UNIX that places a string on the terminal which is typically used in shell scripts and batch programs to output status text to a screen or a file.

Syntax: echo ‘This is a test string’


cat command

cat is the most well-known command of UNIX used to display the contents of a small file on a terminal.

Syntax: cat filename


cat command also supports more than one filename as an argument.

Syntax: cat file1 file2

The contents of the second file are shown immediately after the first file without any header information.


cat command is also useful for creating a file. To create a file enter the command cat followed by ‘>’ without quotes followed by the filename.

Syntax:

  1. $localhost: cat > newfilename //press enter  
  2. Output goes to the filename following it  
  3. <ctrl-d> //to return back to terminal after saving the file  
  4. $localhost:  

who command

who command is used to display the information about all users who are currently logged in on the system. This command is useful to find out the following information:

  • login name
  • terminal name
  • login time
  • remote hostname

Syntax: who


du command

du command stand for disk usage which is a standard unix/linux command, used to estimate and display the disk space used by files on a machine. The du command also displays the files and directory size in a recursively manner.

Syntax: du


Summary

Below are the commands we have learned in this article:

CommandDescription
lsLists all files and directories in the present working directory
cdTo change your current directory
pwdTo display present working directory
mkdirCreates a new directory in the present working directory
rmdirDeletes a directory
cpTo copy and paste a source file to destination file
echoTo display a text on a terminal
catTo display file contents or to create a new file
whoTo display currently logged in users on the system
duTo display dis
 
These were some of the basic commands of Unix which every developer and tester should know. These were just the basic commands without passing any further command options. We will learn them one by one in my upcoming articles.

I hope you liked this article. Thanks for reading. Your valuable comments and queries are most welcome.

Read more articles on UNIX:

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now