Day 3: Linux Basic Commands

Commands-
To view what's written in a file.
$ cat altos.txtcat -b:This adds line numbers to non-blank linescat -n:This adds line numbers to all linescat -s:This squeezes blank lines into one linecat –E:This shows $ at the end of the line
To run any bash script written in editors like nano,vim etc.
./altos.txt./- Allow you to run the bash script
To see all the permissions of files.
ls -lals- List files and directories-l- List files and directories with permission-a- List all files and directories, even hidden files and directories with permission
To change the access permissions of files.
chmod 777 altos.txtor
chmod o+wx testfile ls -l testfile -rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile chmod u-x testfile ls -l testfile -rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile chmod g = rx testfile ls -l testfile -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfileUnderstanding Security Permissions
First, you need to think of the nine signs as three groups of three signs (see box below). Each of the three "rwx" icons represents a different action you can perform on the file.--- --- --- rwx rwx rwx user group otherr = “read” the file’s contents
w = “write”, or modify, the file’s contents
x = “execute” the file
- = "No permission"
To check which commands you have run till now.
historyTo make a file.
touch file1.txtTo remove a file.
rm file1.txtTo make a directory/folder.
mkdir myfolderTo remove a directory/folder.
mkdir -r myfolderTo create a fruits.txt file and to view the content.
touch fruits.txt echo "here is the watermelon" > fruits.txt | cat fruits.txtor
touch fruits.txt vim fruits.txt cat fruits.txtAdd content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
vim fruits.txtvim- It is a text editor and after saving the edited file it'll automatically create the file without givingtouchcommand.Below are the commands to use vim editor,
Entering and Exiting Vim:
vim: Open a file in Vim.vim filename: Open a specific file in Vim.:q: Quit Vim.:q!: Force quit Vim without saving changes.:wqor:x: Save changes and exit Vim.:w: Save changes to the current file.:w filename: Save the file with a new name.:wqor:x: Save changes and exit Vim.Apple Mango Banana Cherry Kiwi Orange Guava # press "ESC + :wq + ENTER" to save and exit the file
To Show only top three fruits from the file.
The
headcommand in Linux is used to display the first few lines of a text file. By default, it displays the first 10 lines of a file, but you can specify a different number of lines using the-noption. Here's the basic syntax of theheadcommand:head [OPTIONS] [FILE]a)
OPTIONSare optional flags that modify the behavior of theheadcommand.b)
FILEis the name of the file you want to view. If not specified,headreads from standard input.To display the first 3 fruits from fruits.txt file,
head -n 3 fruits.txtTo Show only the bottom three fruits from the file.
The
tailcommand in Linux is used to display the last few lines of a text file. It is particularly useful for monitoring log files or any other text files where you want to see the most recent entries. Here's the basic syntax of thetailcommand:tail [OPTIONS] [FILE]a)
OPTIONSare optional flags that modify the behavior of thetailcommand.b)
FILEis the name of the file you want to display. If not specified,tailreads from standard input.To display the last 3 fruits from fruits.txt file,
tail -n 5 fruits.txtTo create another file colors.txt and to view the content.
touch colors.txtAdd content in colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
Red Pink White Black Blue Orange Purple Grey # Press "ESC + :wq + ENTER" to save and exit the fileTo find the difference between fruits.txt and Colors.txt file.
The
diffcommand compares two files line by line and highlights the differences. Here's the basic syntax:diff fruits.txt colors.txtor
vimdiff fruits.txt colors.txt #display the difference with visualsTo capture the packets of current network interface. For more tcpdump commands, Click Here. Below is the Syntax to capture packets from current network:
apt install tcpdump #to install tcpdump sudo tcpdump #to run tcpdumpThe
traceroutecommand is a network diagnostic tool used to trace the route taken by packets from a source to a destination over an IP network. It provides valuable insights into the network path, including the number of hops (routers) between the source and destination, and the round-trip time (RTT) for each hop.traceroute [options] destinationDifference Between tcpdump and traceroute:
tcpdumpandtracerouteare both network diagnostic tools but serve different purposes.tcpdump captures and analyzes network packets in real-time, allowing you to inspect the data traveling over a network. It helps in debugging network issues by providing detailed packet-level information (IP addresses, ports, flags, etc.).
traceroute traces the path that packets take from one device to another over a network, showing each hop between routers. It’s useful for identifying network routing issues and delays at various points in the path.
Tool that allows you to view information about the processes running on your Linux system.
psDifference between top and ps and other similar command.
topandpsare both used to monitor system processes, but they serve different purposes:topprovides a real-time, dynamic view of system performance, showing CPU, memory usage, and a constantly updated list of running processes. It's useful for ongoing monitoring of system resource consumption.psdisplays a snapshot of currently running processes at the moment it is run. It shows detailed process information (PID, memory, CPU usage, etc.) but does not update in real-time.
Other similar commands:
htop(liketopbut more interactive).vmstatshows system resource statistics like memory, processes, and I/O.
Command to Download
wget <url> (and) curl <url>curlandwgetare both command-line tools used for downloading content from the web, but they have key differences:curlis a versatile tool that supports many protocols (HTTP, FTP, SMTP, etc.), and is often used for making API requests, sending data, and handling complex network interactions. It downloads content to stdout by default.wgetis primarily designed for downloading files from the web and supports recursive downloading, making it ideal for mirroring websites or downloading large sets of files. It automatically saves files to disk.
Command to encode and decode something in base64
# Encode echo <Type someting you want to encode> | base64 eg. echo myencode | base64 #input Output- bXllbmNvZGUK #Encoded Output # Decode echo <Enter base64 encoded value> | base64 --decode eg. echo bXllbmNvZGUK | base64 --decode #input Output- myencode #Decoded Output

