Day 12 - Linux Commands Cheat sheet

Day 12 - Linux Commands Cheat sheet

1. File and Directory Commands

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.

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.

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.

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.

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.

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.

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.

2. System Information Commands

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.

cal

Shows the calendar of the current month.

Example: The command cal and its output is shown on the right.

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.

whereis [options] filename

This command is used to find the location of the 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 several options available.

Example: The command to locate apropos command in Linux System is given on the right.

3. File Permission Commands

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.

w

This command represents the write permission.

Example: This commands adds the write permission for a(all) i.e. user, group and others.

x

This command represents the execute permission.

Example: This command adds the execution permission for the user.

4. Hardware Information Commands

COMMAND

MEANING

EXAMPLE & SYNTAX

cpu-info

This command is used to display 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 a human-readable form.

Syntax: $ free -h

lsusb -tv

List all the USB-connected devices.

Syntax: $ lsusb -tv

cat /proc/meminfo

Gives 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.

5. File and Directory Compression Commands

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.

gunzip fileName.gz

This command is used to unzip a file that has gzip compression.

Example: The command to unzip the fileDemo.gz file with gz compression is shown on the right.

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.

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.

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 the demo file tar archive is shown on the right.

  1. Environment Variable Commands

    | COMMAND | MEANING | EXAMPLE & SYNTAX | | --- | --- | --- | | env | This command displays all the environment variables. | Syntax: $ env | | echo $Variable | This command displays the environment variable. | Example: The command at the right will display the INSTANCE environment variable. | | unset | This command removes a variable. | Syntax: $ unset |

7. User Management Commands

COMMAND

MEANING

EXAMPLE & SYNTAX

sudo adduser username

This command is used to add a user.

Syntax: $ sudo adduser username

sudo passwd -l ‘username’

This command is used to change the password of a user.

Example: Command to change the password for user1 is shown

sudo userdel -r ‘username’

This command is used to remove a newly created user.

Example: Command to delete the newly created user1

sudo usermod -a -G GROUPNAME USERNAME

This command is used to add a user to a particular group.

Example: The command to add user2 to group1 is shown.

Sudo deluser USER GROUPNAME

This command is used to remove a user from a group.

Example: The command to delete user1 from group1 is shown.

finger

This command shows the information of all the users logged in.

Syntax: $ finger

finger username

This command gives information about a particular user.

Example: The command to get information about user1 is shown on the right.

8. Networking Commands

COMMAND

MEANING

SYNTAX

dir

This command is used to display files in the current directory of a remote computer.

Syntax: $ dir

put file

This command is used to upload a ‘file’ from the local to the remote computer.

Syntax: $ put file

get file

This file is used to download a ‘file’ from the remote to the local computer.

Syntax: $ get file

quit

This command is used to log out.

Syntax: $ quit

9. Process Commands

COMMAND

MEANING

EXAMPLE & SYNTAX

bg

This command is used to send a process to the background.

Example: The process with id 1 is sent to the background by providing its id to bg.

fg

This command is used to run a stopped process in the background.

Example: The process with id 1 is brought to the foreground with the help of this command.

top

This command is used to get the details of all active processes.

Syntax: $ top

ps

This command is used to give the status of running for a user.

Syntax: $ ps

ps PID

This command gives the status of a particular process.

Example: Displays the status of the process with id 12230.

pidof

This command is used to give the process ID of a particular process.

Syntax:

Git-GitHub Cheat-Sheet

1. 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 "User name"
    Set the email:
    $ git config --global user.email "email id"
    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

2. Starting a project

  • Git init
    Create a local repository:
    $ git init

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

3. Local changes

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

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

4. 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 Branches:
    $ git diff < branch 2>

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

  • Git show Shows objects:
    $ git show

5. 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 the location:
    $ git log -p

6. 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

7. 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
    Create a new branch and switch to it:
    $ git checkout -b Checkout a Remote branch:
    $ git checkout

  • Git stash
    Switch branches without committing to 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-pick
    Apply the changes introduced by some existing commits:
    $ git cherry-pick

8. 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 to 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

9. Remote

  • Git remote
    Check the configuration of the remote server:
    $ git remote -v
    Add a remote for the repository:
    $ git remote adds 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

10. Pushing Updates

  • 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 edited

11. Pulling updates

  • 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

12. Undo changes

  • Git revert
    Undo the changes:
    $ git revert
    Revert a particular commit:
    $ git revert

  • Git reset
    Reset the changes:
    $ git reset -hard
    $ git reset -soft:
    $ git reset --mixed

13. Removing files

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