List of basic Linux commands
==========================================
Linux is a multitasking and multiprocessing system (It allows multiple processes to operate simultaneously without interfering with each other)
Directory-related Linux commands (Directory is nothing but a folder)
pwd ==> Displays present working directory path
mkdir ==> The mkdir command will allow you to create directory.
Example: mkdir Narendra ==> This command will create a directory called "Narendra".
rmdir: The rmdir command will delete an empty directory.
below 2 commands are the same
rm -r <folder_name > To delete a directory and all of its contents recursively
rm -rf <folder name> To delete a directory and all of its contents recursively without requiring confirmation
cd ==>change directory (To traverse from one directory to inside directory)
The cd command will allow you to change directory /directories.
When you open a terminal you will be in your home directory.
To move around the file system you will use cd.
Examples:
- To navigate into the root directory, use cd / ↵
- To navigate to your home directory, use cd ↵ or cd ~ ↵
- To navigate up/back one directory level, use cd .. ↵
- To navigate to the previous directory (or before), use "cd - ↵
- To navigate through multiple levels of a directory at once, specify the full directory path that you want to go to. For example, use, "cd home/Narendra/test_scripts/" to go directly to the test_scripts subdirectory.
- As another example, "cd ~/Desktop" will move you to the Desktop subdirectory inside your home directory.
Files related Linux commands
touch file_name.txt ==> create a single file in pwd (present working directory)
touch file_name1.txt file_name_2.txt ==> create multiple files in pwd
cat file_name1.txt ==> display the content of the file cat file_name1.txt file_name_2.txt ==> display the content of the multiple files at a time
cat file_name1.txt file_name_2.txt > filename_3.txt ==> joins 2 files and stores the output in 3rd file.
cp file_name1.txt file_name_2.txt ==> copy entire content/data from file_name1.txt to file_name_2.txt
cp -r directory_1 directory_2
mv file_name.txt narendra.txt ==> to rename the file ( rename file_name.txt as narendra.txt)
head file_name.txt ==> show the first 10 lines of the file
tail file_name.txt ==> show the last 10 lines of the file
wc file_name1.txt ==> print the number of words, no of lines, file_size(number of bytes)
find . -name "narendra.txt" ==> Find a file (narendra.txt /any extension-related files) in current and sub-directories
find /home -name *.jpg ==>
Look for all .jpg files(any extension-related files) in the /home and directories below it.
ls ==> Displays available files & folders available in pwd
ls -l ==> Displays available files & folders available in pwd with permissions, sizes, date & time
ls -a ==> Displays available & Hidden files & folders available in pwd
ll ==> Displays available & Hidden files & folders available in pwd with permissions, sizes, date & time
ls -lart ==> Displays all the files in the current directory but displays latest files at the below along with time stamp.
ls -R ==> Displays all the files in the sub-directories along with the path.
diff ==> Compare the contents of two files line by line
Example: diff <file_name_1_with_extension> <file_2_name_with_extension>
tar ==> archive multiple files into a common Linux file format.
How to convert a folder into a tar file
tar -czvf new_name_or_existing_dir_name.tar.gz directory_name
Ex: tar -czvf abc.tar.gz narendra
tar -czvf narendra.tar.gz narendra
ls
To untar any tar file --> tar -xvzf tar_file_name
How to unzip a zip file
sudo apt-get install unzip
unzip file_name.zip ( or ) unzip file.zip -d destination_folder
rm ==> This command is to remove or delete a file in your directory.
Ex: rm <file_name_with_extension>
readlink -f file_name ==> It displays the complete file path
Small clarification: pwd will display the file's path, but readlink -f file_name will display the path name and the file name at a time.
è What is chmod 777?
chmod 777 means granting all permissions to read, write, and execute any file/directory by any user of the system
sudo chmod 777 -R Dir_name (Note: -R means recursive Change files and directories recursively)
General Linux commands
lscpu ==> Displays the complete CPU-related information (Model name: XXX XXXX XXXX 64-Core Processor)
who ==> At present parallelly how many people using your machine (& IP address of them )
cat /etc/os-release ==> See the complete OS-related information
command output: NAME="SLES/ UBUNTU/"
VERSION="15-xxx"
VERSION_ID="15.xx"
PRETTY_NAME="xxxx Linux Enterprise Server 15xx
uname -r ==> Displays the current OS kernel version (system information)==> 5.x.xx-xx-default
sudo dkms status ==> Displays which GPU and kernel version ==> amdgpu, 5.xx.x.xx.xx-1xxxxx
man command shows the manual instructions of Linux commands Example: man ps
ps command provides information about the currently running processes, including their process identification numbers (PIDs).
kill command is used to terminate the process manually Example: kill -9 PID
hostname command shows the name of your host/network
ping check connectivity to a server ping <IP_address of any server> Ex: ping 192.168.X.X
last command shows last logins (Note: displays last logins along with date& time)
id command shows details of the user.
top command monitor system resource usage
ifconfig command shows the IP addresses of all network interfaces. ( inet ip_address is essential to access)
Upgrade UBUNTU OS
cat /etc/os-release ==> It will display current OS details on the machine
sudo apt-get update
sudo apt-get upgrade -y
Note1: For upgrade to dot version sudo apt-get dist-upgrade -y && sudo reboot
Ex: Ubuntu 20.04.4 LTS" to Ubuntu 20.04.5 LTS"
Note2: For upgrade to new release version sudo do-release-upgrade -y && sudo reboot
Ex: Ubuntu 20.04" to Ubuntu 22.04"
cat /etc/os-release ==> You have to check OS was upgraded or not?
cat /etc/os-release ==> It will display current OS details on the machine
sudo yum update
sudo yum upgrade -y
Note1: For Update dot version sudo yum dist-upgrade -y && sudo reboot
Note2: For Upgrades to new release sudo do-release-upgrade -y && sudo reboot
cat /etc/os-release ==> You have to check OS was upgraded or not?
Install Java / jdk (Java development kit) through CLI
In Ubuntu OS --> sudo apt install openjdk-17-jdk
In RHEL OS --> sudo yum install java-17-openjdk-devel
or
In RHEL OS --> sudo rpm -ivh jdk-17_linux-aarch64_bin.rpm
If multiple package (java/python/) versions are available then set a specific version as the default version among multiple versions
Ex: 1
sudo update-alternatives --config java
Enter to keep the current selection[+], or type selection number: 1/2/3/4
Ex: 2
sudo update-alternatives --config python
Enter to keep the current selection[+], or type selection number: 1/2/3/4
How to upgrade only kernel directly sudo apt install linux-image-<kernel_version> linux-headers-<kernel_version> linux-modules-extra-<kernel_version>
Ex: sudo apt install linux-image-6.2.0-26-generic linux-headers-6.2.0-26-generic linux-modules-extra-6.2.0-26-generic
then reboot the machine sudo reboot then check by using the command uname -r
Disk Usage-related Linux commands
df command gets a report on the system's disk space usage (it will display in KB (Kilobyte))
df -h command gets a report on the system's disk space usage (it will display in GB (gigabyte))
du command checks the disk space usage of a file or directory
Ex: du narendra.txt or du folder_name
du -ah shows disk usage for all the files & directory
du -sh shows disk usage of the current directory
How to connect remote machine/server from Linux Terminal
ssh username@<IP address> press Enter, enter the password of the remote machine
ssh narendra@10.xx.xx.xx <Enter>
How to copy a file from a local to a remote machine
scp file_name.zip root@10.xx.41.xxx:/home/
How to copy a file within the same machine?
cp<space> file_name_to_be_copied<space>path_of _the_directory (where to copy)
How to copy a directory within the same machine?
cp<space> directory_name_to_be_copied<space> -r <space>path_of _the_directory (where to copy)
watch command :
==============
Syntax: watch [OPTIONS] COMMAND
watch
the command will temporarily clear all of the terminal content and run the provided command at regular intervals. When used without any option watch
will run the specified command every two seconds.
Example: 1
In Terminal==> watch date <Enter> by default, every 2 seconds date will update.
==> watch<space> -n <space>5 <space>date (for 5 seconds date will update)
==> watch <space>-n <space>5 <space>-t <space> date
Note: Ctrl+c to exit from it.
Example: 2
watch python log_parser.py
log_parser python will execute continuously.
Linux Keyboard Shortcuts
ctrl+alt+t ==> To open a terminal in Linux-related OS
ctrl+ Alt +t ==> To open a new terminal window
ctrl+ Shift +t ==> To open the terminal tab
ctrl + a ==> Move the cursor to the beginning of the line
Ctrl + e ==> Move the cursor to the end of the line
ctrl + c ==> To stop/come out / break the currently executing command (In the Terminal)
(if you have typed a long/short command in the terminal, but you don't want to run/ erase it, then you can use Ctrl+C in the Terminal)
ctrl +d ==> to close the current terminal (or) exit ==> logout of the current session.
ctrl +l ==> will clear the terminal screen (or) clear command will clear the terminal screen
ctrl +r ==> Recall the last command that matches the provided characters (handy command)
TAB ==> autofill typing (while typing directory names/file names /commands type starting 2 letters then type TAB )
!! ==> Repeat the last command
ctrl + k ==> cut part of the line after the cursor & add it to the clipboard
ctrl + u ==> cut part of the line before the cursor & add it to the clipboard
ctrl + w ==> cut one word before the cursor & add it to the clipboard
ctrl + y ==> paste from clipboard
ctrl + o ==> Run the previously recalled command (for some Linux flavors only)
How to change hostname in Linux-based machines/servers
1. sudo vi /etc/hostname
2. Modify existing hostname (type required hostname)
3. save it (:wq!)
4. sudo reboot
5. check the new hostname "cat /etc/hostname"
hostnamectl command in Linux
hostnamectl command provides a proper API, to control Linux system hostname and change its related settings.
hostnamectl command also helps to change the hostname without editing the /etc/hostname file in a given system.
Syntax: hostnamectl [OPTIONS...] COMMAND ....
Where COMMAND can be any of the following:
status: Used to check current hostname settings.
set-hostname NAME: Used to set system hostname.
set-icon-name NAME: Used to set icon name for host.
set-chassis NAME: Set chassis type for host.
Types of hostnames:
Static: Assigned by system admin and it is used to initialize the kernel hostname during boot time.
Dynamic or Transient: Assigned by mDNS server or DHCP server during run time.
Pretty: It’s a high-level hostname assigned by system admin or end-user.
hostnamectl / hostnamectl status command will give the below output
Static hostname : NARENDRA_SERVER
Icon name : computer-server
Chassis : server 🖳 /Desktop
Machine ID : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Boot ID : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Operating System : UBUNTU22.04
Kernel : Linux+ 5.xx.xxx.x......
Architecture : x86-64
Hardware Vendor : Supermicro / ASUS
Hardware Model : AS -xxxxxx-XXXX
sudo dmidecode -t chassis command will give the below information
# dmidecode 3.3
Getting SMBIOS data from sysfs.
SMBIOS 3.5.0 present.
Handle 0x0003, DMI type 3, 22 bytes
Chassis Information
Manufacturer: Supermicro
Type: Desktop/server
Lock: Not Present
Version:
Serial Number:
Asset Tag: Chassis Asset Tag
Boot-up State: Safe
Power Supply State: Safe
Thermal State: Safe
Security Status: None
OEM Information:
Height: Unspecified
Number Of Power Cords: 1
Contained Elements: 0
SKU Number: To be filled by O.E.M.
sudo dmidecode -s bios-version --> will give the SBIOS version installed on the machine
Screen tool
What is the purpose of the screen tool?
screen
tool is a terminal multiplexer. It allows you to create, manage, and maintain
multiple terminal sessions within a single window or SSH session.
commands to use the screen tool:
sudo apt-get install screen (it will take a couple of seconds to install)
to create a screen with a specific name then use the below command.
command screen -R <screen_name> (Capital S)
Example: screen -R Narendra
Then, run a script or run a command, which will take a longer time.
while running the script you can come out of the screen, to create one more screen to perform another operation.
to come out of the 1st screen click on Ctrl+A leave the control then just press D to go out of the "Narendra screen"
(Note: we called it [detached from 5150. Narendra]) but still, the command/script will be under process.
screen -R Surendra run the command to create one more screen for executing scripts which may take a longer duration.
Click Ctrl+A leave the control button then press D to come out of the "Surendra screen"
(note: we called it [detached from 5150. Surendra]) but still, the command will be under process.
if you want to see the list of background running screens then type the command:
screen -ls
There are screens on:
5657.Narendra (14/07/22 11:00:47 AM IST) (Detached)
5150.Surendra (14/07/22 10:55:53 AM IST) (Detached)
==> To go to a particular screen & to see what is happening on it type the command
screen -r Narendra (we called -r as re-attach)
==> To close any screen completely, go to the screen and type Ctrl +d / type exit command.
==> To detach the attached screen, screen -d Narendra this command will detach the Narendra screen.
Python Related commands
====================
python -V(capital V) ==> Displays python 2.7.X version installed on your machine
python3 -V(capital V) ==> Displays python 3.X.X version installed on your machine.
What is a log file?
A log file is a computer-generated data file that contains information about usage patterns,
activities, and operations within an operating system, application, server or another device.
Log files show whether resources are performing properly and optimally.
What is the purpose of a log file?
Log files are used to record events or activities that occur within a computer system/ application/ program.
Log files serve as a detailed record of what has happened within a system,
which can be used for troubleshooting problems or investigating security incidents.
Example1: let’s assume server crash happened!
then "Log files are used to investigate what happened before server crashed"
Note: In Linux based OS, all log files will be saved at "/var/log/" path
Example2: After system turn ON, if you have faced any problem,
then we need to check in boot.log (available at "/var/log/" )
Request: If you find this information useful, please provide your valuable comments.
No comments:
Post a Comment