Linux Command
Linux for DevOps

Linux Command

Hi Folks, After working as a DevOps Engineer for almost 3 years and learning DevOps from Various sources, Concatenating my learnings in phases starting from Linux.

Learned DevOps-related methodologies from TrainWithShubham Imran Teli Technical Guftgu

Starting from the Basic Linux Commands :

cd to change directory.

ls to list all files and directories.

ls -l to long list the files and directories.

pwd to show the present working directory

whoami to provide user details

lsb_release -a to check the version details for Linux and next release

cat file1 : cat is the keyword to create File an File1 is the name of file

once we use cat command to create file we can update data for file in the next line and then can press ctrl+D to close.

example :

cat file1

Enter details of file 1

ctrl+d

file created.

cat helps us create non-editable files. Data can be appended.

tac is the command used to print data in reverse order.

This means lines printed from bottom to top.

cat file3 file4 >> file5 : used to concatenate file3 and file4 and save the response as file5.

Touch can also be used to create files. Touch command helps us create multiple empty files. Furthermore, we can use vi or vim editor to add data in the files created using the touch command.

Touch command helps us create multiple empty files. Helps us to change the timestamp and access time.

touch -a file7.txt can be used to change the access time for the file.

touch -m file7.txt can be used to change the modify time for the file.

touch -am file7.txt can be used to change both access and modify the time for the file.

stat file7.txt can be used to check the access time, modify time and birth time.

vi text editor :

:w to save content typed.

:q to quit the editor.

:wq to save and quit.

:q! to quit forcefully without saving the data.

clear command to clear the screen.

history command to get the history of commands used for the current session.

sudo su to swith user.

sudo su and sudo -i both can be used to switch root users. sudo su can used to switch to any other user keeping the original user's environment but configurations and environment will remain the same. But sudo -i will change configurations and environment as well. It will be similar to the one you log in and create a new session as well.

ls -a can be used to show all the hidden files created.

. with the file name can be used to create the hidden files.

mkdir can be used to create a directory.

example : mkdir firstdirectory

mkdir -p d1/d2/d3 can be used to create the nested directories.

cd can be used to change the directory

cd firstdirectory then cd .. can be used to return back to /

pwd can be used to provide the present working directory

useradd username to add user to /etc/passwd file

useradd kamalpreet to add kamalpreet as a user.

useradd -m kamalpreet to add a user with folder -m for making folder..

cat /etc/passwd to see the users added.

groupadd group1 to create group named group1

groupadd newgroup to create group names newgroup

cat /etc/group to see groups.

usermod -aG kannu1 newgroup command is used to add a user to the group. aG refers to append the group.

gpasswd -a gur mom (to add gur to group mom. -a for single user)

gpasswd -M kamal13,kamal14,kamal15 kamalgrp (to add multiple users to group at same time)

rm -r d1 (to remove the directory along with the child directories)

rmdir to remove the normal directory

hostname to provide the IP address

cat /etc/os-release to provide details related to the os.

ifconfig again to provide the IP address

echo command to print data on screen example :

echo "Anything you want to display"

echo "kamalpreet printed in file2" > file2 used to print output of echo command in file named file2

echo > file2 (will empty the file means will delete the data from a file if exists)

echo > "kamalpreet printed in file 13" > file13 (it will created file13 if do not exit earlier).

echo "Hello world" | tee -a file1313.txt (send data to a file named file1313 and print on screen as well).

grep command is used to search the keywords

grep root /etc/passwd (to find root in file passwd)

grep -i Root /etc/passwd (case insensitive)

grep -i file * (searches file case sensitively all over)

grep -iR file * (case insensitive recursively searched file all over)

ln -s file1 sftlink (to create softlink for file1)

if we delete the file1 then sftlink will be void or of no use.

ln file13 backup (to create hard link for file13. If we delete the file13 backup file will exist. therefore used to create a backup for the file)

tar -cvf tarfiletobecreated directorynameforwhichthefileneedtobecreated

gzip tarfilecreated (to create zip file for tar file)

gunzip tarfiletobecreated.gz (to unzip the tar file)

tar -cvf tr.tar firstdirectory (to create verbose forcefully for a directory named firstdirectory)

tar -xvf tr to untar the file.

