Cheat sheet Linux and Git

Nikita Rawool
3 min readFeb 1, 2023

So, I’m taking up this challenge of #90DaysOfDevops with #TrainWithShubham community.

Let’s start with Day12

Linux File Command

ls to see files and folders

To see hidden files and folders

ls -a

To see the whole structure of the file

ls -l

Copy command

cp source destination

Ask if file alreday exists either it will overwrite content

cp -l

To copy dir1 directory into dir3 if dir3 doesn’t exist then it will create

cp -r dir1 dir3

To go one step back

cd ..

To go two steps back

cd ../..

To go into the root folder

cd /

To go into the home folder

cd ~

Cat command

To see contact of file

cat filename

Move command

move source destination

it will ask for permission if the destination already has that file

move -i source destination

Make directory command

mkdir image

Remove directory command

rmdir a/b/c/d it will just remove d folder not all

rmdir -p a/b/c/d it will remove the whole structure

suppose c has some content in it then won’t remove c

rm -rv a/b/c remove dir if it contains file in it

File Types in Linux

Character file.

Block file

. when u want to connect a hard disk then this will be used

Link file

Hard link

ln source destination

The hard link is just a copy of the main file

Soft link

ln -s Source Destination

A soft link is the link to the original file if you delete the original then the linked won’t be accessible

Socket file

one direction copy of data in between process

Named pipes

two direction copy of data in between process

How to identify These files

c character file

- regular file

b block file

l linked file

s socket file

p named pipe

d directory

Archiving

Tar is used to group multiple files in single file

command

Less command

To see the content of the file from the beginning

less file1.txt

capital B to go up

q to exit less command

shift G to go end of file

/nikita to search nikita word

Touch command

to create a new empty file

Find command

find . -size 40k file with size 40k

find . -size +40k more than.40k

find . -size -40k less than 40k size

Find . — name Filename find filename in current dir

Find -empty to find empty file

find . mmin +60 files modified 60 min ago

find . mmin — 60 files modified 60 min ago

find .mmin 60 files modified at 60 min from current time

Grep Command

To search something in file

Grep ‘line5’ file1.txt

Grep -i ‘line5l file1.txt’

Piping

output of cat will be provided as input to grep command

Cat /etc/os- release | grep -i ‘ubuntu’

Vi editor

yy copy

p paste

dd delete line

x delete word

:w save

:q Quit

:wq save quit

:q! quit without saving

Git and GitHub

Initialize git repository

Git init

Move file to staging Area

git add filename

Git commit to save changes

git commit-m “msg while commenting”

To check all commits

git log

Check status of current git directory

git status

To connect remote repository

git remote add origin_new https://github.com/nikita8086/Devops.git

To push files to git repository

git push origin main

Branching

Command to see all branches

git branch

Command to create new branch

git branch branchName

Command to switch baranch

git checkout branch_Name

Merge new branch with main branch

git merge branch_name

Create and switch branch

git checkout -b branch_name

delete a branch

git checkout -d <branch name>

Git reset To undo the changes

Git reset HEAD filename

Merge a branch with main branch

git checkout main

git merge <branch name>

Git revert To remove changes from remote repository

git revert <commit id of the commit that needs to be removed>

Git Stash

To save changes without committing

git stash list

git stash drop

git stash clean

Cherry-pic

Pickup commits of one branch and apply them to another branch

git cherry-pick commit id

Git Rebase

combining sequence of commit in new commit while merging

git rebase <branch name>

--

--