Day 14 Task: Create a Linux & Git-GitHub Cheat Sheet

Day 14 Task: Create a Linux & Git-GitHub Cheat Sheet

Easy-to-Use Linux and Git-GitHub Cheat Sheet

Task: Create a Cheat Sheet

Linux Cheat Sheet -

1. File and Directory CRUD Navigation Commands

CRUD stands for Create, Read, Update, and Delete. CRUD operations are said to be the basic operations on any file or directory or database. Even if you are not a Linux User, file and directory CRUD operations are something that you should be comfortable with.

COMMANDMEANINGEXAMPLE & SYNTAX
ls (list all directories)Lists all the files and directories inside the current directory in which you are.Syntax: $ ls
ls -RLists all the files and directories inside the current directory as well as all the files and directories of the sub-directories as well.Syntax: $ ls -R
ls -aLists all the files and directories in the current directory and also lists the hidden files (such as .git files). However, this command does not list the files and directories of the sub-directories.Syntax: $ ls -a
ls -alLists files and directories of the current directory along with the details like permissions (read, write, execute), owner, file/dir size, etc.Syntax: $ ls -al
cdThis command is used to move to the root directory.Syntax: $ cd
cd ~Same function as cd i.e. move to the root/home directory. Please note that there is a space between cd and tilde (~) symbol.Syntax: $ cd ~
cd ..Move to one level up directory.Syntax: $ cd ..
cd dirNameMove to a particular directory from the current directory. Note that you can only move down the directory and not to the directories in the above level.Example: In the command shown on the right, we move from the root directory to Desktop.
mkdirThis command creates a directory.Example: The command shown in the right will create a directory named “exampleDir” in the current directory in which we are.
Syntax: $ mkdir exampleDir
cat > fileNameThis command creates a file in the current directory.Example: The command shown in the right creates a new file in the current directory and the name of the file will be file1 with an extension of ‘.txt’.
Syntax: $ cat > file1.txt
cat fileNameThis command displays the content in a file. If a file is not present in the current directory, it gives a message showing no such file exists.Example: The command shown on the right displays the content of the file file1.txt. “Hello there!” is the content inside it. Syntax: $ cat file1.txt Hello there!
cat f1 f2 > f3This command joins the content of two files and stores it in the third file. If the third file does not exist, it is first created and then the joined content is stored.Example: The command in the right stores the joined content of file1 and file2 in file3. File1 has “Hello there!” and file2 has “What’s up?” in their content. We have displayed the content of file3. Syntax: $ cat file1.txt file2.txt > file3.txt $ cat file3.txt Hello there! What’s up?
rmdir dirNameThis command is the remove directory command. It deletes a directory.Example: The remove directory command for deleting a directory named “exampleDir” is shown on the right. Syntax: $ rmdir exampleDir
mv fileName “new file path”This command is the move file command. It moves the file to the new path specified.Example: The mv command moves the file file1.txt to “Docs” directory. Syntax: $ mv file1.txt “Docs/”
mv fileName newNameThis command changes the name of the file from the old name i.e. the fileName to the newName.Example: The command in the right changes the name of the file file1 to file2. Syntax: $ mv file1.txt file2.txt
find <starting position to search> <expression determining what to find> <options> <what to find>This command is used for walking a file hierarchy. It is used to find files/directories and perform operations on them. We can search by file, folder, name, creation date, modification date, etc. There are a number of options available. For instance, exec searches the file that meets the criteria and returns 0 as exit status for successful command execution.Example: The command in the right is for searching a file with the name file1.txt in the Docs directory. Syntax: $ find ./Docs -name file1.txt
grep <options> pattern fileNameThe full form of this command is a global search for regular expression and printout. This command searches a file for a particular pattern of characters and displays all the lines that contain that pattern. The pattern being searched is called a regular expression (regex). There are a lot of <options> available. For instance, c is an option that is used to only count the number of lines in the file that matches the pattern.Example: The command to count the number of lines that have “abc” in them in the file file1.txt is shown on the right. Syntax: $ grep -c “abc” file1.txt

2. System Information Commands

These are some of the general-purpose system information commands that are important to know and easy to remember.

COMMANDMEANINGEXAMPLE & SYNTAX
historyThis command displays the list of all the typed commands in the current terminal session.Syntax: $ history
clearClears the terminal i.e. no previous command will be visible on the screen now.Syntax: $ clear
hostnameShows the name of the system host.Syntax: $ hostname
hostidDisplays the id of the host of the system.Syntax: $ hostid
sudoAllows a regular user to run the programs with the security privileges of a superuser or root.Syntax: $ sudo
apt-getThis command is used to install and add new packages.Syntax: $ apt-get
dateThis command is used to show the current date and time.Example: The command and its output are shown on the right. Syntax: $ date
Fri Feb 25 14:58:08 IST 2022
calShows the calendar of the current month.Example: The command cal and its output is shown on the right. Syntax: $ cal
whoamiThis command displays the name with which you are logged in.Example: The command is typed in and it shows the username with which the user has logged in. Syntax: $ whoami Guneet Malhotra
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.Example: The command to locate apropos command in Linux System is given on the right. Syntax: $ whereis apropos

3. File Permission Commands

There are 3 types of people who can use a file and each type has 3 types of access to the file. This is shown in the diagram given below:

There are 3 types of people accessing a file and they are:

  1. User (u)

  2. Group (g)

  3. Others (o)

