Linux Programmer | RHCE | RHCSA

Search This Blog

Linux CLI Tools

'Find' command:

List all files from all subfolders in linux

find . -type f -printf "%T@ %p\n" | sort -nr | cut -d\  -f2-

List all subfolders

find /dir -type d

"-bash: /usr/bin/find: Argument list too long" error while listing and removing files.

Solution:

First navigate to folder where you want to delete files, do’t use “ls”, “ls -la” as it can take lot of time before it will show result, so just make sure your in correct directory before running this command as it will delete all files in that directory.

find . -type f -exec rm -v {} \;

Command will find all files one by one and will delete it.

If you want to delete only specific files for example only .jpg files

find . -maxdepth 1 -name "*.jpg" -print0 | xargs -0 rm

It will search files with .jpg extension in current directory and will delete it one by one.

 

'sed' command usage

Remove Text between two patterns.

For example,
content of filename.txt is,
2020-12-04 15:06:23,917 - [Mobile Service] [42.108.171.121]

result we want is,
2020-12-04 15:06:23 [Mobile Service] [42.108.171.121]
 
sed 's/,[^[+]*\[/\ [/' filename.txt 
 

Read complete line in 'for' loop with  spaces

IFS=$'\n'       # make newlines the only separator
for j in $(cat ./file_wget_med)    
do
    echo "$j"
done
# Note: IFS needs to be reset to default!
 

Add string after a certain string in the same line in a text file

sed "0,/members/{s/\bmembers */&$host, /}" printer.cfg

awk approach:
awk -v h=$host '!f && /members /{ $2=sprintf("%8s, %s",h,$2); f=1 }1' printer.cfg

Replace , with space 

sed -e 's/,/ /g' 
tr , \\n

Add line after some line

sed -i 'client_script="purval" /a clientscript=hello' {filename}

Remove empty space from file

sed -i '/^[[:space:]]*s/d' {filename}

Add Word At End of The Line of Matched Pattern

sed -i '/^all:/ s/$/ purval/' {file_name}

Append line at the end of the file using sed

sed -i '' -e '$a\testing of adding new line at the end' file
 
Append line in specific location with using sed 
 
sed '1 a\appended line' your-file (1 is line number)

append line into file which contains spaces before starting of line

text="\ \ autohide=0" 
sed -i "6 i \ \ ${text}" panel

Replace start of each line with Five Space 

sed -i -e 's/^/ /' panel

delete lines from searched pattern

sed -i '/<context name="Root">/,+12d' filename

delete perticular line from from file ( with line numer ) 

sed -i '9d' {filename.txt}

command for kill zombie process

sudo kill -HUP $(ps -A -ostat,ppid | grep -e '[zZ]'| awk '{ print $2 }')

Delete Lines Between Two line Numbers

sed -i '4,10d' filename.txt

in case of delete from variable
 
sed -i ''$first','$last'd' filename.txt

Delete Lines Between Two Searched pattern

sed '/PATTERN-1/,/PATTERN-2/d' input.txt

Remove specific character in every line 

sed -i 's/a/ /' filename

Remove first character from every line 

sed -i 's/^./ /' filename

 Remove last character from every line

sed -i 's/$./ /' filename

Remove First and Lase character of every line

sed 's/.//;s/.$//' filename

Remove First Character only,if it is a Specific character 

sed 's/^F//' file

Remove Last character only if it is specific character 

sed 's/x$//' file

Remove First three characters from every line

sed 's/...//' file

Remove First n characters from every line

 sed -r 's/.{4}//' file

Remove last n character of every line

sed -r 's/.{3}$//' 

Networking commands 

List all IP in connected network

arp-scan --interface=eth0 --localnet

Get primary ip address


ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

OR

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
OR

hostname -I
 
Configure VPN macid(dummy macid)

/sbin/ip li add purval type dummy

/sbin/ifconfig -a
 
Provide all permission to all users 

chmod -R g+s dirname

setfacl -Rm g:users:rwX,d:g:users:rwX dirname


Replace "-" with whitespace

echo "abc-pqr-lmn" | tr ' ' '-' 

Network Information :

1. get DNS:
nmcli dev list | grep -i IP4 | grep -i DNS | awk '{print$2}'

2. get IP:

hostname -I
ifoconfig

3. get Gateway:

nmcli dev list | grep -i IP4

4.Network manager tool

nm-tool

output is something like..

NetworkManager Tool

State: connected (global)


- Device: eth0  [Wired connection 1] -------------------------------------------
  Type:              Wired
  Driver:            r8169
  State:             connected
  Default:           yes
  HW Address:        40:8D:5C:FD:BA:16

  Capabilities:
    Carrier Detect:  yes
    Speed:           1000 Mb/s

  Wired Properties
    Carrier:         on

  IPv4 Settings:
    Address:         192.168.1.196
    Prefix:          24 (255.255.255.0)
    Gateway:         192.168.1.1

    DNS:             192.168.1.1


