# Day-12: LINUX &GIT-GITHUB CHEAT SHEET

### **File Commands**

⦁ `ls` --&gt; Directory listing.

⦁ `ls -al` --&gt; Formatted listing with hidden files.

⦁ `ls *.sh` --&gt; list all the files having .sh extension.

⦁ `ls -i` --&gt; list the files and directories with index numbers inodes.

⦁ `ls -d */` --&gt; list only directories.(we can also specify a pattern).

⦁ `ls -lt` --&gt; Sorting the Formatted listing by time modification.

⦁ `cd dir` --&gt; Change directory to dir.

⦁ `cd -->` Change to the home directory.

⦁ `cd -` --&gt; Go to the last working directory.

⦁ `cd ..` --&gt; change directory to one step back.

⦁ `cd ../..` --&gt; Change directory to 2 levels back.

⦁ `pwd` --&gt; Show the current working directory.

⦁ `mkdir dir` --&gt; Creating a directory dir.

⦁ `cat >file` --&gt;Places the standard input into the file.

⦁ `more file` --&gt; Output the contents of the file.

⦁ `head file` --&gt; Output the first 10 lines of the file.

⦁ `tail file` --&gt; Output the last 10 lines of the file.

⦁ `tail -f file` --&gt; Output the contents of a file as it grows, starting with the last 10 lines.

⦁ `touch file` --&gt;Create or update file.

⦁ `rm file` --&gt; Deleting the file.

⦁ `rm -r dir` --&gt; Deleting the directory.

⦁ `rm -f file` --&gt; Force to remove the file.

⦁ `rm -rf dir` --&gt; Force to remove the directory dir.

⦁ `cp file1 file2` --&gt; Copy the contents of file1 to file2.

⦁ `cp -r dir1 dir2` --&gt; Copy dir1 to dir2; create dir2 if not present.

### **Process management**

⦁ `ps` --&gt; To display the current working processes.

⦁ `top` --&gt; Display all running processes.

⦁ `kill pid` --&gt; Kill the process with given pid.

⦁ `killall proc` --&gt; Kill all the process named proc.

⦁ `pkill pattern` --&gt; Will kill all processes matching the pattern.

⦁ `date` --&gt; Show the current date and time.

⦁ `cal` --&gt; Show this month's calendar.

⦁ `uptime` --&gt; Show current uptime.

⦁ `w` --&gt; Display who is online.

⦁ `whoami` --&gt; Who you are logged in as.

⦁ `uname -a` --&gt; Show kernel information.

⦁ `man` --&gt; command Show the manual for command.

⦁ `df` --&gt; Show the disk usage.

⦁ `du` --&gt; Show directory space usage.

⦁ `free` --&gt; Show memory and swap usage.

⦁ `whereis app` --&gt; Show possible locations of app.

⦁ w`hich app` --&gt; Show which applications will be run by default.

### **Searching**

⦁ `grep pattern` --&gt; file Search for pattern in file.

⦁ `grep -r pattern dir` --&gt; Search recursively for pattern in dir.

⦁ `command | grep pattern` --&gt; Search pattern in the output of a command.

⦁ `locate file` --&gt; Find all instances of file.

⦁ `find . -name filename` --&gt; Searches in the current directory (represented by a period) and below it, for files and directories with names starting with filename.

### **File permission**

⦁ `r` --&gt; read permission

⦁ `w` --&gt; write permission

⦁ `x` --&gt; execute permission

⦁ `chmod 777 filename` --&gt; Assign read, write, and execute permission to everyone.

⦁ `chmod -R 777 dirname` --&gt; Assign full permission to the directory and all sub- directories.

⦁ `chmod 766 filename` --&gt; Assign full permission to the owner, and read and write permission to group and others.

⦁ `chown username filename` --&gt; Change the ownership of a file.

⦁ `chrgp groupname filename` --&gt; Change group ownership of a file.

### **User Management Commands**

⦁ `sudo useradd username` --&gt; To add a new user.

⦁ `sudo passwd -l username` --&gt; To change the password of a user.

⦁ `sudo userdel -r username` --&gt; To remove a newly created user.

⦁ `sudo usermod -a -G groupname username` --&gt; To add a user to a group.

⦁ `sudo userdel user groupname` --&gt; To remove a user from a group.

### **Compression**

⦁ `tar cf` --&gt; file.tar file Create tar named file.tar containing file.

⦁ `tar xf file.tar` --&gt; Extract the files from file.tar.

⦁ `tar czf file.tar.gz files` --&gt; Create a tar with Gzip compression.

⦁ `tar xzf file.tar.gz` --&gt; Extract a tar using Gzip.