du -sh filepath.tar (to show the size of the file in human-readable format.

tar -xvf filename.tar -C newlocation/filenameofextractedfile . example tar -xvf /mnt/backup.tar -C /root/desptop/

tar -cvzf finlename.tar.cz to create and archive tar file compressed in size.

tar -xvzf finlename.tar.cz to extract and archive tar file compressed in size.

chmod 700 filename to change mode of the file.

chown kamal file13 (to change the owner of file13 to kamal. and kamal should already be user created using useradd kamal).

groupadd kml

chgrp kml file13 (to change the group of file named file13 to kml from root)

sed 's/old_text/new_text/g' input_file (to replace the text pattern old to new in the file name input_file) example: sed 's/kamalpreet/kannu/g' file1

free -m to check memory details

wc filepath provide word count line count and character count in file

wc -l filepath is specifically used for number of lines.

ls | wc /etc/passwd first ls command is used to search files under root directory and then pipeline is created to count number of lines in file passwd

find /etc -name host* used to find the file under /etc folder named like host....

head -numberoflines /etc/passwd to display the number of lines from the head from /etc/passwd file.

tail -numberoflines /etc/passwd to display number of lines from tail from /etc/passwd file.

grep (global regular expression print)

grep kamal /etc/passwd to find kamal in /etc/passwd file

grep -i ubuntu /etc/passwd case insensitive search in passwd file

grep kamal /etc/passwd /etc/passwd1 to find the string in multiple files names passwd and passwd1

grep -r root / to search root in all the files recursively

grep -l kamal /etc/passwd /etc/shadow to display the file name that matches the string

grep -L kamal /etc/passwd /etc/shadow to display the file name that does not match the string

grep ^root filepath to display the line that starts with the string root

grep bin/bash$ etc/passwd to display line that ends with the string.

grep kamal /etc/passwd > mtnt.txt to search and redirect output to the new file.

grep TRACE logfile > tracefile.log to create a file named tracefile.log to store the trace related details from logfile

grep ERROR logfile > errorfile.log to create a file named errorfile.log to store the error related details from logfile

grep WARN logfile > warnfile.log to create a file named warnfile.log to store the warnrelated details from logfile

awk command used to provide columns from the file

awk '{print $1, $2, $5}' warnfile.log

awk '{print NR, $1, $2, $5}' warnfile.log NR for row number

awk '{print NR, $1, $2, $5}' | head warnfile.log NR is used for row number and print only 10 from top for column 1,2 and 5 for warnfile.log file.

awk 'NR>=10 && NR<=30 {print $1, $2, $5}' tracefile to print rows between 10 and 30 for the file named tracefile

awk /EVENT/ tracefile.log works similarly to the grep to find all the events from the log files.

awk 'NR>10 && NR<=30 && /INFO/ {print NR, $1, $2, $3}' application.log to sort rows 10 to 30 and print only info details with only 1,2,3 column.

awk '$2>"08:52:00" && $2<"08:54:00" {print $1,$2,$5}' application.log to sort based upon the time from column 2 of the log file named application.log .

When you use ls -ld followed by a directory path, it provides information about that directory, including its permissions, owner, group, modification time, and more

chown -R ansible:devops /opt/devopsdir to change owner recursively for devops group to ansible for all the directories and sub-directories under the path specified.

chmod o-x /opt/devopsdir to change mode o for others -x means remove execute permission for the directory specified.

chmod o-r /opt/devopsdir similar to the above command for root user

chmod g+w /opt/devopsdir add write permission for group.

su - ansible to switch user to the user created.

chown aws.devops /opt/webdata change owner to aws for DevOps group for the directory specified in path.

chmod 000 /opt/webdata, chmod 777 /opt/webdata, chmod 770 /opt/webdata

visudo is a command used in Linux to safely edit the sudoers file. The sudoers file, located at /etc/sudoers, determines which users or groups are allowed to execute commands with elevated privileges using the sudo command.

Editing the sudoers file directly can be risky, as a misconfiguration can lead to a loss of system security or functionality. Therefore, visudo is provided as a safe way to edit the sudoers file.

The curl command in Linux is a versatile tool for making network requests using various protocols, such as HTTP, HTTPS, FTP, and more. It is commonly used for downloading or uploading data from the command line.

curl -O https://example.com/somefile.txt to download the file from the URL.

curl -O newfile.txt https://example.com/somefile.txt to download the file with specified name from the URL

The -o option is useful when you want to control the local filename for downloaded content or when you want to ensure that the content is saved to a specific location.

wget is a command-line utility in Linux used for downloading files from the internet. It is a versatile tool for retrieving data from HTTP, HTTPS, FTP, and other web protocols.

wget https://example.com/somefile.zip

wget -O localfile.zip https://example.com/somefile.zip -o to save name to localfile for somefile

wget -c https://example.com/largefile.zip to resume download.

wget --limit-rate=100k https://example.com/largefile.zip limiting the download speed using the wget command

The rpm command in Linux is used for managing software packages in Red Hat-based systems, including CentOS and Fedora. RPM stands for "Red Hat Package Manager." It allows you to install, upgrade, query, verify, and remove packages on your system.

rpm -i package.rpm install package with the name provided.

rpm -U package.rpm upgrade the package.

rpm -q package_name to get information about the package name.

rpm -qa to list all the installed packages.

rpm -ql package_name to list files inside package.

rpm -e package_name to remove the installed package.

rpm -V package_name to verify package

rpm -ivh httpd-2.4.6-95.el7.centos.x86_64.rpm

rpm -ivh command is used to install an RPM package on a Red Hat-based Linux system while providing detailed information and verbose output during the installation process

-i: This option specifies that you want to install the RPM package.

-v: The -v option stands for "verbose" and provides detailed information about the installation process. It shows you each step of the installation.

-h: The -h option, also known as "hash," displays a hash (#) symbol to indicate the progress of the installation. It provides a visual indicator of how far the installation has progressed.

rpm --help for more details about the command in Linux.

systemctl status command is used in Linux to check the status and view detailed information about a specific service managed by systemd. In our case, we are using it to check the status of the Apache HTTP server, commonly known as "httpd" on many Linux systems.

ps -ef is used to show a list of currently running processes. E stands for everyone and f stands for full detailed output.

| is used to send the output of the first command to the next.

grep -v 'grep': This command with the -v option is used to filter out any lines that contain the text "grep" This is done to exclude the grep command itself from the results because it may also match the search criteria for the below command.

ps -ef | grep httpd | grep -v 'grep'

find command - > find / -name file1313 to find file under root directory with name file1313

search files on the basis of the suid user id) -- > find / -perm 4755 to find in the root directory with suid permission

find / -perm 2644 to find files with guide permission.

find / -user root search files on the basis of user.

find / -size -10M to find files with sizes less than 10 MB.

find / -size +10M to find files with sizes More than 10 MB.

find ~/ -name *.log to find any file with a .log extension.

find ~/ -user ubuntu to search files on the basis of the owner.

find ~/ -group DevOps to find files with group DevOps

wc command :

wc -l /etc/passwd (count number of lines)

wc -w filename (count the number of words in a file)

head command

head filename to display the upper 10 lines.

head -n 15 filename to display the number of lines from the top.

tail command

tail filename to display bottom 10 lines.

tail -n 15 filename to display the number of lines from the bottom.


some basic scripts :

first script.sh

#!/bin/bash

echo "First script created on $(date)"


first script is just used to create a file named first.sh and on executing that file we will get the message "first script created on the current date and time."

second script

#!/bin/bash

echo "Enter the name of the folder to be created"

read folder_name

mkdir $folder_name

echo "Folder created please check using ls command..."

Second.sh script is used to create a file which on executing will ask for the folder name from the user. On providing the name it will create a directory with the user-provided name.


third.sh

#!/bin/bash

echo "checking variables"

name=kamalpreet

echo "The name is $name"

echo $1, $2

third.sh script is used to check variables in linux.

fourth.sh

#!/bin/bash

src_dir=/home/ubuntu/scripts

tgt_dir=/home/ubuntu/backups

echo "Backup"

current_time=$(date "+%y-%m-%d-%H-%M)

echo $current_time

final_file=$tgt_dir/script-backup-$current_time.tgz

tar -czf $final_file -C $src_dir .

echo "Backup completed"

fourth.sh script is used to create backup files from the source file in Linux.







要查看或添加评论,请登录

Kamalpreet Singh的更多文章

  • Docker for DevOps

    Docker for DevOps

    Hi Folks, In this article, we will be learning about Docker from zero. This means we will be covering all the commands…

  • Docker Compose File

    Docker Compose File

    TrainWithShubham Imran Teli Bhupinder Rajput l ??????? ?????? l ??????? ?????? Hi Folks, In this article, we will learn…

  • Terraform with AWS provider

    Terraform with AWS provider

    Infrastructure as a code (IAAC) tool allows us to manage infrastructure with configuration files rather than GUI…

    5 条评论
  • Continuous Integration on AWS Cloud

    Continuous Integration on AWS Cloud

    Hi Folks, In the last article, we posted the Continuous Integration Project using Jenkins, Nexus, SonarQube, and Slack.…

  • Continuous Integration Using Jenkins, Nexus, SonarQube, Slack

    Continuous Integration Using Jenkins, Nexus, SonarQube, Slack

    Hi Folks, Created this continuous integration Project using Jenkins for continuous integration, Git as a Version…

  • Re-Architecting Web App using AWS Cloud

    Re-Architecting Web App using AWS Cloud

    Refactoring the AWS Lift and Shift project: https://www.linkedin.

  • AWS DevOps Lift and Shift Project

    AWS DevOps Lift and Shift Project

    AWS DevOps Project Hosting multi-tier web application stack on AWS cloud for production (Lift And Shift) AWS services…

  • Kubernetes Architecture

    Kubernetes Architecture

    Hi Folks, Kubernetes is known as the Container Orchestration Tool. In this article, I will be explaining K8S…

  • Docker for DevOps

    Docker for DevOps

    TrainWithShubham Imran Teli Technical Guftgu Docker Notes DevOps is the methodology used to reduce conflicts between…

    1 条评论
  • GIT for DevOps

    GIT for DevOps

    TrainWithShubham Imran Teli Technical Guftgu GIT (Distributed Version Control System) GIT was introduced by Linus…

    2 条评论

社区洞察

其他会员也浏览了