ls
is used to list files and directories.
ls
ls -l -h
option provides details in human readable format.- Some options require values arguments/values to be passed.
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
command clears the terminal.- Shortcut:
Ctrl+L
clear
man
displays the user manual of a command .- Here we pass the command as an argument.
man <command>
man ls
- Type
q
to exit the manual
date
displays the system date and time.
date
whoami
displays the current logged in user
whoami
- Shell keeps track of the commands you have typed in.
- Use up ( ⬆) and down ( ⬇) arrows to access the commands.
history
displays the history of the commands you have typed in so far.- By default, It shows the last 500 recent commands.
history
Bash maintains the history to . bash_history file.
cat .bash_history
exit
to close/end a shell session.
exit
touch
creates an empty file.
touch filename
cat
eads contents of file and prints it.
cat filename
echo
output/prints a string in the terminal.
echo "content"
Using > (greater than) operator we can redirect the output of echo command to a file.
echo "Hello World!" > filename
-mv
renames the file names.
- destination can be a new or existing file.
mv source destination
mv practice.txt exam.txt
cp
copies src_file to dest_file.
cp src_file dest_file
cp exam.txt fun_text.txt
Note : If dest_file already exist then cp
overrides the contents of dest_file.
rm
removes (delete) files.
rm filename
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:.
representsCurrent directory
..
representsparent directory
ls -a
mkdir
creates a directory.
mkdir directory_path
pwd
prints name of current working directory.
cd
changes the current working directory.
cd directory_path
Note : cd /
changes your current directory to root folder.
mkdir
creates a directory.
mkdir directory_name
cd ..
move to parent directory.- Here
..
is relative path to parent directory.
cd ..
There are two notations for file paths:
- Absolute Path
- Relative Path
Representing the complete path of a file or folder from the root.
Representing the path of a file or folder wrt. current working directory.
In relative path conventions:
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
mv
renames the directory name
mv source destination
mv tutorial commands
mv
moves files or directories from source to destination paths.
mv source destination
mv welcome.txt commands
cp
can be used to copy files between directories.
cp file_path directory_name
cp welcome.txt commands
cp -r
can be used to copy a directory.
cp -r source_path destination_path
cp -r commands linux
Raises an error if destination path have any non existing directories in between.
rm -r
removes(deletes) directories.
rm -r directory_name
rm -r commands
- A text editor is used for editing text files.
- Various text editors are:
- Notepad++
- Sublime Text
- gEdit
- Visual Studio Code etc..
Nano is an easy to use command line text editor for Unix and Linux-based operating systems.
To open a file with nano, pass the filename as an argument.
nano filename
Add the text of the file in the middle of the editor.
To save a file, PRESS Ctrl+O
and Enter()
To exit from nano editor, PRESS Ctrl+X
To view file Contents:
cat filename
We can filter the contents of a file using the following filter commands.
- head
- tail
- grep
- Used to print top N lines of a file.
- By default, it will print the first 10 lines.
head [-N] filename
head -2 sentences.txt
- Used to print last N lines of a file.
- By default it will print the last 10 lines.
tail [-N] filename
tail -2 sentences.txt
wc
is used to find out number of lines, word count and characters count in the files.
wc filename
- 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.
command_1 | command_2 | command N
cat sentences.txt | head -2
Searches a file or files for lines that have a certain pattern.
cat filename | grep <pattern>
cat sentences.txt | grep "morning"
Number of lines that contain the word morning in the given file.
Occurrences of the word "morning" in the given file from the lines 10 to 15
">" takes the standard output of the command and redirects it to the file.
command > filename
cat sentences.txt | head -2 > learnings.txt
- 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:
We can use tar to compress files & directories
tar -czvf file-name.tar.gz path1 path2 ..
tar -czvf my_collection.tar.gz videos report.txt
tar -xzvf filename.tar.gz -C path
tar -xzvf my_collection.tar.gz -C collections
It is used to package all the files into one file with .zip extension
zip -r zipfile.zip file1 folder1 file2 ...
zip -r collections.zip videos report.txt
The unzip command extracts all files from the specified ZIP archive
unzip filename.zip -d path
unzip collections.zip -d new-folder
The root user, also known as the superuser or administrator, has access to all commands and files.
sudo
command temporarily elevates the privileges allowing users to complete sensitive tasks without logging in as the root user.
sudo command
which
command is used to identify the location of a given executable path.
which command
$ which sudo
/usr/bin/sudo
useradd
is used to create a new user with the given username.
sudo useradd username
sudo useradd ganu
passwd
is used to set or change password of a given user.
sudo passwd username
sudo passwd ganu
su
is used to execute command as another user.
su user
su -c command user
Multi-user operating systems like linux provide two levels of authorization in securing the files
- Ownership
- Permission
Users accessing a file/ directory can be categorized into 3 types
chmod permissions filename
chmod 764 sample.txt
"chmod" stands for change mode
- For changing the user of a file/directory
sudo chown user filename
sudo chown root sample.txt
- For changing the user and group of a file/directory
sudo chown user:group filename
sudo chown root:root sample.txt
- 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
- APT stands for the Advanced Packaging Tool
- It is used to install, upgrade or remove software packages in linux
sudo apt
Installing a package from a repository
sudo apt install package_name
- It is a Package Manager for macOS.
- With
brew
command you can install the packages in macOS.
- With
brew install package_name
yum
is a graphical based package management tool for RPM (RedHat Package Manager) based Linux systems.
yum package_name
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.
wget "URL"
wget "https://www.lifewire.com/uses-of-command-wget-2201085"
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.
curl "URL"
curl "wttr.in"
apt-cache
command is used to searches for a particular package.
sudo apt-cache search package_name
apt-cache search google
- 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
will upgrade all the applications to latest version.
sudo apt upgrade
update
will simply update the information about the existing packages and their versions available
sudo apt update
dpkg -l
: It is used to list all installed packages
sudo dpkg -l
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
sudo add-apt-repository repository_link
Your Linux device can use that signature to check the authenticity of the packages.
apt-key
: Add PPA security key
sudo apt-key add - KEY_ID
remove
: removes the installed packages
sudo apt remove package_name
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.
ping
checks the network connectivity between host and server/host.
ping hostname/IP address
ping google.com
traceroute
prints the route that a packet takes to reach the host.
traceroute host_address
traceroute google.com
A network interface is a software interface to networking hardware.
Different types of network interfaces are:
- Ethernet
- Loopback etc.
ifconfig
gives you the information of various network interfaces.
ifconfig
exxxx is a physical interface representing Ethernet network card.
- lo is a special virtual network interface called loopback device.
- It is used to connect to services running on the user system
- Linux environment variables act as placeholders for information stored within the system.
- They will be available to the programs launched from the shell.
env
command can be used to print all the environment variables.
env
export
command is used to define/update value for a variable.
export VARIABLE_NAME=variable_value
export CUSTOM_ENV_VARIABLE=10
- Use $ to access environment variables.
- When redefining variables, do not use the dollar sign.
echo $VARIABLE_NAME
$ echo $CUSTOM_ENV_VARIABLE
unset
command removes an environment variable.
unset VARIABLE_NAME
PATH variable holds the colon-separated list of folder paths where executables are located.
echo $PATH
alias
is a (usually short) name that the shell translates into another name or command.
alias name=value
alias t=traceroute
t google.com
unalias
removing an existing alias is known as unaliasing.
unalias alias_name
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.
export VARIABLE_NAME=variable_value
# ~/.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