Also, the access that we want to give to each of them is of three types:

  1. Read (r)

  2. Write (w)

  3. Execute (x)

COMMANDMEANINGEXAMPLE & SYNTAX
ls -l fileNameThis command is used to show the file permissions along with the owner and other details of the specified file.Example: The file permissions along with the owner and other details is shown for the file file1.txt on the right.
rThis command represents the read permission.Example: The command shown in the right adds the read permission to the o (other) class for the file file1.txt. Syntax: $ chmod o+r file1.txt
wThis command represents the write permission.Example: This commands adds the write permission for a(all) i.e. user, group and others. Syntax: $ chmod a+w file1.txt
xThis command represents the execute permission.Example: This command adds the execution permission for the user. Syntax: $ chmod u+x file1.txt

4. Hardware Information Commands

Let us now see, some of the hardware information commands that give us the information about the hardware that we are using.

COMMANDMEANINGEXAMPLE & SYNTAX
cpu-infoThis command is used to display the information about your CPU. Note that this command is not available by default. It can be used after installation of the necessary package using sudo apt install cpuinfo.Syntax: $ cpu-info
free -hThis command is used to display the free and used memory. -h is used for converting the information (to be displayed) to human-readable form.Syntax: $ free -h
lsusb -tvList all the USB connected devices.Syntax: $ lsusb -tv
cat /proc/meminfoGives the information about memory like total and occupied and so on.Syntax: $ cat /proc/meminfo
duThis command stands for disk usage and is used to estimate the space usage for a file or directory.Example: The following command gives the size in human-readable form for the Desktop folder. Syntax: $ du -h Desktop

5. File and Directory Compression Commands

The files can be compressed and then extracted to save the storage. We see this happening many times in our daily lives that we have to compress some file to send it or we have to extract a downloaded file. There are several commands for file compression in Linux given below:

COMMANDMEANINGEXAMPLE & SYNTAX
gzip fileNameThis command is used to compress a file with gzip compression.Example: The command to zip file1 using gzip compression is shown on the right. Syntax: $ gzip file1
gunzip fileName.gzThis command is used to unzip a file that has gzip compression.Example: The command to unzip fileDemo.gz file with gz compression is shown on the right. Syntax: $ gunzip fileDemo.gz
tar cf myDir.tar myDirThis command is used to create an uncompressed tar archive.Example: The command to create an uncompressed tar archive for the directory demoDir is shown on the right. Syntax: $ tar cf demoDir.tar demoDir
tar cfz myDir.tar myDirThis command is used to create a tar archive with gzip compression.Example: The command to create gzip tar archive for the directory demoDir is shown on the right. Syntax: $ tar cfz demoDir.tar demoDir
tar xf fileThis command is used to extract the contents of any type of tar archive.Example: The command to extract the content of demoFile tar archive is shown on the right. Syntax: $ tar xf demoFile

Git Commands -

S. NoCommand NameUse
1git initInitialise a local Git Repository
2git add.Add one or more files to the staging area
3git commit -m “Commit Message”Commit changes to the head but not to the remote repository.
4git statusCheck the status of your current repository and list the files you have changed.
5git logProvides a list of all commits made on a branch
6git diffView the changes you have made to the file
7git push origin <branch name>Push the branch to the remote repository so that others can use it.
8git config --global user.name “Name”Tell Git who you are by configuring the author name
9git config --global user.email user@email.comTell Git who you are by configuring the author email id.
10git clone <repository_name>Creates a Git repository copy from a remote source
11git remote add origin <server>Connect your local repository to the remote server and add the server to be able to push it.
12git branch <branch_name>Create a new branch
13git checkout <branch_name>Switch from one branch to another
14git merge <branch_name>Merge the branch into the active branch
15git rebaseReapply commits on top of another base tip
16git checkout -b <branch_name>Creates a new branch and switch to it
17git stashStash changes into a dirty working directory
18git pullUpdate local repository to the newest commit
19git revert <commit_id>Revert commit changes
20git clean -nShows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
21git log --summaryView changes (detailed)
22git diff HEADShow difference between working directory and last commit.
23git log --onelineView changes (briefly)
24git reflogShow a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.
25git rebase -i <base>Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.
26git restore --staged <file_name>Resetting a staged file
27git rm -r [File_name]Remove a file (or folder)
28git config --listList all variables set in config file, along with their values
29git branch -d <local_branch>Delete local branch in Git
30git push -d <remote_name> <branch_name>Delete remote branch in Git
31git stash popUnstash the changes
32git commit -amThe -am along with the command is to write the commit message on the command line for already staged files.
33git commit -ammendThe amend is used to edit the last commit. Incase we need to change the last committed message, this command can be used.
34git rmThe git rm command is used to remove or delete files from working tree and index.
35git pull --rebaseGit rebase is used to rewrite commits from one branch to another branch.
36git merge --squashThe squash along with git merge produces the working tree. It indexes in the same way as that of the real merge, but discards the merge history.
37git revert -e <commit_id>edit the commit mesage before reverting, -e is used for the same.
38git bisectGit bisect goes through all the previous commit and uses binary search to find the bugged commit.
39git blamegit blame is used to know who/which commit is responsible for the lastest changes in the repository.
40git cherry-pickChoosing a commit from one branch and applying it to another is known as cherry picking in Git.

Follow for more updates:)