Skip to content

Latest commit

 

History

History
789 lines (770 loc) · 19.3 KB

README.md

File metadata and controls

789 lines (770 loc) · 19.3 KB

Command-line

Listing Files & Directories

ls   is used to list files and directories.

ls

Using Additional Options

  • ls -l -h option provides details in human readable format. link ls details
  • Some options require values arguments/values to be passed.
    • --block-size   option rounds the file size to nearest values.
    • Inputs:  KB , MB link ls size

Get Options for Commands

help   displays a list of options that you can use with the command.

ls --help

This Command will be helpful when you don’t know about its parameters and return type etc.

Clear the Screen

  • clear   command clears the terminal.
  • Shortcut:   Ctrl+L
clear

Get User Manual for Commands

  • man   displays the user manual of a command .
  • Here we pass the command as an argument.

syntax :

man <command>

example :

man ls
  • Type   q   to exit the manual

Get System Date & Time

date   displays the system date and time.

date

Get Current User

whoami   displays the current logged in user

whoami

Previous Commands

  • Shell keeps track of the commands you have typed in.
  • Use up ( ⬆) and down ( ⬇) arrows to access the commands.

History

  • history   displays the history of the commands you have typed in so far.
  • By default, It shows the last 500 recent commands.
history

Bash History

Bash maintains the history to . bash_history file.

cat .bash_history

Exit

exit   to close/end a shell session.

exit

Summary :

link Summary-1

Working With Files

Creating a File

touch   creates an empty file.

touch filename

Viewing File Content

cat   eads contents of file and prints it.

cat filename

Echo

echo   output/prints a string in the terminal.

echo "content"

Writing to Files Using echo Command

Using > (greater than) operator we can redirect the output of echo command to a file.

echo "Hello World!" > filename

Renaming a File

-mv   renames the file names.

  • destination can be a new or existing file.

syntax :

mv source destination

Example :

mv practice.txt exam.txt

Copying Files

cp   copies src_file to dest_file.

syntax :

cp src_file dest_file

Example :

cp exam.txt fun_text.txt

Note : If dest_file already exist then   cp   overrides the contents of dest_file.

Deleting a File

rm   removes (delete) files.

syntax :

rm filename

example :

rm exam.txt

Hidden Files

  • Linux, by default, hides many of the sensitive system files, in order to avoid accidental changes.
  • Hidden files starts with "."
  • ls a   shows the hidden files.
  • ls a   also shows the current and parent directories:
    • . represents Current directory
    • .. represents parent directory
ls -a

Summary:

link summary-2

Working with Directories

Creating A Directory

mkdir   creates a directory.

mkdir directory_path

Current Working Directory

pwd   prints name of current working directory. link create-directory

Changing the Current Working Directory

cd   changes the current working directory.

cd directory_path

Note : cd /   changes your current directory to root folder.

Creating a Directory in Directory

mkdir   creates a directory.

mkdir directory_name

Switching to Parent Directory

  • cd ..   move to parent directory.
  • Here   ..   is relative path to parent directory.
cd ..

File Paths

There are two notations for file paths:

  1. Absolute Path
  2. Relative Path

Absolute Path:

Representing the complete path of a file or folder from the root. link absolute-path

Relative Path:

Representing the path of a file or folder wrt. current working directory.

In relative path conventions:

  • .   refers to the current working directory.
  • ..   refers to the parent directory. link relative-path

Home Directory

Each user in the computer is given a separate directory to work with - called home directory.

  • cd ~  can be used to switch to home directory.
cd ~
  • cd   (cd and space) command can also be used to switch to home directory.
cd 

Renaming a directory

mv   renames the directory name

syntax :

mv source destination

example :

mv tutorial commands

Moving a directory

mv   moves files or directories from source to destination paths.

syntax :

mv source destination

example :

mv welcome.txt commands

Copying Files to Another Directory

cp   can be used to copy files between directories.

syntax :

cp file_path directory_name

example :

cp welcome.txt commands

Copying Directory

cp -r   can be used to copy a directory.

syntax :

cp -r source_path destination_path

example :

cp -r commands linux

Common Mistake

Raises an error if destination path have any non existing directories in between. link common-mistake

Deleting a Directory

rm -r   removes(deletes) directories.

syntax :

rm -r directory_name

example :

rm -r commands

Summary

We can use folder/file paths for cp, mv, rm commands. link summary-3

Working with Files

Text Editor

  • A text editor is used for editing text files.
  • Various text editors are:
    • Notepad++
    • Sublime Text
    • gEdit
    • Visual Studio Code etc..

Nano

