Most Important Basic Linux Commands

Most Important Basic Linux Commands

 

In this blog, I am sharing the most important basic Linux commands which are frequently using in our day to day work.

Here I have created four files (file1.txt, file2.txt, file3.txt, and .file4.txt) inside workfolder to show an example for the below commands.

 

ls

ls command is a basic command to see the list of files in the current directory

The ls command lists files and directories within the current working directory. Files listed in the grid format.

Example

[narayanatutorial@localhost workfolder]$ ls
file1.txt file2.txt file3.txt

 

ls -lrt

Files listed order based on Last Modified Time (In Reverse Order).

Example

[narayanatutorial@localhost workfolder]$ ls -ltr
total 0
-rw-rw-r--. 1 narayanatutorial narayanatutorial 0 Apr 16 15:25 file1.txt
-rw-rw-r--. 1 narayanatutorial narayanatutorial 0 Apr 16 15:37 file2.txt
-rw-rw-r--. 1 narayanatutorial narayanatutorial 0 Apr 16 15:43 file3.txt
[narayanatutorial@localhost workfolder]$

 

ls -a

By default ls command will not display hidden files that contain. (dot) in front of the file name. So by using this command, we can see hidden files. Here .file4.txt is a hidden file.

Example

[narayanatutorial@localhost workfolder]$ ls -a
. .. file1.txt file2.txt file3.txt .file4.txt

 

cd

The cd command changes the current directory in Linux and can toggle between directories conveniently. cd is similar to the CD and CHDIR commands in MS-DOS.

Example

[narayanatutorial@localhost ~]$ cd workfolder/

Here we can see the current directory path using pwd
[narayanatutorial@localhost workfolder]$ pwd
/home/narayanatutorial/workfolder
[narayanatutorial@localhost workfolder]$

 

pwd

The pwd (print working directory) command displays the name of the current working directory.

Example

[narayanatutorial@localhost workfolder]$ pwd
/home/narayanatutorial/workfolder
[narayanatutorial@localhost workfolder]$

 

clear

The clear command clears the screen which is similar to cls command in the MS-DOS.

 

man

If we don’t know options of the command or syntax of the command then no need to worry. we have documented all commands options and syntaxin. we can see more details of each command by using the man command.

The man is short for manual. The man allows a user to format and display the user manual built into Linux distributions which documents commands and other aspects of the system.

Example

[narayanatutorial@localhost workfolder]$ man ls

Output

LS(1) User Commands LS(1)

NAME
 ls - list directory contents

SYNOPSIS
 ls [OPTION]... [FILE]...

DESCRIPTION
 List information about the FILEs (the current directory by default).
 Sort entries alphabetically if none of -cftuvSUX nor --sort.

 Mandatory arguments to long options are mandatory for short options
 too.

 -a, --all
 do not ignore entries starting with .

 -A, --almost-all
 do not list implied . and ..

 --author
 with -l, print the author of each file
:

 

mkdir

Creates a new directory. Here we have created workfolder-2 folder inside /home/narayanatutorial

Example

[narayanatutorial@localhost ~]$ mkdir workfolder-2
[narayanatutorial@localhost ~]$ ls
Desktop Downloads Pictures Templates workfolder
Documents Music Public Videos workfolder-2
[narayanatutorial@localhost ~]$ pwd
/home/narayanatutorial

 

rm

Deletes the specified directory, provided it is already empty. Here we have deleted workfolder-2 inside /home/narayanatutorial

[narayanatutorial@localhost ~]$ rm -r workfolder-2/
[narayanatutorial@localhost ~]$ ls
Desktop Downloads Pictures Templates workfolder
Documents Music Public Videos
[narayanatutorial@localhost ~]$ pwd
/home/narayanatutorial
[narayanatutorial@localhost ~]$

 

echo

it’s a built-in command to display line of text/string that is passed as an argument. And also used to display environment variable value.

[narayanatutorial@localhost ~] echo "Hello World"
Hello World