5. Check the currunt connection :

nmcli con status |  grep -i yes | head -1
 

Fastest Ping between some range of ip's 

fping command 

fping -s -g 10.10.0.1 10.10.0.21 -r 1

 Internet is Not accessible after connect VPN in windows Machine

  • Open Windows Power Shell
  • Enter following commands
                     Get-VpnConnection
                     Set-VpnConnection "VPN Connection" -SplitTunneling $TRUE
  • Restart the Machine  
 

Check port is open or not ?

 telnet myserver.com $port

  • connection timeout" if the port is blocked by the firewall
  • "Connection refused" if the service is down/not listening on specified port, but port is reachable.
  • "connected to server_ip" if connection is successful

netstat -plnt | grep ':25'
 ss -lntu | grep ':25'
nmap -sT -O localhost | grep 25
lsof -i:25   

List All open Ports in Linux

cat /etc/services

ss -lntu (list all network ports using ss command)

netstat -lntu (list all network ports using netstat command)

Other commands

Print uniq records by matching second column

cat filename | sort -u -k2

Increment version number with command line

echo "4.8.60" | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}'

Install as Non-interactive mode

sudo DEBIAN_FRONTEND=noninteractive apt-get -y install [packagename]

Add user into some group

usermod -a -G group_name user_name

Remove User From Group

gpasswd -d user_name group_name

List The installed Services

initctl list

Link For Download HPLIP (in case of hp Printers not working )

https://drive.google.com/open?id=0BwPBGrPmbppAZi10eTJRcko1MjA

Hour wise total logs count:

grep -i "mobileservice" localhost_access_log.2023-07-09.txt | awk '{split($4,a,":"); print a[2]}' | cut -d: -f1 | sort | uniq -c

To Encrypt Shell Script

shc -T -r -f script name

To Extract From Rightside using cut command

  echo "abc-def pqr-jkl" | cut -d ' ' -f-1 

output:
abc-def

Create Symlink in /etc/rc2.d

sudo update-rc.d service enable

Remove Ctrl+m Problem is Shell Script

tr -d '\b\r'

To Control Window

wmctrl -l

Assign permission automatically after copy

cp --no-preserve=all file somedir/

Mount windows share

mount -t cifs -o username=username,password=password,rw,file_mode=0777,dir_mode=0777 //192.168.1.99/public /opt/mount

Display Window share in linux

smbclient -L 192.168.1.3 -U username

add new line at place of '\'

tr '/' '\n'

combine all rows in one line

paste -d'|' -s

remove All files except file.txt

rm !(file.txt)

add line before some pattern

awk '/TV_SCRIPT_DIR=/{print "Add Word"}1' {file-name}

Install inotify-send this package needs to be install

apt-get install libnotify-bin

Reinstall the package

sudo apt-get install --reinstall aptitude

Reboot Wine server :

wineserver -k

Python-six error occoured during apt-get 

/usr/share/python/dist/python-six file is not there
- after replacing this file from another system the python is working now
- sudo apt-get install --reinstall python-six

Mount USB in Linux Server OS

1. sudo fdisk -l
2. get the correct USB partition name from the list for e.g. /dev/sdb1
3. mount /dev/sda1 /mnt/

in case of unmount 

1. umount /mnt/ 

Create tar of Root (/)

tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /

Compress and Decompress using .bz2 file in linux

apt-get install bzip2
bzip2 -zk filename.tar

Extract ISO Content using 7zip command

sudo apt-get install p7zip-full p7zip-rar
7z x iso_name.iso

Extract ISO Content using isoinfo command

isoinfo -i osname.iso