Nano is an easy to use command line text editor for Unix and Linux-based operating systems. link nano

Open file

To open a file with nano, pass the filename as an argument.

nano filename

Updating File

Add the text of the file in the middle of the editor. link update file

Saving File

To save a file,   PRESS Ctrl+O   and   Enter()

Exit Nano

To exit from nano editor,   PRESS Ctrl+X

Viewing File Contents

To view file Contents:

cat filename

Filtering & Output Redirection

Filtering

We can filter the contents of a file using the following filter commands.

  • head
  • tail
  • grep

head

  • Used to print top N lines of a file.
  • By default, it will print the first 10 lines.

syntax :

head [-N] filename

example :

head -2 sentences.txt

tail

  • Used to print last N lines of a file.
  • By default it will print the last 10 lines.

syntax :

tail [-N] filename

Example :

tail -2 sentences.txt

Counting

Word Count

wc   is used to find out number of lines, word count and characters count in the files.

syntax :

wc filename

example :

link word count

piping

  • Pipe is used to combine two or more commands
  • Output of one command is passed as an input to the command followed and so on.

Using ‘ | ’

syntax :

command_1 | command_2 | command N 

example :

cat sentences.txt | head -2

Grep

Searches a file or files for lines that have a certain pattern.

syntax :

cat filename | grep <pattern>

example :

cat sentences.txt | grep "morning"

Example 1

Number of lines that contain the word morning in the given file. link grep ex 1

Example 2

Occurrences of the word "morning" in the given file from the lines 10 to 15 link grep ex2

Output Redirection

">" takes the standard output of the command and redirects it to the file.

syntax :

command > filename

example :

cat sentences.txt | head -2 > learnings.txt

Compressing & Uncompressing Files

  • File compression is a reduction in the number of bits needed to store the data of a file.
  • Files are stored in such a way that, it uses less disk space than all the individual files and directories combined
  • Advantages of compressing files are:
    • taking less disk space
    • easier and faster transmission
  • Commonly used file formats for the compressed files:
    • gzip
    • zip
    • tar link concept of compressing and uncompressing

tar

We can use tar to compress files & directories

Compression

syntax :

tar -czvf file-name.tar.gz path1 path2 ..

example :

tar -czvf my_collection.tar.gz videos report.txt

Extract/ Uncompress

syntax :

tar -xzvf filename.tar.gz -C path

example :

tar -xzvf my_collection.tar.gz -C collections

Zip

It is used to package all the files into one file with .zip extension

syntax :

zip -r zipfile.zip file1 folder1 file2 ...

example :

zip -r collections.zip videos report.txt

Unzip

The unzip command extracts all files from the specified ZIP archive

syntax :

unzip filename.zip -d path

example :

unzip collections.zip -d new-folder

Summary

link summary-4

Super User & File Permissions

Linux Users

The root user, also known as the superuser or administrator, has access to all commands and files.

Root User

sudo   command temporarily elevates the privileges allowing users to complete sensitive tasks without logging in as the root user.

sudo command

Linux Commands

Executable Path

which  command is used to identify the location of a given executable path.

syntax :

which command

example :

$  which sudo
/usr/bin/sudo

Create New Users

useradd   is used to create a new user with the given username.

syntax :

sudo useradd username 

example :

sudo useradd ganu

Set/Change User Password

passwd   is used to set or change password of a given user.

syntax :

sudo passwd username

example :

sudo passwd ganu

Execute Command as Another User

su   is used to execute command as another user.

su user
su -c command user

File Permissions

Authorization Levels

Multi-user operating systems like linux provide two levels of authorization in securing the files

  • Ownership
  • Permission

User Ownership

Users accessing a file/ directory can be categorized into 3 types link ownership-table

Ownership and Permissions

link permission-table

File Permissions in Linux

link linux-file-permissions

Changing Permissions

syntax :

chmod permissions filename

example :

chmod 764 sample.txt

"chmod" stands for change mode link chmod-concept link chmod-table

Changing Ownership

  • For changing the user of a file/directory

syntax :

sudo chown user filename

example :

sudo chown root sample.txt
  • For changing the user and group of a file/directory

syntax :

sudo chown user:group filename

example :

sudo chown root:root sample.txt

Packaging

Package & Repository

  • A package file is a compressed collection of files that comprise the software package
  • Packages are stored in repositories to make them accessible to users

Package Managers

apt

  • APT stands for the Advanced Packaging Tool
  • It is used to install, upgrade or remove software packages in linux

syntax :

sudo apt

Installing Package

Installing a package from a repository

syntax :

sudo apt install package_name

brew

  • It is a Package Manager for macOS.
    • With  brew  command you can install the packages in macOS.

