Useful Basic Linux/Unix Commands :
This article will provide you some basic Linux/Unix commands that will help us to deal with the one of popular operating system i.e. Linux.
The below are the most basic commands in Linux/Unix OS:
1: ls
The ls command lists all files and directories within the current working directory. We can also see all the hidden files by using the command ls -a.

# ls command will display all files and directories in current directory
$ ls
Applications		Movies
Desktop				Music
Documents			Pictures
Downloads			Public
Library

#Display filesize in human readable format (e.g. KB, MB etc.,)
$ls -lh
total 697184
drwx------@  4 rahul  staff   136B Aug 12 10:11 Applications
drwx------+ 10 rahul  staff   340B Oct  4 13:04 Desktop
drwx------+  7 rahul  staff   238B Sep 26 16:23 Documents
drwx------+  5 rahul  staff   170B Sep 27 20:08 Downloads
drwx------@ 61 rahul  staff   2.0K Sep 26 17:33 Library
drwx------+  3 rahul  staff   102B Jun  8 19:26 Movies
drwx------+  4 rahul  staff   136B Jun  9 20:47 Music
drwx------+  3 rahul  staff   102B Jun  8 19:26 Pictures
drwxr-xr-x+  5 rahul  staff   170B Jun  8 19:26 Public

#Order Files Based on Last Modified Time (In Reverse Order) by using ls -lrth
$ls -lrth
total 697184
drwxr-xr-x+  5 rahul  staff   170B Jun  8 19:26 Public
drwx------+  3 rahul  staff   102B Jun  8 19:26 Pictures
drwx------+  3 rahul  staff   102B Jun  8 19:26 Movies
drwx------+  4 rahul  staff   136B Jun  9 20:47 Music
drwx------@  4 rahul  staff   136B Aug 12 10:11 Applications
drwx------+  7 rahul  staff   238B Sep 26 16:23 Documents
drwx------@ 61 rahul  staff   2.0K Sep 26 17:33 Library
drwx------+  5 rahul  staff   170B Sep 27 20:08 Downloads
drwx------+ 10 rahul  staff   340B Oct  4 13:04 Desktop

#To list hidden files as well in directory use ls -alrth
$ls -alrth
total 697288
drwxr-xr-x+  5 rahul  staff   170B Jun  8 19:26 Public
drwx------+  3 rahul  staff   102B Jun  8 19:26 Pictures
drwx------+  3 rahul  staff   102B Jun  8 19:26 Movies
-r--------   1 rahul  staff     7B Jun  8 19:26 .CFUserTextEncoding
drwxr-xr-x   6 root   admin   204B Jun  8 19:30 ..
drwx------   3 rahul  staff   102B Jun  9 08:01 .ssh
drwx------+  4 rahul  staff   136B Jun  9 20:47 Music
drwxr-xr-x   4 rahul  staff   136B Jun 10 12:29 .composer
-rw-r--r--   1 rahul  staff    58B Jun 11 22:46 .gitconfig
-rw-------   1 rahul  staff     0B Jun 21 19:37 .node_repl_history
drwx------   3 rahul  staff   102B Jun 21 19:38 .config
drwxr-xr-x   7 rahul  staff   238B Jun 22 09:12 .npm
-rw-r--r--   1 root   staff   116B Jun 27 22:53 .pearrc
drwxr-xr-x   6 rahul  staff   204B Jul  2 19:43 .subversion
-rw-------   1 rahul  staff   8.6K Jul  2 22:35 .viminfo
drwx------@  4 rahul  staff   136B Aug 12 10:11 Applications
drwx------+  7 rahul  staff   238B Sep 26 16:23 Documents
drwx------@ 61 rahul  staff   2.0K Sep 26 17:33 Library
-rw-r--r--@  1 rahul  staff    12K Sep 27 11:23 .DS_Store
drwx------+  5 rahul  staff   170B Sep 27 20:08 Downloads
-rw-------   1 rahul  staff   8.3K Oct  4 12:07 .bash_history
drwx------+ 10 rahul  staff   340B Oct  4 13:04 Desktop
drwx------   2 rahul  staff    68B Oct  4 19:26 .Trash
drwxr-xr-x+ 26 rahul  staff   884B Oct  4 19:26 .
drwx------  32 rahul  staff   1.1K Oct  4 21:22 .bash_sessions

                            
2: pwd
The pwd (print working directory) command displays the name of the current working directory. So, to know which directory you are in, we can use the pwd command. It gives us the Absolute Path, which means the path that starts from the root. The root is the base of the Linux filesystem.

#display absolute path of current working directory                        
$ pwd
/Users/rahul/Desktop

                            
3: cd
The cd command - change directory - will allow the us to change between directories. We use cd command to navigate between two different directories. For example, if you are in home directory, and you want to navigate to the Movies folder, then you can type in cd Movies.

#Use cd to switch between directories
$ cd Movies

                            
4: mkdir
The mkdir command is used to create a new folder or a directory.

#To create new directory
$ mkdir MyFolder

#To create new directory with name having space i.e. "My Folder"
$ mkdir My\ Folder

#Or To create new directory with name having space i.e. "My Folder"
$ mkdir "My Folder"

#To create multiple directories in one shot
$ mkdir MyFolder1 MyFolder2 MyFolder3

                            
5: rm
The rm command is used to delete files and directories. rm cannot simply delete a directory. rm -rf is used to delete a directory recursively. In this case, it deletes both the folder and the files in it.

#To delete file in Linux
$ rm index.php

#To delete directory containing files and folders
$ rm -r MyFolder

#To delete directory containing files and folders forcefully
$ rm -rf MyFolder


                            
6: touch
The touch command is used to create a empty file. It can be anything, from an empty txt file to an empty zip file.

#To create empty files
$ touch index.php

#To create multiple empty files
$ touch index.php home.php login.php

                            
7: cp
The cp command is used to copy files/folders via command line. It takes two parameters, the first one is location of the file to be copied and the second is where to copy.

#To make a copy of files
$ cp /home/htdocs/codephponline/index.php /home/Desktop/

#To make a copy of directory
$ cp -r /home/htdocs/codephponline /home/Desktop/

                            
8: mv
The mv command is used to mv files/folders via command line. We can also use mv command to rename files/foldes.

#To move files/folders from one location to other
$ mv /home/htdocs/codephponline/index.php /home/Desktop/

#To rename file
$ mv /home/htdocs/codephponline/index.php /home/htdocs/codephponline/main.php


                            
9: tar
The tar command is used to create, maintain, modify, and extract files that are archived in the tar format. tar stands for tape archive. It is an archiving file format.

#Create tar Archive File
$ tar -cvf codephponline.tar /home/codephponline

#Create tar.gz Archive File
$ tar -cvzf codephponline.tar.gz /home/codephponline

#Untar tar Archive File
$ tar -xvf codephponline.tar

#Uncompress tar.gz Archive File
$ tar -xvf codephponline.tar.gz

#List Content of tar Archive File
$ tar -tvf codephponline.tar

#List Content tar.gz Archive File
$ tar -tvf codephponline.tar.gz


                            
10: locate
The locate command is used to locate a file in a Linux OS, just like the search command in Windows. This command is very useful when you don’t know where a file is saved or the actual name of the file.

#To search file named ReadMe.txt
$ locate ReadMe.txt

#To search all file having extension .txt
$ locate *.txt

                            
11: clear
clear command is used to clear the terminal if it gets filled up with too many commands.

#wipes the linux terminal clean
$ clear

                            
About Me
Rahul Yadav

Rahul Yadav

Technical Architect

Kuala Lumpur, Malaysia

Connect Me:     

I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

Tag Cloud