Day 12 - Linux & Git-GitHub Cheat Sheet

Day 12 - Linux & Git-GitHub Cheat Sheet

Day 12 of 90daysofdevops

Linux Cheat Sheet

Basic Commands

CommandDescription
lsLists all files and directories in the present working directory
ls -alLists files and directories with detailed information like permissions, size, owner, etc.
cd or cd ~Navigate to HOME directory
cd ..Move one level up
cdTo change to a particular directory
cd /Move to the root directory
cat > filenameCreates a new file
cat filenameDisplays the file content
cat file1 file2 > file3Joins two files (file1, file2) and stores the output in a new file (file3)
mv file "new file path"Moves the files to the new location
mv filename new_file_nameRenames the file to a new filename
sudoAllows regular users to run programs with the security privileges of the superuser or root
rm filenameDeletes a file
manGives help information on a command
historyGives a list of all past commands typed in the current terminal session
clearClears the terminal
mkdir directory-nameCreates a new directory in the present working directory or a at the specified path
rmdirDeletes a directory
mvRenames a directory
pr -xDivides the file into x columns
pr -hAssigns a header to the file
pr -nDenotes the file with Line Numbers
lp -nc, lpr cPrints “c” copies of the File
lp-d lp-PSpecifies the name of the printer
apt-getCommand used to install and update packages
mail -s 'subject' -c 'cc-address'
-b 'bcc-address' 'to-address'Command to send email
mail -s "Subject" to-address < FilenameCommand to send an email with an attachment

ls Options

CommandDescription
-rReverse order
-RRecursive list
-aShow all (including hidden)
-tSort by last modified
-SSort by file size
-lLong listing format
-1One file per line
-mComma-­sep­arated output
-QQuoted output

System Information Commands

CommandDescription
hostnameShows the name of the system host.
hostidDisplays the id of the host of the system.
apt-getThis command is used to install and add new packages.
dateThis command is used to show the current date and time.
calShows the calendar of the current month.
whoamiThis command displays the name with which you are logged in.
whereis [options] fileNameThis command is used to find the location of source/binary file of a command and manuals sections for a specified file in Linux System. This command is similar to the find command but this command is faster as it produces more accurate results by taking less time compared to the find command. There are again a number of options available.

File Permission Commands

CommandDescription
rread permission
wwrite permission
xexecute permission
-=no permission
chmod 777 filenameAssign full(read, write, and execute) permission to everyone
chmod -R 777 dirnameAssign full permission to the directory and all sub-directories
chmod 766 filenameAssign full permission to the owner, and read and write permission to group and others
chmod -x filenameRemove the execution permission of any file
chown username filenameChange the ownership of a file
chown user:group filenameChange the owner and group ownership of a file
chown -R user:group dirnameChange the owner and group ownership of the directory and all sub-directories

Disk Management Commands

CommandDescription
fdisk -lList all disk partitions
fdisk /dev/sdaCreate a new partition on /dev/sda device
mkfs.ext4 /dev/sda1Format the partition named /dev/sda1
fsck.ext4 /dev/sda1Check and repair a filesystem for any error
mount /dev/sda1 /mntMount any partition to any directory
df -hDisplay free space of mounted file system
df -iDisplay free inodes on the filesystem
du -hsDisplay the size of your current directory
lsblkDisplay information about block devices
lsusb -tvDisplay all USB devices
hdparm -tT /dev/sdaPerform a read speed test on disk /dev/sda
badblocks -s /dev/sdaTest for unreadable blocks on disk /dev/sda
topDisplay and manage the top processes
htopInteractive process viewer (top alternative)
mpstat 1Display processor related statistics
vmstat 1Display virtual memory statistics
iostat 1Display I/O statistics
tail -100 /var/log/messagesDisplay the last 100 syslog messages (Use /var/log/syslog for Debian based systems.)
tcpdump -i eth0Capture and display all packets on interface eth0
tcpdump -i eth0 'port 80'Monitor all traffic on port 80 ( HTTP )
lsofList all open files on the system
lsof -u userList files opened by user
free -hDisplay free and used memory ( -h for human readable, -m for MB, -g for GB.)
watch df -hExecute "df -h", showing periodic updates

User Management Commands

CommandDescription
sudo adduser usernameTo add a new user
sudo passwd -l 'username'To change the password of a user
sudo userdel -r 'username'To remove a newly created user
sudo usermod -a -G GROUPNAME USERNAMETo add a user to a group
sudo deluser USER GROUPNAMETo remove a user from a group
fingerShows information of all the users logged in
finger usernameGives information of a particular user

File and Directory Compression Commands

CommandDescription
gzip fileNameThis command is used to compress a file with gzip compression.
gunzip fileName.gzThis command is used to unzip a file that has gzip compression.
tar cf myDir.tar myDirThis command is used to create an uncompressed tar archive.
tar cfz myDir.tar myDirThis command is used to create a tar archive with gzip compression.
tar xf fileThis command is used to extract the contents of any type of tar archive.
tar -cvf filename.tar filenameCompress a file in the Tar archive
tar -xvf filename.tarUncompress a Tar file
tar -tvf filename.tarList the content of the Tar file
tar -xvf filename.tar file1.txtUntar a single file from Tar file
tar -rvf filename.tar file2.txtAdd a file to the Tar file
zip filename.zip filenameCompress a single file to a zip
zip filename.zip file1.txt file2.txt file3.txtCompress multiple files to a zip
zip -u filename.zip file4.txtAdd a file to a zip file
zip -d filename.zip file4.txtDelete a file from a zip file
unzip -l filename.zipDisplay the content of zip archive file
unzip filename.zipUnzip a file
unzip filename.zip -d /dirnameUnzip a file to a specific directory