Remove Special characters from one line (for eg. //192.168.1.76 remove forward slash from this )

cat file-name | head -1 | awk '{print$2}' | cut -d ':' -f2 | sed 's/^.//' | sed 's/^.//'

Get Different lines from two files:

comm -3 <(sort /tmp/.mem | uniq) <(sort /tmp/.all | uniq) | awk '{print$1}' 

Print All the Lines till next search pattern  is found

cat file-name | sed -n '/purval/,/patel/p' 

(this will start to print lines from purval and till next patel is display it will display the lines)

See The Full time of File creation

ls -lt --full-time filename/or/file/path 

Create Virtual display in linux which is not visible to others:

sudo apt-get install xvfb
Xvfb :101 -screen 0 1x1x8 &

this will create 101 number display virtually

Set default applications

  - Always open .doc file in Kingsoft office bty default, and open .xlsx file in libreoffice by default

- list of default applications is stored into /usr/share/applications/default.list ( system wide)


- which is also available as a user wide


nano ~/.local/share/applications/mimeapps.list


[Default Applications]
application/excel=libreoffice-calc.desktop
application/wps-office.doc=wps-office-wps.desktop
application/wps-office.xlsx=libreoffice-calc.desktop

 

Get List of IP's from Network With their macid's

apt-get install arp-scan
sudo arp-scan --interface=eth0 --localnet

/var/log/auth.log is not working or Empty

check your permissions of /var/log/auth.log file 

  chmod 0644 /var/log/auth.log

 
  chown syslog:adm /var/log/auth.log

get the full user name in process list

ps axo user:50,pid,cmd

xrdp_client folder is mounted on RDP login  users  Desktop

umount the fuse mount point
umount /sys/fs/fuse/connections

Remove device file
rm /dev/fuse

unload and load module, this will trigger udev
rmmod fuse
modprobe fuse
 
restart xrdp now.

find the max value of column 1 and print respective record from column 2 from file

 file.txt 

157.50.15.61
117.247.186.42
117.247.186.42
117.247.186.42
117.247.186.42
117.247.186.42
117.247.186.42
command: 
cat file.txt | sort -r | uniq -c | awk -v max=0 '{if($1>max){want=$2; max=$1}}END{print want} ' 
output:
117.247.186.42

Change Date format :

login_time="Fri Jan 12 10:09:21 IST 2018"
date_format=`date --date="$login_time" +%H:%M:%S`  OR +%d%m%y OR +%s 
output will be: 10:09:21
%s will print timestamp

if want to find the older time from the time list then use the below command
date --date="$timet" +%s
print all timestamps using this command and then simply find the higher value from this

Sort IP's

$ cat addresses.txt
129.95.30.40
5.24.69.2
19.20.203.5
1.2.3.4
19.20.21.22
5.220.100.50
cat addresses.txt sort -V
OR

sort -n addresses.txt 

OR

sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 addresses.txt

1.2.3.4
5.220.100.50
5.24.69.2
19.20.203.5
19.20.21.22
129.95.30.40

 

Pass Port number with ssh command

sshpass -p $PASSWORD scp -P $PORT -o StrictHostKeyChecking=no "$filepath" $USER@"$IP":$PATH
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p$PORT $USER@"$ip" "bash -s" -- < "$filepath"
ssh -o StrictHostKeyChecking=no -CX -p$PORT "$USER"@localhost $commandline

How to know OS Installation date in linux

 fs=$(df / | tail -1 | cut -f1 -d' ') && tune2fs -l $fs | grep created 

Filesystem created:       Thu Feb  1 20:37:38 2018 

Add Content of file on some specific line to another file.

sed -i '2r file1.txt' file2.txt

Error While Running python file.

- i have installed python-qt4 package for the python-qt program.

sudo apt-get install python-qt4

my sample code is written as below,
layout.py

import sys
from PyQt4 import QtGui

def window():
   app = QtGui.QApplication(sys.argv)
   w = QtGui.QWidget()

   b = QtGui.QPushButton(w)
   b.setText("Hello World!")
   b.move(50,20)

   w.setGeometry(10,10,300,200)
   w.setWindowTitle("PyQt")
   w.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   window()

Trying to run the program,
$ python layout.py

it is showing an error,
SyntaxError: Non-ASCII character '\xe2' in file layout.py on line 14, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Solution:
# need to change character encoding to UTF-8.

add two lines on the start of the program,

#!/usr/bin/python
# # coding: utf-8

And it works !!

Rsync rsync on remote server. 

with using port-

rsync -avz -e "ssh -p $port" /file/to/path $user@{$server-ip}: 
 
without port -
 
rsync -avz /file/to/path $user@{$server-ip}:


Allow FTP service in iptables and activate FTP
iptables -I INPUT 4 -m tcp -p tcp -m conntrack --ctstate NEW --dport 21 -j ACCEPT
modprobe ip_conntrack_ftp

Update Mlocate Database

sudo updatedb

OpenSSL Error

openssl: /usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by openssl)
 
Solution: 
apt-get install libpcre3-dev libssl-dev perl make build-essential curl

List files which have two or more dots(.)

Suppose we have files,
 
filename.min.js, fileis.test.txt, file1.txt, file2.txt

if we want to list file names which have two or more dots(.)
 
command will be, 
ls |  grep -P '^[^.]+\.([^.]+\.)+[^.]*$'
 
 
Get size of only for directories
 
sudo du -sh ./*/

Convert .ppk file to .pem file.

1. Download putty
    apt-get install putty

2. Then apply below commands for converting .ppk to .pem
    puttygen your-file.ppk -O private-openssh -o your-file.pem

3. change permissions
    chown 400 your-file.pem

4. Then try to login with SSH
     ssh -i your-file.pem user@server-ip
 
 
Disable suspend and Hibernation in linux
 
disable following targets at systemd level,
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
 
Run the following command to apply the changes,
sudo systemctl restart systemd-logind.service
 
Reboot the system for this to take affect, and check the status using,
sudo systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target 

Re-enablethe feature using,
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

 

No comments:

Post a Comment

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...