⦁ `tar cjf file.tar.bz2` --&gt; Create tar with Bzip2 compression.

⦁ `tar xjf file.tar.bz2` --&gt; Extract a tar using Bzip2.

⦁ `gzip file` --&gt; Compresses file and renames it to file.gz .

⦁ `gzip -d file.gz` --&gt; Decompresses file.gz back to file.

### **Network**

⦁ `ping host` --&gt; Ping host and output results.

⦁ `whois domain` --&gt; Get whois information for domains.

⦁ `dig domain` --&gt; Get DNS information for domain.

⦁ `dig -x host` --&gt; Reverse lookup host.

⦁ `wget file` --&gt; Download file.

⦁ `wget -c file` --&gt; Continue a stopped download.

### **<mark>Git-GitHub Cheat-Sheet</mark>**

**\*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`](http://user.name) `"UserName"`  
**Set the email:**  
`git config --global` [`user.email`](http://user.email) `"`[`yourEmail@gmail.com`](mailto:yourEmail@gmail.com)`"`  
**Check the setting:**  
`git config global --list`

**\*Initializing a repository**

`git init`

**\*Staging files**

`git add file1` --&gt; Stages a single file

`git add file1 file2` --&gt; Stages multiple files

`git add .` \--&gt; Stages the current directory and all its content

**\*Viewing the status**

`git status`

**\*Committing the staged files**

`git commit -m “Message”`

**\*Skipping the staging area**

`git commit -am “Message”`

**\*Removing files**

`git rm file` --&gt; Removes from working directory and staging area

`git rm --cached file` --&gt; Removes from staging area only

**\*Renaming or moving files**

`git mv file file1.txt`

**\*Viewing the staged/unstaged changes**

`git diff` --&gt; Shows unstaged changes

`git diff --staged` --&gt; Shows staged changes

`git diff --cached` --&gt; Same as the above

**\*Viewing the history**

`git log` --&gt; Full history

`git log --oneline` --&gt; Summary

`git log --reverse` --&gt; Lists the commits from the oldest to the newest

**\*Unstaging files (undoing git add)**

`git restore --staged file`

**\*Comparing commits**

`git diff HEAD~2 HEAD` \--&gt; Shows the changes between two commits

**\*Viewing a commit**

`git show 921a2ff` --&gt; Shows the given commit

`git show HEAD` --&gt; Shows the last commit

`git show HEAD~2` --&gt; Two steps before the last commit

### **<mark>Branching &amp; Merging</mark>**

**\*Managing branches**

`git branch dev` --&gt; Creates a new branch dev

`git checkout dev` --&gt; Switches to the dev branch

`git checkout -b dev` --&gt; Creates and switches to dev branch

`git branch -d dev` --&gt; deletes the dev branch

**\*Stashing**

`git stash` --&gt; Creates a new stash

`git stash list` --&gt; Lists all the stashes

`git stash show stash@{1}` --&gt; Shows the given stash

`git stash apply 1` --&gt; Applies the given stash to the working dir

`git stash drop 1` --&gt; Deletes the given stash

`git stash clear` --&gt; Deletes all the stashes

**\*Merging**

`git merge dev` --&gt; Merges the dev branch into the current branch

`git merge --squash dev` --&gt; Performs a squash merge

**\*Viewing the merged branches**

`git branch --merged` --&gt; Shows the merged branches

`git branch --no-merged` --&gt; Shows the unmerged branches

**\*Rebasing**

`git rebase master` --&gt; Changes the base of the current branch

**\*Cherry picking**

`git cherry-pick fed53ed` --&gt; Applies the given commit on the current branch

### <mark>Collaboration</mark>

**\*Cloning a repository**

`git clone url`

**\*Syncing with remote**

`git fetch origin master` --&gt; Fetches master from origin

`git fetch origin` --&gt; Fetches all objects from origin

`git pull` --&gt; Fetch + merge

`git push origin master` --&gt; Pushes master to origin

**\*Managing remote**

`git remote` --&gt; Shows remote repos

`git remote add upstream url` --&gt; Adds a new remote called upstream

`git remote rm upstream` \--&gt; Removes upstream

### **<mark>Rewriting History</mark>**

**\*Undoing commits**

`git reset --soft HEAD` --&gt; Removes the last commit, keeps changes staged

`git reset --mixed HEAD` --&gt; Unstages the changes as well

`git reset --hard HEAD` --&gt; Discards local changes

**\*Reverting commits**

`git revert 72856ea` --&gt; Reverts the given commit

`git revert HEAD~3` --&gt; Reverts the last three commits

**\*Amending the last commit**

`git commit --amend`

**\*Interactive rebasing**

`git rebase -i HEAD~5`

\&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Thank you for reading! :-) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