[narayanatutorial@localhost ~] echo $PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/WINDOWS/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files/Intel/WiFi/bin:/cygdrive/c/Program Files/Common Files/Intel/WirelessCommon:/cygdrive/c/WINDOWS/System32/OpenSSH:/cygdrive/c/Program Files/Java/jdk1.8.0_231/bin:/cygdrive/d/apache-maven-3.6.1/bin:/cygdrive/c/Users/narayanaswamy.ragi/AppData/Local/Microsoft/WindowsApps:/cygdrive/c/Users/narayanaswamy.ragi/AppData/Local/Programs/Git/cmd

 

cat

cat command used to see the content of the text file

[narayanatutorial@localhost ~] cat file1.txt
Narayana Tutorial is a web developers site, with tutorials and references on programming languages such as Java, Servlet, Jsp, Struts, Hibernate, Spring etc.. and web development languages such as HTML, CSS, JavaScript, PHP, SQL, and JQuery, covering most aspects of web programming.
Narayana Tutorial has focus on simplicity, practicing easy and straight-forward learning.

We use simple code explanations, and illustrate how to use it.

The tutorials starts from basic level, and moves all the way up to complete professional references.
narayanaswamy.ragi@RBNS /cygdrive/d/narayanatutorial/workfolder

 

cp

cp command used to copy the file from one location to another location.

[narayanatutorial@localhost ~] cp file1.txt ../workfolder2/

[narayanatutorial@localhost ~] cd ../workfolder2

[narayanatutorial@localhost ~] ls
file1.txt

 

mv

mv command used to move file or directory from location to another location and also used to rename the file or folder.

Move file from one folder to another folder

[narayanatutorial@localhost ~] ls
file1.txt file2.txt file3.txt file4.txt

[narayanatutorial@localhost ~] mv file4.txt ../workfolder2/

[narayanatutorial@localhost ~] cd ../workfolder2

[narayanatutorial@localhost ~] ls
file1.txt file4.txt

Rename file

[narayanatutorial@localhost ~] ls
file1.txt  file4.txt

[narayanatutorial@localhost ~] mv file4.txt test.txt

[narayanatutorial@localhost ~] ls
file1.txt  test.txt

ps -ef

ps -ef command used to get a list of running processes in the system.

[narayanatutorial@localhost ~] ps -ef
UID PID PPID TTY STIME COMMAND
narayana 948 905 pty0 19:34:56 /usr/bin/ps
narayana 904 1 ? 08:37:54 /usr/bin/mintty
narayana 905 904 pty0 08:37:55 /usr/bin/bash

 

ps -ef|grep <process-name>

The command used to find the specific process list out of all.

Ex: ps -ef|grep java
    ps -ef|grep tomcat

The command fetch the running java process list

 

head

head command used to get the content of the file from the top with the specified number of lines

Syntax: head -<number of lines> <file-name>

Ex: head -2 file1.txt

[narayanatutorial@localhost ~] ls
file1.txt  test.txt

[narayanatutorial@localhost ~] head -2 file1.txt
Narayana Tutorial is a web developers site, with tutorials and references on programming languages such as Java, Servlet, Jsp, Struts, Hibernate, Spring etc.. and web development languages such as HTML, CSS, JavaScript, PHP, SQL, and JQuery, covering most aspects of web programming.
Narayana Tutorial has focus on simplicity, practicing easy and straight-forward learning.

 

tail

tail command used to get content of the file from the bottom with the specified number of lines. By default 10 number of lines, the content will be displayed if not mentioned number of lines in the command.

Syntax: tail -<number of lines> <file-name>

Ex: tail -2 file1.txt

 

[narayanatutorial@localhost ~] ls
file1.txt test.txt

[narayanatutorial@localhost ~] tail -2 file1.txt
We use simple code explanations, and illustrate how to use it.
The tutorials starts from basic level, and moves all the way up to complete professional references.

 

SCP (secure copy)

SCP is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows secure transferring of files in between the local host and the remote host or between two remote hosts.

Syntax

scp <filename> <remote-username>@<remote-host/ip>:<remote-folder-path>

Example

scp sample.txt [email protected]:/home/narayana

once you execute the above command, prompt for the password, then enter the remote user password, and then file copying will be started.

 

 

 

 

2 Comments

Add a Comment
  1. I have seen lot blogs and Information on othersites But in this Java Blog Information is very useful thanks for sharing it……..

Leave a Reply