Networking command

CommandDescription
SSH username@ip-address or hostnamelogin into a remote Linux machine using SSH
Ping hostname="" or =""To ping and Analyzing network and host connections
dirDisplay files in the current directory of a remote computer
cd "dirname"change directory to “dirname” on a remote computer
put fileupload ‘file’ from local to remote computer
get fileDownload ‘file’ from remote to local computer
quitLogout

Process command

CommandDescription
bgTo send a process to the background
fgTo run a stopped process in the foreground
topDetails on all Active Processes
psGive the status of processes running for a user
ps PIDGives the status of a particular process
pidofGives the Process ID (PID) of a process
kill PIDKills a process
niceStarts a process with a given priority
reniceChanges priority of an already running process
dfGives free hard disk space on your system
freeGives free RAM on your system

Git & Github Cheat Sheet

Git configuration

  • Git config
    Get and set configuration variables that control all facets of how Git looks and operates.
    Set the name:
    git config --global user.name "UserName"
    Set the email:
    git config --global user.email "yourEmail@gmail.com"
    Set the default editor:
    git config --global core.editor Vim
    Check the setting:
    git config -list

  • Git alias
    Set up an alias for each command:
    git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status

Starting a project

  • Git init
    Create a local repository:
    git init

  • Git clone
    Make a local copy of the server repository.
    git clone

Local changes

  • Git add
    Add a file to staging (Index) area:
    git add Filename
    Add all files of a repo to staging (Index) area:
    git add .

  • Git commit
    Record or snapshots the file permanently in the version history with a message.
    git commit -m " Commit Message"

Track changes

  • Git diff
    Track the changes that have not been staged: git diff
    Track the changes that have been staged but not committed:
    git diff --staged
    Track the changes after committing a file:
    git diff HEAD
    Track the changes between two commits:
    git diff <commit hash 1> <commit hash 2>

  • Git status
    Display the state of the working directory and the staging area.
    git status

  • Git show Shows objects:
    git show

Commit History

  • Git log
    Display the most recent commits and the status of the head:
    git log
    Display the output as one commit per line:
    git log -oneline
    Displays the files that have been modified:
    git log -stat
    Display the modified files with location:
    git log -p

  • Git blame
    Display the modification on each line of a file:
    git blame <file name>

Ignoring files

  • .gitignore
    Specify intentionally untracked files that Git should ignore.

    Create .gitignore:
    touch .gitignore

    List the ignored files:
    git ls-files -i --exclude-standard

Branching

  • Git branch Create branch:
    git branch List Branch: git branch --list Delete a Branch: git branch -d Delete a remote Branch: git push origin -delete Rename Branch: git branch -m

  • Git checkout
    Switch between branches in a repository.
    Switch to a particular branch:
    git checkout branch_name
    Create a new branch and switch to it:
    git checkout -b branch_name

  • Git stash
    Switch branches without committing the current branch. Stash current work:
    git stash
    Saving stashes with a message:
    git stash save ""
    Check the stored stashes:
    git stash list
    Re-apply the changes that you just stashed:
    git stash apply
    Track the stashes and their changes:
    git stash show
    Re-apply the previous commits:
    git stash pop
    Delete a most recent stash from the queue:
    git stash drop
    Delete all the available stashes at once:
    git stash clear
    Stash work on a separate branch:
    git stash branch

  • Git cherry pic
    Apply the changes introduced by some existing commit:
    git cherry-pick commit_id

Merging

  • Git merge
    Merge the branches:
    git merge
    Merge the specified commit to the currently active branch:
    git merge

  • Git rebase
    Apply a sequence of commits from distinct branches into a final commit.
    git rebase
    Continue the rebasing process:
    git rebase -continue

    Abort the rebasing process:
    git rebase --skip

  • Git interactive rebase
    Allow various operations like edit, rewrite, reorder, and more on existing commits.
    git rebase -i

Remote

  • Git remote
    Check the configuration of the remote server:
    git remote -v
    Add a remote for the repository:
    git remote add

    Fetch the data from the remote server:
    git fetch
    Remove a remote connection from the repository:
    git remote rm
    Rename remote server:
    git remote rename
    Show additional information about a particular remote:
    git remote show
    Change remote:
    git remote set-url

  • Git origin master
    Push data to the remote server:
    git push origin master

    Pull data from remote server:
    git pull origin master

Push

  • Git push
    Transfer the commits from your local repository to a remote server.

    Push data to the remote server:
    git push origin master

    Force push data:
    git push -f
    Delete a remote branch by push command:
    git push origin --delete branch_name

Pull

  • Git pull
    Pull the data from the server:
    git pull origin master
    Pull a remote branch:
    git pull

  • Git fetch
    Download branches and tags from one or more repositories.

    Fetch the remote repository:
    git fetch <repository Url>

    Fetch a specific branch:
    git fetch
    Fetch all the branches simultaneously:
    git fetch -all
    Synchronize the local repository:
    git fetch origin

Undo changes

  • Git revert
    Undo the changes:
    git revert
    Revert a particular commit:
    git revert <commit hash>

  • Git reset
    Reset the changes:

    git reset -hard git reset -soft: git reset --mixed

Removing files

  • Git rm
    Remove the files from the working tree and from the index:
    git rm <file Name>
    Remove files from the Git But keep the files in your local repository:
    git rm --cached

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #linux #git #github