Surely gonna help you a lot if you are a beginner here, even more, essential for CCP. I have provided almost every basic Linux important command from the base level with real-life Linux coding examples with various use cases and individual details.
Hope you all engage with it!
History of Linux
According to history "https://en.wikipedia.org/wiki/Linux#History"
--It was first released on September 17, 1991, by Linus Torvalds.[13][14][15] Linux is typically packaged as a Linux distribution, which includes the kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name "GNU/Linux" to emphasize the importance of GNU software, causing some controversy
Basic LINUX commands.
To view what's written in a file.
The command to view what's written in a file is
cat filename
To change the access permissions of files.
The command to change the access permission of files is
chmod 777 filename
To check which commands you have run till now.
The command to check which commands you have run till now is
history
To remove a directory/ Folder.
The command to remove a folder is
rmdir foldername
- The command to create a file and view the content is
nano fruits.txt
or
vi fruits.txt
Add content in Fruits.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
cat> Fruits.txt Apple Mango Banana Cherry Kiwi Orange Guava
Show only the top three fruits from the file.
The command to view the top three fruits from the file i
head -n 3 fruits.txt (output: Apple Mango Banana)
Show only the bottom three fruits from the file.
The command to view only the bottom three fruits from the file is
tail -n 3 fruits.txt (output: Kiwi Orange Guava)
To create another file Colors.txt and to view the content.
vi Indreni.txt
Add content in Indreni.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.
cat> Indreni.txt Red Pink White Black Blue Orange Purple Grey
(Print Working Directory): Display the current directory.
pwd
ls
(List): List files and directories in the current directory.ls
cd
(Change Directory): Change to a different directory.cd /path/to/directory
mkdir
(Make Directory): Create a new directory.mkdir new_directory
cp
(Copy): Copy files or directories.cp file.txt /path/to/destination
mv
(Move/Rename): Move or rename files or directories.mv file.txt new_location/file.txt
rm
(Remove): Remove files or directories.rm file.txt
cat
(Concatenate): Display the contents of a file.cat file.txt
echo
(Echo): Print text or variables to the terminal.echo "Hello, World!"
grep
(Global Regular Expression Print): Search for a specific pattern in files.grep "pattern" file.txt
chmod
(Change Mode): Change the permissions of a file or directory.chmod 644 file.txt
sudo
(Superuser Do): Execute a command with administrative privileges.sudo apt-get update
man
(Manual): Display the manual page of a command.man ls
ping
: Check the connectivity to a remote host.ping google.com
wget
(Web Get): Download files from the web.wget https://example.com/file.txt
(Change Ownership): Change the ownership of files or directories.
chown user chown group
Search for files or directories based on various criteria.
find /path/to/search -name "*.txt"
SERVER Installation Linux Command:
Apache HTTP Server (Ubuntu):
Step 1: Update the package index:
sudo apt update
Step 2: Install Apache:
sudo apt install apache2
Step 3: Start the Apache service:
sudo systemctl start apache2
Step 4: Enable the Apache service to start on boot:
sudo systemctl enable apache2
- Red Hat/CentOS:
Open a terminal and run the following command to install tree
:
Copy codesudo yum install tree
- shows hidden files:
ls -a
ARCHIVE Linux command:
-x
: This option is used to extract files from a tar archive. When combined with other options, it indicates that you want to perform an extraction operation.-p
: The-p
option preserves the permissions of the extracted files. It ensures that the original file permissions (including ownership and access rights) are retained during the extraction process.-v
: The-v
option enables verbose output, which provides more detailed information during the extraction or creation of an archive. It displays the names of files as they are processed.-f
: The-f
option is used to specify the name of the tar archive file to be created or extracted. It must be followed by the filename of the tar archive.-c
: The-c
option is used to create a new tar archive. When combined with other options, it indicates that you want to perform a creation operation.-a
: The-a
option automatically detects the appropriate compression program based on the file extension of the archive. For example, if the archive has a.gz
extension, it will usegzip
for compression.
To summarize:
tar -xpvf archive.tar
: Extracts files from thearchive.tar
file, while displaying verbose output and preserving permissions.tar -xpf archive.tar
: Extracts files from thearchive.tar
file, preserving permissions.tar -xf archive.tar
: Extracts files from thearchive.tar
file without verbose output or preserving permissions.tar -caf archive.tar.xz directory/
: Creates a new compressed tar archive namedarchive.tar.xz
usingxz
compression for thedirectory/
directory.tar -cf archive.tar directory/
: Creates a new tar archive namedarchive.tar
from thedirectory/
directory.
- Extract files from a tar archive with verbose output and preserved permissions:
tar -xpvf archive.tar
- Extract files from a tar archive while preserving permissions:
tar -xpf archive.tar
- Extract files from a tar archive without verbose output or preserved permissions:
tar -xf archive.tar
- Create a new compressed tar archive using
xz
compression:
tar -caf archive.tar.xz directory/
- Create a new tar archive:
tar -cf archive.tar directory/
Troubleshooting in Linux :
often involves diagnosing and resolving issues related to system performance, networking, processes, and services. Here are some commonly used troubleshooting commands in Linux:
ping
: Checks network connectivity by sending ICMP echo requests to a specified destination. For example:
ping google.com
traceroute
: Determines the route taken by packets to reach a destination. It displays the IP addresses and response times of each hop along the path. For example:
traceroute google.com
netstat
: Displays network statistics, including active network connections, listening ports, and routing tables. For example:
netstat -tuln
top
: Provides real-time information about system processes, resource usage, and CPU activity. It helps identify resource-intensive processes. For example:
top
ps
: Lists currently running processes. It shows information such as process ID (PID), CPU and memory usage, and command details. For example:
ps
df
: Shows disk space usage information for mounted file systems. It helps identify available and used disk space. For example:
df -h
free
: Displays memory usage and available memory on the system. It provides information about total, used, and free memory. For example:
free -h
dmesg
: Examines the system's kernel ring buffer for diagnostic messages. It helps identify and troubleshoot hardware-related issues. For example:
dmesg | tail
journalctl
: Retrieves log messages from the systemd journal. It is useful for examining system logs and diagnosing issues. For example:
journalctl -xe
Pipeline Commad:
- Find files with a specific extension in a directory:
ls /path/to/directory | grep ".txt"
This command uses the ls
command to list the files in a directory and pipes it to grep
to filter files with the ".txt" extension.
- Calculate the sum of numbers in a file:
cat numbers.txt | awk '{sum += $1} END {print sum}'
This command uses the cat
command to read the contents of a file and pipes it to awk
to calculate the sum of numbers in the file.
Text Editors (Nano , Vim)
Nano:
- Open a file in Nano:
nano filename
Save a file in Nano: Press
Ctrl + O
, then Enter to save the file.Exit Nano: Press
Ctrl + X
to exit Nano. If there are unsaved changes, it will prompt you to save before exiting.
Vim:
- Open a file in Vim:
vim filename
Switch to insert mode in Vim: Press
i
to enter insert mode. You can then edit the file.Save a file in Vim: Press
Esc
to exit insert mode, then type:w
and press Enter to save the file.Exit Vim: Press
Esc
to ensure you're in command mode, then type:q
and press Enter to exit Vim. If there are unsaved changes, it will not allow you to exit. To force exit without saving changes, use:q!
.