๐ Beyond Basics: Using Advanced Linux ๐ง Commands
Part 1

๐ Quick Intro ๐ฅ
In this blog, I will explain advanced commands related to:-
Searching files
Text editor
File compression and archiving commands
Redirection
Environment variables
๐ Search Files ๐
To search files there are two commands
find&locatefindis most commonly used, because it gives the latest datalocatea bit faster thanfind, because it searches in locate DB only not the entire file system, locate DB has to be synced with the file system after every change to get the latest data. [so it is avoided]find <path> -name <file>--> Search file in given path using file name & return path of file, if file exists
find <path> -name <pattern>--> Search file in given path using pattern & return path of a file, if file exists
\==> If you see above image, we can search file by extension or starting file name
find <path> -empty--> Search empty files or directories in the given path
find <path> -perm XXX--> Search files or directories that match given permission
find <path> -type <d/f>--> Search files by type files or directories
NOTE:- You can combine different flags based on requirement
find <path> <other-flags> -exec <command> {} \;--> Execute some command on the output of the other flags used


๐ Text Editor ๐
- There are two most used editors
viandsed
Vi editor
vi file--> Open file in the editor [create a new file if doesn't exist]vi -r file--> Open file in read-only modThere are two modes in
vieditor๐ด Command mode
vieditor is opened in command mode by defaultThis mode allows us to use shortcuts
dd--> Delete line where cursor is presentd<n>--> Delete next n+1 lines including line where cursor is presentu--> undok--> move up by one linej--> move down by one linel--> move right by one characterh--> move left by one character$--> positions cursor to end of line0--> positions cursor to beginning of line<n>G--> go to nth linew--> positions cursor to next word first letterb--> positions cursor to previous word first letter(--> positions cursor to start of file)--> positions cursor to end of file/word--> search for word [n for next search, N for previous search]
๐ด Insert mode
To get into command mode from insert mode press Esc
:wq--> save and exit file:q--> exit file without doing change:q!--> exist file without saving changes:n--> position cursor to nth line
Stream editor
It shows updated on terminal only, wont update source file by default

sed 's/old/new/' <file>--> display output by changing only first occurrence of word in each line
sed 's/old/new/n' <file>--> display output by changing nth occurrence of word in each line
sed 's/old/new/g' <file>: display output by changing all occurrences of word in each line
sed '<n>d' <file>: display output by deleting nth line
sed '$d' <file>: display output by deleting last line
sed '<x>,<y>d' <file>: display output by deleting lines between [x,y]
Note :- all the changes are not saved by default, you need to use -i after sed to save changes
๐ File Compression and Archiving ๐๏ธ
Archiving files is all about combining multiple files and directories into a single file, for easier portability and storage [size doesn't change]
Compressing files means it reduces the size of files and directories by removing unnecessary things and bundling in a single file
There are two commands that are used most
tarandzipto archive and compress files๐ฉFLAGS
c--> Create a new archive filex--> Extract archive filev--> Verbosef--> Specify file namez--> gzip compression [.tar.gz]tar cvf <output> <files-to-be-archived>--> Archive files into a single file
tar xvf <output> <files-to-be-archived>--> Extract files from the archive file
tar tvf <tar-file>--> View files in tar file with details same as inls -l
tar cvfz <output>.tar.gz <files-to-be-compressed>--> Archive and compress files using gzip compression, it will create .tar.gz output compressed file
tar xvfz <output>.tar.gz--> Extract compressed file using gzip compression
\==> zip is not an in-built command, need to install it
sudo apt-get install zipzip <output-file>.zip <files-to-be-compressed>--> Compress multiple files into a single file
๐ฉ FLAGS
-d--> Remove file from zip file-u--> Replace file with updated one in the zip file or add a new file-x--> Exclude files that are to be skipped during compression-r--> Include all files and sub-directories-m--> After zipping, delete the directory or file


unzip <zip-file>--> Unzip files from .zip file
unzip -l <zip-file>--> Display files in .zip file
๐ Redirection โช๏ธ
| Command | Description |
| cmd < file | Input of cmd is taken from file. |
| cmd > file | Standard output (stdout) of cmd is redirected to file. |
| cmd 2> file | Error output (stderr) of cmd is redirected to file. |
| cmd 2>&1 | stderr is redirected to the same place as stdout. |
| cmd1 <(cmd2) | Output of cmd2 is used as the input file for cmd1. |
| cmd > /dev/null | Discards the stdout of cmd by sending it to the null device. |
| cmd &> file | Every output of cmd is redirected to file. |
| cmd 1>&2 | stdout is redirected to the same place as stderr. |
| cmd >> file | Appends the stdout of cmd to file. |
๐ Environment Variables โ
Environment variables are used to store configuration settings, system information, & other variables that can be accessed by processes and shell scripts.
Already existing environment variables can be seen using
envorprintenv
export VAR=Value--> Create a new environment variable [will last only till logout]echo $VAR--> Print value of environment variableunset VAR--> Remove environment variable
export -p--> show all exported environment variables
Happy Learning!!! ๐




