Monday, May 18, 2020

Linux tips

Number of files

 ls -l filename* | wc -l

Command to print directory structure 
ls -R MY_FOLDER | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//──/g' -e 's/─/├/' -e '$s/├/└/'

List only directories
ls -l | grep '^d'

Count total number of files in particular directory with specific extension.

ls -lR /path/to/dir/*.jpg | wc -l

Max thread, we can use. or number CPU.

lscpu


GREP

-Search multiple strings
grep '1\|2\|3' filename

-Number of lines with specific pattern search
grep -o 'STRING' filename | wc -l


SED
Replace values in file,
sed -i 's/old_string/new_string/' filename

With special character simple quotes
sed -i "s/old_string/'new_string'/g" filename

Add header string in the file.
sed -i '1s/^/addedtext\n/' filename

Add footer string in the file
echo "helloworld" | tee -a filename >/dev/null

Change extension filename in loop

for f in filename.txt;do mv $f  $f.sql;done

Split larger files into smaller parts 

-l number of lines

split -l 500000 filename

--bytes
K
M
G

Execute next the command on terminal example:

split --bytes=1G path/your_file.extension path/your_new_file.extension

Convert file UTF-8

iconv -f ISO-8859-1 -t UTF-8//TRANSLIT filein.txt > fileout.txt

Check special character on file 

grep --color='auto' -P -n "[\x80-\xFF]" file.txt

Could not get lock /var/lib/dpkg/lock-frontend - open

Delete the lock file:

sudo rm /var/lib/apt/lists/lock

Delete the lock file in the cache directory

sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock

Generate RSA private and public key

Private key:

openssl genrsa -out private.pem 2048


Public Key from RSA private key:


openssl rsa -in private.pem -outform PEM -pubout -out public.pem

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!


Solution: run next command

ssh-keygen -R  YOUR_IP





No comments:

Post a Comment

Provisioning Cloud SQL with Private Service Connect Using Terraform & Accessing from Cloud Run with Spring Boot

In this post, we'll explore how to provision Cloud SQL instances with Private Service Connect (PSC) connectivity using Terraform and the...