Linux commands commonly used in DevOps

Linux commands commonly used in DevOps

A list of Linux commands commonly used in DevOps along with detailed descriptions for each command. These commands are essential for managing and maintaining systems, automating tasks, and deploying applications in a DevOps environment. Let's get started:

Folders / Directory

mkdir <folder name> : Create single folder

mkdir <folder name> <folder name> : Create multiple folders

mkdir -p <folder 1>/<folder 2>/<folder 3> : Create nested folders

mkdir <folder name>{starting-range..ending-range}

ex. mkdir day-{1..5} : Create multiple numbers of folders at once

cd <folder name> : Change directory

cd ~ : [username] goes to another user’s home directory.

cd .. : moves one directory up.

cd- : move to your previous directory.

pwd : Check present working directory (current working folder)

Files

touch <file name> : Create single file

cat > <file name> : Create single file

touch <file name> <file name> : Create multiple files

touch <file name>{starting-range..ending-range} :Create multiple numbers of files at once

ex. touch day-{1..5}

cat <file name> : View what’s in the file

cat filename1 filename2 > filename3 : Merge 2 files and store data in one

diff [options] <file 1> <file 2>:Find the difference between two files

options:

-c displays the difference between two files in a context form.

-u displays the output without redundant information.

-i make the diff command case in sensitive

List out Files & Folders

ls : List out folders and files

ls -la : List out all the files and folders including hidden with file permissions

ls -ltr : List out all the files and folders with file permissions

ls -R : Lists all the files in the subdirectories

Print on screen

echo “<to be print>” : Print

Date and Time

date : Current date and time

Clear Screen

clear : Clear the screen

ctrl + L (windows): Make current line at the top of the screen without removing data

cmd + L (mac)

Copy and Paste

cp <option> <source location> <destination location> : Copy file and paste

cp <option> <source-location>* <destination location>: Copy all data starting with specific word/character

cp -r <source location> <destination location>: Copy folder having data

Delete

rm <option> <source location> <destination location>: Remove the file and directory

options

-r for recursive (used for folders)

-v verbose

-f for forcefully

Move and Rename

mv <source location> <destination location> : Move the file or folder

mv <old name > <new name> : Rename the file or folder

history [options] : Commands you have run till now

options

-c clears the complete history list.

-d offset deletes the history entry at the OFFSET position.

-a appends history lines

Super User

sudo su : Be a Super User

sudo <command> : Run command with super user ability

User Management

sudo useradd <option> <username> : create a user account

option:

-m to make a directory for the user

sudo passwd <username> : Set password to a user account

su <username> : Switch User Account

exit : Logout from a user account

sudo userdel <username> : Delete user account

sudo usermod -l <new_name> <old_name> : Change user account name

whoami : Current username

Group Management

groupadd [name]: Add group account

grep [group name] /etc/group : Group property

grep [group name] /etc/group: Group property

grep [group name] /etc/gshadow :Group admin property

groupdel [groupname] : Delete group

gpasswd [options] [username] [groupname]:Add and Remove members

options

-a add single member

-M add multiple members

-d remove member

gpasswd -A [usename] [groupname]: make group admin

Edit file

vim <file name>: Vim editor

nano <file name>: Nano editor

Change File Permission

chmod <permission> <file name>: Change file permission

permission

u (Owner) - Permissions used for the owner of the file.

g (Group) - Permissions used by members of the group.

o (Other) - Permissions used by all other users.

r (read) – permit to read the file.

w (write) – permit to write the file.

x(execute) –permit to execute the file.

Change File Owner

chown [option] owner[:group] file(s): Change the file owner

Change Group Owner

sudo chgrp <group name> <file name>: change the group owner

Shell Scripting

#!/bin/bash - GNU Bourne-Again Shell :Identify interpreter (first line for script)

#!/bin/sh - The Bourne Shell

#!/bin/csh - The C Shell

#!/bin/ksh -The Korn Shell

read <variable name>: Read input value

./file.sh : Run file

bash file.sh:Run file

Package manager

sudo apt-get install <package Name> -y : Install package

sudo apt-get update : Update packages

sudo apt-get upgrade -y : Upgrade packages

Manage tools

sudo systemctl status <tool name>: Check status of tool

sudo systemctl start <toolname>: Start Tools

sudo systemctl stop <toolname>: Stop tools

sudo systemctl enable <toolname>: Auto enable tools

AWK (prints files data with programming)

awk [pattern] {action}: Print

Pattern:

$0 - entire line

$1, $2, $3 … - for each column (if exists)

NR - number of records (lines)

NF-number of fields(columns in the current line)

Action:

print

ex. awk '{print $0}' file.sh

Find (find directory)

find [option] [path] [expression] : Find the directory

option:

-name

-type

-user

find /temp -size -10M: Search the file with less than 10 MB

find /temp -size 10M: Search the file with 10 MB

find /temp -size +10M: Search the file with more than 10 MB

Grep (prints specific word in file)

grep [option] [expression] <file name> <file name> … : Find a word in a file(s)

options:

-i = intensive (case sensitive)

-r = recursively

-v = invert string match

-l = display the file names that match the string

-L = display the file names that do not contain the string

-n =match line with number

^=display the lines that start with a string

grep [expression] <source file> > <destination file>: Search and redirect output in a new file

Disk space

df [options] [file]: To display disk space usage

options:

-h Human readable

-m displays usage in MBs.

-k displays usage in KBs.

-T show the file system type in a new column

File Size

du -h [file path]: show file size in human-readable format

Show IP

ifconfig: Show ip address

ip addr: Show ip address

ip a: Show ip address

Services

systemctl [option] [service name]

Options:

status – display status

enable - permanently on

disable - permanently off

start - start the service

stop - to stop

restart - to restart

e.g : systemctlstatussshd

Remember, this is not an exhaustive list, but it covers many of the commonly used Linux commands in a DevOps context. Each command has various options and use cases,

I hope this helps to understand the Few basic Linux Commands