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.
COMMAND | MEANING | EXAMPLE & SYNTAX |
ls (list all directories) | Lists all the files and directories inside the current directory in which you are. | Syntax: $ ls |
ls -R | Lists 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 -a | Lists 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 -al | Lists files and directories of the current directory along with the details like permissions (read, write, execute), owner, file/dir size, etc. | Syntax: $ ls -al |
cd | This 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 dirName | Move 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. |
mkdir | This 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 > fileName | This 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 fileName | This 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 > f3 | This 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 dirName | This 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 newName | This 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 fileName | The 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.
COMMAND | MEANING | EXAMPLE & SYNTAX |
history | This command displays the list of all the typed commands in the current terminal session. | Syntax: $ history |
clear | Clears the terminal i.e. no previous command will be visible on the screen now. | Syntax: $ clear |
hostname | Shows the name of the system host. | Syntax: $ hostname |
hostid | Displays the id of the host of the system. | Syntax: $ hostid |
sudo | Allows a regular user to run the programs with the security privileges of a superuser or root. | Syntax: $ sudo |
apt-get | This command is used to install and add new packages. | Syntax: $ apt-get |
date | This 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 | ||
cal | Shows the calendar of the current month. | Example: The command cal and its output is shown on the right. Syntax: $ cal |
whoami | This 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] fileName | This 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:
User (u)
Group (g)
Others (o)
Also, the access that we want to give to each of them is of three types:
Read (r)
Write (w)
Execute (x)
COMMAND | MEANING | EXAMPLE & SYNTAX |
ls -l fileName | This 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. |
r | This 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 |
w | This 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 |
x | This 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.
COMMAND | MEANING | EXAMPLE & SYNTAX |
cpu-info | This 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 -h | This 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 -tv | List all the USB connected devices. | Syntax: $ lsusb -tv |
cat /proc/meminfo | Gives the information about memory like total and occupied and so on. | Syntax: $ cat /proc/meminfo |
du | This 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:
COMMAND | MEANING | EXAMPLE & SYNTAX |
gzip fileName | This 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.gz | This 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 myDir | This 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 myDir | This 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 file | This 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. No | Command Name | Use |
1 | git init | Initialise a local Git Repository |
2 | git add. | Add one or more files to the staging area |
3 | git commit -m “Commit Message” | Commit changes to the head but not to the remote repository. |
4 | git status | Check the status of your current repository and list the files you have changed. |
5 | git log | Provides a list of all commits made on a branch |
6 | git diff | View the changes you have made to the file |
7 | git push origin <branch name> | Push the branch to the remote repository so that others can use it. |
8 | git config --global user.name “Name” | Tell Git who you are by configuring the author name |
9 | git config --global user.email user@email.com | Tell Git who you are by configuring the author email id. |
10 | git clone <repository_name> | Creates a Git repository copy from a remote source |
11 | git remote add origin <server> | Connect your local repository to the remote server and add the server to be able to push it. |
12 | git branch <branch_name> | Create a new branch |
13 | git checkout <branch_name> | Switch from one branch to another |
14 | git merge <branch_name> | Merge the branch into the active branch |
15 | git rebase | Reapply commits on top of another base tip |
16 | git checkout -b <branch_name> | Creates a new branch and switch to it |
17 | git stash | Stash changes into a dirty working directory |
18 | git pull | Update local repository to the newest commit |
19 | git revert <commit_id> | Revert commit changes |
20 | git clean -n | Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean. |
21 | git log --summary | View changes (detailed) |
22 | git diff HEAD | Show difference between working directory and last commit. |
23 | git log --oneline | View changes (briefly) |
24 | git reflog | Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs. |
25 | git 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. |
26 | git restore --staged <file_name> | Resetting a staged file |
27 | git rm -r [File_name] | Remove a file (or folder) |
28 | git config --list | List all variables set in config file, along with their values |
29 | git branch -d <local_branch> | Delete local branch in Git |
30 | git push -d <remote_name> <branch_name> | Delete remote branch in Git |
31 | git stash pop | Unstash the changes |
32 | git commit -am | The -am along with the command is to write the commit message on the command line for already staged files. |
33 | git commit -ammend | The amend is used to edit the last commit. Incase we need to change the last committed message, this command can be used. |
34 | git rm | The git rm command is used to remove or delete files from working tree and index. |
35 | git pull --rebase | Git rebase is used to rewrite commits from one branch to another branch. |
36 | git merge --squash | The 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. |
37 | git revert -e <commit_id> | edit the commit mesage before reverting, -e is used for the same. |
38 | git bisect | Git bisect goes through all the previous commit and uses binary search to find the bugged commit. |
39 | git blame | git blame is used to know who/which commit is responsible for the lastest changes in the repository. |
40 | git cherry-pick | Choosing a commit from one branch and applying it to another is known as cherry picking in Git. |
Follow for more updates:)