Installing Package

syntax :

brew install package_name

yum

yum  is a graphical based package management tool for RPM (RedHat Package Manager) based Linux systems.

Installing Package

syntax :

yum package_name

Downloading Files From Web

wget

wget   is a command-line utility for downloading files from the web.

To install   wget   :

  • apt
sudo apt install wget
  • brew :
sudo brew install wget
  • yum:
sudo yum install wget

wget  will download the resource specified in the url to the current directory.

syntax :

wget "URL"

example :

wget "https://www.lifewire.com/uses-of-command-wget-2201085"

curl

curl   is a command-line utility for transferring data from or to a server designed to work without user interaction.

To instal   curl :

  • apt :
sudo apt install curl
  • brew :
sudo brew install curl
  • yum :
sudo yum install curl

curl   prints the contents of the URL to the output.

syntax :

curl "URL"

example :

curl "wttr.in"

Searching a Package

apt-cache   command is used to searches for a particular package.

syntax :

sudo apt-cache search package_name

example :

apt-cache search google

Updating Packages

  • System checks against the repositories.
  • If newer version available of the program it will update the information about the existing packages and their versions available. Updating a package from a repository:

upgrade

upgrade   will upgrade all the applications to latest version.

sudo apt upgrade

update

update   will simply update the information about the existing packages and their versions available

sudo apt update

Listing Installed Packages

dpkg -l   : It is used to list all installed packages

sudo dpkg -l

Adding Repository

PPA (Personal Package Archive) is an application repository that can be used to upgrade and install packages from third parties.   add-apt-repository   : It is used to add repository

syntax :

sudo add-apt-repository repository_link

Adding the Private PPA's security key

Your Linux device can use that signature to check the authenticity of the packages. apt-key   : Add PPA security key

syntax :

sudo apt-key add - KEY_ID

Removing a Package

remove   : removes the installed packages

syntax :

sudo apt remove package_name

Summary

link summary-5 Warning Under the installation of an   apt   package from a PPA, there is a change in one of the MongoDB installation steps, due to the changes made by the MongoDB website.

Update command from

curl https://www.mongodb.org/static/pgp/server-4.4.asc  | sudo apt-key add -

to

curl https://pgp.mongodb.com/server-4.4.asc | sudo apt-key add -

for a seamless monogodb installation.


Networking Commands

Network Connectivity

ping   checks the network connectivity between host and server/host.

syntax :

ping hostname/IP address

example :

ping google.com

Route Path

traceroute   prints the route that a packet takes to reach the host.

syntax :

traceroute host_address

example :

traceroute google.com

Network Interface

A network interface is a software interface to networking hardware.

Different types of network interfaces are:

  • Ethernet
  • Loopback etc.

Information About Network Interfaces

ifconfig

ifconfig   gives you the information of various network interfaces.

ifconfig

link ifconfig

Ethernet Interface

exxxx is a physical interface representing Ethernet network card. link Ethernet

loopback interface

  • lo is a special virtual network interface called loopback device.
  • It is used to connect to services running on the user system link loopback

Environment Setup

Environment Variables

  • Linux environment variables act as placeholders for information stored within the system.
  • They will be available to the programs launched from the shell.

Listing

env   command can be used to print all the environment variables.

env

Creating & Updating

export   command is used to define/update value for a variable.

syntax :

export VARIABLE_NAME=variable_value

example :

export CUSTOM_ENV_VARIABLE=10

Accessing Environment Variables

  • Use $ to access environment variables.
  • When redefining variables, do not use the dollar sign.

syntax :

echo $VARIABLE_NAME

example :

$  echo $CUSTOM_ENV_VARIABLE

Delete Environment Variable

unset   command removes an environment variable.

unset VARIABLE_NAME

Commands Folder Path

PATH variable holds the colon-separated list of folder paths where executables are located.

echo $PATH

Aliasing Command

alias   is a (usually short) name that the shell translates into another name or command.

syntax :

alias name=value

example :

alias t=traceroute
t google.com

Un-aliasing Command

unalias   removing an existing alias is known as unaliasing.

unalias alias_name

Persistent Variables

Environment variables defined in a shell are deleted as soon as you exit the terminal.

To persistent environment variables we write them in .bashrc or ~/.bash_profile configuration files.

Editing .bashrc

syntax :

export VARIABLE_NAME=variable_value

example :

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/Examples/startup-files (in the package bash-doc)
# for Examples
case $- in
    *i*) ;
...
export CUSTOM_ENV_VARIABLE=5
  • To immediately apply all changes in .bashrc,

    use the   source   command

source ~/.bashrc