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

[[email protected] workfolder]$ ls
file1.txt file2.txt file3.txt

 

ls -lrt

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

Example

[[email protected] 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
[[email protected] 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

[[email protected] 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

[[email protected] ~]$ cd workfolder/

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

 

pwd

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

Example

[[email protected] workfolder]$ pwd
/home/narayanatutorial/workfolder
[[email protected] 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

[[email protected] 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

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

 

rm

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

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

 

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.

[[email protected] ~] echo "Hello World"
Hello World

[[email protected] ~] 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

[[email protected] ~] 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.
[email protected] /cygdrive/d/narayanatutorial/workfolder

 

cp

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

[[email protected] ~] cp file1.txt ../workfolder2/

[[email protected] ~] cd ../workfolder2

[[email protected] ~] 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

[[email protected] ~] ls
file1.txt file2.txt file3.txt file4.txt

[[email protected] ~] mv file4.txt ../workfolder2/

[[email protected] ~] cd ../workfolder2

[[email protected] ~] ls
file1.txt file4.txt

Rename file

[[email protected] ~] ls
file1.txt  file4.txt

[[email protected] ~] mv file4.txt test.txt

[[email protected] ~] ls
file1.txt  test.txt

ps -ef

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

[[email protected] ~] 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

[[email protected] ~] ls
file1.txt  test.txt

[[email protected] ~] 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

 

[[email protected] ~] ls
file1.txt test.txt

[[email protected] ~] 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