10 useful Linux Commands

10 useful Linux Commands

Linux servers are nothing without commands. Commands makes Linux more powerful. The user inputs a command to linux machine through a shell program called terminal and it shows the output of it. Linux commands helps the server admin to troubleshoot issues in a faster and easier way. Here we are going to discuss about 10 useful Linux Commands and it options that helps server admin to do his job more easier.

#Vim Command :

Vim is a text file editing application used in linux. We use vim command in terminal (#vim <filename>). while we use the command with filename, it opens up the file if it already exists or else it will create a new file with the given filename. There are two modes in vim. One is command mode and other is the insert mode. In command mode, user can use commands as editing shortcuts to delete,copy,paste,move,undo lines and in insert mode user can insert text.

Command Mode:

In command mode, user can use commands as editing shortcuts to delete,copy,paste,move,undo lines and lots of more options.

Example :

h – Moves the cursor one character to the left

l  – Moves the cursor one character to the right

K – Moves the cursor up one line

J – Moves the cursor down one line

CTRL + F – scrolls down fullscreen

CTRL + B – scrolls up fullscreen

CTRL + D – scrolls down fullscreen

CTRL + U – scrolls up fullscreen

w – Forward one word

b – Backward one word

x – Delete character

dw- Delete word from the cursor

db – Delete word backward from the cursor

dd- Delete line the current line

yy- Copy the current line

p- Paste below cursor

P- Paste above cursor

u- To undo last action

CTRL + R- To redo last action

:q! – To quit without saving

:wq! – To save and quit


Insert mode :

a- To insert text to following current cursor position

A- To insert text to the end of current line

i- To Insert text before the current cursor position

I- To Insert text at the beginning of the cursor line

There are lot more commands in vim that we can use. Above listed  are the commonly used commands in vim.  

#apt-get Command

apt stands for Advanced Package Tool. APT performs such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.

#apt-get install <packagename> – To install a package.

#apt-get remove <packagename> – To remove a package.

#apt-get update – It updates the software repositories in /etc/apt/sources.list and /etc/apt/sources.list.d
directory.

#apt-get upgrade – It updates the system after the updates.

#awk Command

Awk command is used to search a word pattern from a document. It searches one or more files to see if they contain lines that matches with the specified patterns and then performs the associated actions.

# awk ‘//{print}’/etc/hosts – It prints all the lines in the file /etc/hosts since no pattern is given.

# awk ‘/localhost/{print}’ /etc/hosts  -It will match line having localhost in the  /etc/hosts file.

# awk ‘/l.c/{print}’ /etc/hosts – The (.) will match strings containing loc, localhost, localnet.

# awk ‘/l*c/{print}’ /etc/localhost – It will match strings containing localhost, localnet, lines, capable.

# awk ‘/[al1]/{print}’ /etc/hosts – It will match all strings containing character a or l or 1 in a line in
the file /etc/hosts.

# awk ‘/[Kk]T/{print}’ /etc/hosts – It matches strings starting with either K or k followed by T:

# awk ‘/[0-9]/{print}’ /etc/hosts – All the line from the file /etc/hosts contain at least a single number [0-9] .

# awk ‘/^fe/{print}’ /etc/hosts – It matches all the lines that start with the pattern provided .

# awk ‘/ab$/{print}’ /etc/hosts – It matches all the lines that end with the pattern provided.

Regular expressions are made of:

Ordinary characters such as space, underscore(_), A-Z, a-z, 0-9.

Meta characters that are expanded to ordinary characters, they include:

(.) it matches any single character except a newline.

(*) it matches zero or more existences of the immediate character preceding it.

[ character(s) ] it matches any one of the characters specified in character(s), one can also use a hyphen (-) to mean a range of characters such as [a-f], [1-5], and
so on.

^ it matches the beginning of a line in a file.

$ matches the end of line in a file.

\ it is an escape character.

[0-9] means a single number

[a-z] means match a single lower case letter

[A-Z] means match a single upper case letter

[a-zA-Z] means match a single letter

[a-zA-Z 0-9] means match a single letter or number

#chattr Command

(Change Attribute) is used to set/unset certain attributes to a file in Linux system to secure accidental deletion or modification of important files and folders. Even the root user cannot delete or modify file/folder once attributes are sets with chattr command.

Syntax of chattr:

#chattr [operator] [flags] [filename]

Following are the list of common attributes and associated flags can be set/unset
using the chattr command.

*If a file is modified with ‘S‘ attribute set, the changes are updates synchronously
on the disk.

*A file is set with ‘a‘ attribute, can only be open in append mode for writing.

*A file is set with ‘i‘ attribute, cannot be modified (immutable). Means no renaming,
no symbolic link creation, no execution, no writable, only superuser can unset the
attribute.

*A file with the ‘j‘ attribute is set, all of its information updated to the ext3
journal before being updated to the file itself.

*A file is set with ‘t‘ attribute, no tail-merging.
*A file with the attribute ‘d‘, will no more candidate for backup when the dump
process is run.

*When a file has ‘u‘ attribute is deleted, its data are saved. This enables the user
to ask for its undeletion.

+ : Adds the attribute to the existing attribute of the files.

– : Removes the attribute to the existing attribute of the files.

= : Keep the existing attributes that the files have.


#chattr +i nixtree.txt – To add attributes on files to secure from deletion.The immutable
bit +i can only be set by superuser (i.e root) user or a user with sudo privileges can able to set.

#lsattr – To check the attributes.

#chattr -i nixtree.txt – To unset attribute on Files.

#chattr +a nixtree.txt – Append data without Modifying existing data on a File.

#chattr -R +i myfolder – To Secure Directories.To secure entire directory and its files,
we use ‘-R‘ (recursively) switch with ‘+i‘ flag along with full path of the folder.

#chattr -R -i myfolder – To unset permission, we use same ‘-R’ (recursively) switch with
‘-i’ flag along with full path of the folder.


#cp Command

cp command in linux is used to copy files and directories.

#cp <filename> <destination>

options:

#cp -f – forcefully copy by removing the destination file if needed.

#cp -i – ask before overwrite.

#cp -n – no file overwrite.

#cp -v – shows the file that are being copied.

 

#dig Command

Dig stands for (Domain Information Groper). It is a network administration command-line
tool for querying Domain Name System (DNS) name servers. It is useful for checking DNS problems and also to perform DNS lookups and displays the answers that are returned from the name servers.

#dig nixtree.com – Query Domain “A” Record. It look up the “A” record for the domain name
nixtree.com. Dig command reads the /etc/resolv.conf file and querying the DNS servers
listed there. The response from the DNS server is what dig displays.

#dig nixtree.com +short – The +short option will drastically cut the output.Displays short
information.

#dig nixtree.com MX – Querying MX Record for Domain.

#dig nixtree.com SOA – Querying SOA Record for Domain.

#dig nixtree.com TTL – Querying TTL Record for Domain.

#dig nixtree.com +nocomments +noquestion +noauthority +noadditional +nostats – Querying only answer section.

#dig nixtree.com ANY +noall +answer – Querying ALL DNS Records Types

#dig -x 198.235.134.108 +short – DNS Reverse Look-up.

#dig nixtree.com mx +noall +answer redhat.com ns +noall +answer – Querying Multiple DNS Records.


#dmesg Command


The ‘dmesg‘ command displays the messages from the kernel ring buffer like system architecture, cpu, attached device, RAM etc. When computer boots up, a kernel is loaded into memory. During that period number of messages are being displayed where we can see hardware devices detected by kernel.

#dmesg | more – List all loaded Drivers in Kernel.

#dmesg | grep sda – List all Detected Devices.

#dmesg | head  -20 – Print Only First 20 Lines of Output.

#dmesg | tail -20 – Print Only Last 20 Lines of Output.

#dmesg | grep -i usb – Search Detected Device or Particular String.The ‘-i’ option instruct to grep command to ignore the upper or lower case letters.

#dmesg -c – Clear dmesg Buffer Logs.

#watch “dmesg | tail -20” – Monitoring dmesg in Real Time.


#ls Command

ls command is used to list all the files and directories in the current working directory.
It is one of the most frequently used commands in linux. When we use ls command without any options,it will list all the directories and files without any details.By using different types of options in ls command we can list the details in different types.

#ls -a – It lists all files,also includes files that starts with “.”

#ls -l <filename> – it will provide the permissions for the user,group and others. And also shows owner of the
file,group belongs to, date and time.

#ls -ld <directory> –  it will provide the permissions for the user,group and others. And also shows owner of the
directory,group belongs to, date and time.

#ls -author – list the author of all files,size,date

#ls –sort=size – it sort the list according to the size

#ls –sort=time – it sort the list according to the time

#ls –sort=extension – it sort the list according to the extension

#ls -i – list files with inode number

#ls -r – sort in reverse order

#ls -sh – list with size in human readable format

#ls -S – sort by file size

#ls -t – sort by time

#Find Command 

Find command can be used to find files and directories. We can search by file, folder,
name, creation date, modification date, owner and permissions. Find is a very useful
command which will reduce our work.It has so many options can be done in a single command.

options :

-name : to search using name

-type : to search by type (file/directory)

-perm : to search by file/directory permissions.

-exec : to execute a operation to a file/directory.

-user : to search by user.

-group : to search by group.

-mtime : search by last modified day

-atime : search by last access day

-ctime : search by last changed day

-amin : search by last access time

-mmin : search by last modified time

-cmin : search by last changed time

-size : search by size.


examples:


# find . -name nixtree – Find all the files whose name is nixtree in a current working directory.

# find /home -name nixtree – Find all the files under /home directory with name nixtree.

# find /home -iname nixtree – Find all the files whose name is nixtree and contains both capital
and small letters in /home directory.

# find / -type d -name nixtree – Find all directories whose name is nixtree in / directory.

# find . -type f -name nixtree.php – Find all php files whose name is nixtree.php in a current working directory.

# find . -type f -name “*.php” – Find all php files in a directory.

# find . -type f -perm 0777 -print – Find all the files whose permissions are 777.

# find / -type f ! -perm 777 – Find all the files without permission 777.

# find / -perm 2644 – Find all the SGID bit files whose permissions set to 644.

# find / -perm 1551 – Find all the Sticky Bit set files whose permission are 551.

# find / -perm /u=s – Find all SUID set files.

# find / -perm /g=s – Find all SGID set files.

# find / -perm /u=r – Find all Read Only files.

# find / -perm /a=x – Find all Executable files.

# find / -type f -perm 0777 -print -exec chmod 644 {} \; – Find all 777 permission files and use
chmod command to set permissions to 644.

# find / -type d -perm 777 -print -exec chmod 755 {} \; – Find all 777 permission directories and
use chmod command to set permissions to 755.

# find . -type f -name “nixtree” -exec rm -f {} \; – To find a single file called “nixtree” and remove it.

# find . -type f -name “*.txt” -exec rm -f {} \; – To find and remove multiple file with .txt

# find /tmp -type f -empty – To find all empty files under path /tmp.

# find /tmp -type d -empty – To file all empty directories under path /tmp.

# find /tmp -type f -name “.*” – To find all hidden files.

# find / -user root -name nixtree.txt – To find all or single file called nixtree.txt under / root directory of owner root.

# find /home -user nixtree – To find all files that belongs to user nixtree under /home directory.

# find /home -group developer – To find all files that belongs to group Developer under /home directory.

# find /home -user nixtree -iname “*.txt” – To find all .txt files of user nixtree under /home directory.

# find / -mtime 50 – To find all the files which are modified 50 days back.

# find / -atime 50 – To find all the files which are accessed 50 days back.

# find / -mtime +50 –mtime -100 – To find all the files which are modified more than 50 days back and less than 100 days.

# find / -cmin -60 – To find all the files which are changed in last 1 hour.

# find / -mmin -60 – To find all the files which are modified in last 1 hour.

# find / -amin -60 – To find all the files which are accessed in last 1 hour.

# find / -size 50M – To find all 50MB files.

# find / -size +50M -size -100M – To find all the files which are greater than 50MB and less than 100MB.

# find / -size +100M -exec rm -rf {} \; – To find all 100MB files and delete them using one single command.

# find / -type f -name *.mp3 -size +10M -exec rm {} \; – Find all .mp3 files with more than 10MB and delete them using one single command.

 

#sed Command

Sed (stream editor) command helps us to modify each line of a file or stream by replacing specified parts of the line.

Syntax : sed OPTIONS… [SCRIPT] [INPUT FILE…]

# sed -n ‘7,15p’ nixtree.txt – Return lines 7 through 15 from nixtree.txt.

# sed ‘10,47d’ nixtree.txt – Exclude lines 10 through 47 from nixtree.txt.

# sed -n -e ‘6,9p’ -e ‘20,25p’ nixtree.txt – Display lines 6-9 and 20-25 from nixtree.txt.

# sed ‘s/unix/linux/’ nixtree.txt – It replaces the word “unix” with “linux” in the file.

# sed ‘s/unix/linux/2’ nixtree.txt – It replaces the second occurrence of the word “unix” with “linux” in a line.

# sed ‘s/unix/linux/g’ nixtree.txt – It replaces all the occurrences of the string in the line.

# sed -n ‘s/unix/linux/p’ nixtree.txt – Printing only the replaced lines.

# sed ‘$d’ nixtree.txt – To Delete a last line.

# sed -n ‘/^Jul  1/ p’ /var/log/secure – Viewing lines containing with a given pattern.

# sed G nixtree.txt – Insert blank line between all lines on the output.

# sed ‘G;G’ nixtree.txt – Insert two blank line between all lines on the output.

Facebook Comments