Linux Commands for DevOps
(Git commands from 7th page)
In this section, we will have a look at the most frequently used Linux commands for DevOps that are
used while working in DevOps.
ls
This DevOps Linux commands lists all the contents in the current working directory.
syntax:
$ ls <flag>
Command Description
ls <path
By specifying the path after ls, the content in that path will be displayed
name>
Using ‘l’ flag, lists all the contents along with its owner settings, permissions & time
ls –l
stamp (long format)
ls –a Using ‘a’ flag, lists all the hidden contents in the specified directory
sudo
This command executes only that command with root/ superuser privileges.
syntax:
$ sudo <command>
Command Description
sudo useradd <username> Adding a new user
sudo passwd <username> Setting a password for the new user
sudo userdel <username> Deleting the user
sudo groupadd <groupname> Adding a new group
sudo groupdel <groupname> Deleting the group
sudo usermod -g <groupname> <username> Adding a user to a primary group
cat
This DevOps Linux commands can read, modify or concatenate text files. It also displays file contents.
syntax:
$ cat <flag> {filename}
Command Description
cat -b This adds line numbers to non-blank lines
cat -n This adds line numbers to all lines
cat -s This squeezes blank lines into one line
cat –E This shows $ at the end of line
grep
This Linux command for DevOps searches for a particular string/ word in a text file. This is similar to
“Ctrl+F” but executed via a CLI.
syntax:
$ grep <flag or element_to_search> {filename}
Command Description
grep -i Returns the results for case insensitive strings
grep -n Returns the matching strings along with their line number
grep -v Returns the result of lines not matching the search string
Returns the number of lines in which the results matched
grep -c
the search string
sort
This command sorts the results of a search either alphabetically or numerically. It also sorts files, file
contents, and directories.
syntax:
$ sort <flag> {filename}
Command Description
sort -r the flag returns the results in reverse order;
sort -f the flag does case insensitive sorting
sort -n the flag returns the results as per numerical order
tail
It is complementary to head command. The tail command, as the name implies, print the last N
number of data of the given input. By default, it prints the last 10 lines of the specified files. If you
give more than one filename, then data from each file precedes by its file name.
syntax:
tail [OPTION]... [FILE]...
tail -n 3 state.txt or tail -3 state.txt => -n for no. of lines
tail +25 state.txt
-c num: Prints the last ‘num’ bytes from the file specified.
chown
Different users in the operating system have ownership and permission to ensure that the files are
secure and put restrictions on who can modify the contents of the files. In Linux there are different
users who use the system:
Each user has some properties associated with them, such as a user ID and a home directory.
We can add users into a group to make the process of managing users easier.
A group can have zero or more users. A specified user is associated with a “default group”. It
can also be a member of other groups on the system as well.
Ownership and Permissions: To protect and secure files and directory in Linux we use permissions to
control what a user can do with a file or directory. Linux uses three types of permissions:
Read: This permission allows the user to read files and in directories, it lets the user read
directories and subdirectories stores in it.
Write: This permission allows a user to modify and delete a file. Also, it allows a user to
modify its contents (create, delete and rename files in it) for the directories. Unless you give
the execute permission to directories, changes does not affect them.
Execute: The write permission on a file executes the file. For example, if we have a file
named sh so unless we don’t give it execute permission it won’t run.
Types of file Permissions:
User: This type of file permission affects the owner of the file.
Group: This type of file permission affects the group which owns the file. Instead of the
group permissions, the user permissions will apply if the owner user is in this group.
Other: This type of file permission affects all other users on the system.
Note: To view the permissions we use:
ls -l
chown command is used to change the file Owner or group.
Whenever you want to change ownership you can use chown
command.
Syntax:
chown [OPTION]… [OWNER][:[GROUP]] FILE…
chown [OPTION]… –reference=RFILE FILE…
Example: To change owner of the file:
chown owner_name file_name
chown master file1.txt
where the master is another user in the system. Assume that if you
are user named user1 and you want to change ownership to root
(where your current directory is user1). use “sudo” before syntax.
sudo chown root file1.txt
chmod
This command is used to change the access permissions of files and
directories.
Syntax:
chmod <permissions of user,group,others> {filename}
4 – read permission
2 – write permission
1 – execute permission
0 – no permission
lsof
While working in Linux/Unix system there might be several file and
folder which are being used, some of them would be visible and
some not. lsof command stands for List Of Open File. This
DevOps Linux command provides a list of files that are opened.
Basically, it gives the information to find out the files which are
opened by which process. With one go it lists out all open files in the
output console.
Syntax:
$lsof [option][user name]
Options with Examples:
List all open files: This command lists out all the files that are
opened by any process in the system.
~$ lsof
Here, you observe there are details of the opened files. ProcessId,
the user associated with the process, FD(file descriptor), size of the
file all together gives detailed information about the file opened by
the command, process ID, user, its size, etc.
FDrepresents as File descriptor.
cwd: Current working directory.
txt: Text file.
mem: Memory file.
mmap: Memory-mapped device.
List all files opened by a user: There are several users of a
system and each user has different requirements and accordingly
they use files and devices. To find a list of files that are opened by a
specific user this command is useful.
Syntax:
lsof -u username
Along with that we can see the type of file here and they are:
DIR: Directory
REG: Regular file
CHR: Character special file
id
id command in Linux is used to find out user and group names and
numeric ID’s (UID or group ID) of the current user or any other user in the
server. This command is useful to find out the following information as
listed below:
User name and real user id.
Find out the specific Users UID.
Show the UID and all groups associated with a user.
List out all the groups a user belongs to.
Display security context of the current user.
Syntax:
id [OPTION]… [USER]
Options:
-g: Print only the effective group id.
-G: Print all Group ID’s.
-n: Prints name instead of a number.
-r: Prints real ID instead of numbers.
-u: Prints only the effective user ID.
–help: Display help messages and exit.
–version: Display the version information and exit.
Note: Without any OPTION it prints every set of identified information i.e.
numeric IDs.
Examples:
To print your own id without any Options:
id
The output shows the ID of current user UID and GID.
diff
diff command is used to find the difference between two files. This
command analyses the files and prints the lines which are not similar. Lets
say we have two files test and test1. you can find the difference between
the two files using the following command.
Syntax –
diff test.txt test1.txt
apt-get
apt-get is a command-line tool that helps in handling packages in Linux.
Its main task is to retrieve the information and packages from the
authenticated sources for installation, upgrade and removal of packages
along with their dependencies. Here APT stands for the Advanced
Packaging Tool.
syntax:
apt-get [options] command
update: This command is used to synchronize the package index files
from their sources again. You need to perform an update before you
upgrade.
apt-get update
df,du
The df (disk free) command reports the amount of available disk space
being used by file systems. The du (disk usage) command reports the
sizes of directory trees inclusive of all of their contents and the sizes of
individual files.
The aim is to make sure you are not overshooting the 80% threshold. If
you exceed the threshold it’s time to scale or clean-up the mess, because
running out of resources you have the change your application show some
fickle behavior.
To check in a human-readable format:
$ sudo df -h
kill
kill command in Linux (located in /bin/kill), is a built-in command which is
used to terminate processes manually. This command sends a signal to a
process that terminates the process. If the user doesn’t specify any signal
which is to be sent along with kill command then default TERM signal is
sent that terminates the process.
kill -l :To display all the available signals you can use below command
option:
Syntax:$kill -l
Git Commands
git init
Usage: git init [repository name]
This command creates a new repository.
git config
Usage: git config --global user.name “[name]”
Usage: git config --global user.email “[email address]”
This command sets the author name and email address respectively. This
is useful information with the commits.
git clone
Usage: git clone [url]
This command lets you get a copy of a repository from an existing URL.
git add
Usage: git add [file]
This command adds a file to the staging area.
Usage: git add *
This command adds one or more to the staging area.
git commit
Usage: git commit -m “[ Type in the commit message]”
This command records or snapshots the file permanently in the version
history.
Usage: git commit -a
This command commits any files you’ve added with the git add command
and also commits any files you’ve changed since then.
git status
Usage: git status
The git status command displays the state of the working directory and
the staging area.This command lets you see the changes that are in the
staging, those that are not staged and are not tracked by Git.
git show
Usage: git show [commit]
This command shows the metadata and content changes of the specified
commit.
git rm
Usage: git rm [file]
This command deletes the file from your working directory and stages the
deletion.
git remote
Usage: git remote add [variable name] [Remote Server Link]
This command connects your local repository to the remote server.
git push
Usage: git push [variable name] master
Usage: git push [variable name] [branch]
This command sends the branch commits to your remote repository.
Usage: git push –all [variable name]
This command pushes all branches to your remote repository.
Usage: git push [variable name] :[branch name]
This command deletes a branch on your remote repository.
git pull
Usage: git pull [Repository Link]
This command fetches and merges changes on the remote server to your
working directory.
git branch
Usage: git branch
This command lists all the local branches in the current repository.
Usage: git branch [branch name]
This command creates a new branch.
Usage: git branch -d [branch name]
This command deletes the feature branch.
git checkout
Usage: git checkout [branch name]
This command lets you switch from one branch to another.
Usage: git checkout -b [branch name]
This DevOps Linux command creates a new branch and also switches to it.
git merge
Usage: git merge [branch name]
This DevOps Linux command merges the specified branch’s history into
the current branch.
git rebase
Usage: git rebase [branch name]
git rebase master – This command will move all our work from the current
branch to the master.