Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Apr 18, 2022

sed examples

Ref: https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/

sed 's/unix/linux/' test.txt   # Replacing or substituting string (sed command replaces the first occurrence of the pattern in each line and it)

sed 's/unix/linux/2' test.txt  # Replacing the nth occurrence of a pattern in a line

sed 's/unix/linux/g' test.txt  # Replace all occurances in a line as well

sed 's/unix/linux/3g' test.txt # Replace nth till last (3rd, 4th, 5th etc.,)

echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

sed '3 s/unix/linux/' test.txt # restrict the sed command to replace the string on a specific line number.

sed 's/unix/linux/p' test.txt  # The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

sed -n 's/unix/linux/p' test.txt # print only replaced lines, n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

sed '1,3 s/unix/linux/' test.txt # Replace range of lines

sed '5d' test1.txt  # delete 5th line

sed '$d' test1.txt  # delete last line

sed '3,6d' test1.txt # delete from range x to y

sed '12,$d' test1.txt # delete from nth line till last

sed '/abc/d' test1.txt # delete line which matches pattern


# Replace & update same file (-i means --in-place)
sed -i 's/ABC/ABCDEF/g' sed_test.txt

# Replace & update same file (-i means --in-place), -ibak takes original file as backup (sed_test.txtbak)
sed -ibak 's/ABC/ABCDEF/g' sed_test.txt


Jun 19, 2021

systemctl chkconfig - Enable/Disable service on boot


chkconfig --list httpd

chkconfig --list varnish

(or)

systemctl list-unit-files | grep httpd

systemctl list-unit-files | grep varnish



systemctl is-active httpd

systemctl is-active varnish


Enable on boot

  • chkconfig httpd on

Disable on boot

  • chkconfig httpd off

Apr 21, 2018

View number of requests by time from Apache access log

View number of requests by time from Apache access log
  • Overall requests in an hour
    • grep "18/Apr/2018:11" /var/log/httpd/access_log | wc -l
  • Overall requests in a minute
    • grep "18/Apr/2018:11:05" /var/log/httpd/access_log | wc -l
  • Overall requests in a minute
    • grep "18/Apr/2018:11:05:10" /var/log/httpd/access_log | wc -l
  • Overall requests by sec in an hour (group by sec)
    • grep "18/Apr/2018:11" /var/log/httpd/access_log | cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":"$3}' | sort -nk1 -nk2 | uniq -c | awk '{ if ($1 > 10) print $0}'

Find Sum of RSS memory in ps command in linux/unix

Find Sum of RSS memory in ps command in linux/unix
  • In KB
    • ps aux | awk 'BEGIN {sum=0} {sum +=$6} END {print sum}'
  • In MB
    • ps aux | awk 'BEGIN {sum=0} {sum +=$6} END {print sum/1024}'
  • In GB
    • ps aux | awk 'BEGIN {sum=0} {sum +=$6} END {print sum/1024/1024}'

How to find free memory available
  • cat /proc/meminfo
  • using free command
    • In KB
      • free
    • In MB
      • free -m
    • In GB
      • free -g 
I observed
  • Sum of RSS memory in ps less than memory actually used
  • Total used memory a lot higher than sum of RSS
  • Reason
    • The Linux kernel will use available memory for disk caching

Dec 5, 2012

Unix How to Find a Particular Directory


We know how to find a file in Unix directories using

This will search sample.txt in current directory

#]find . -iname sampe.txt


But if we want to find out a particular directory in the unix, type -d denotes search for directory

#] find / -name mysql -type d
/var/log/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/etc/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/share/mysql


In order to narrow down the search, you could mention exact path (/etc) where you want to search

#] find /etc -name mysql -type d
/etc/mysql



Unix For Loop


#]mkdir for_test_dir1

#]mkdir for_test_dir2

#]mkdir for_test_dir3

#]cat for_test_dir1/for_test.txt
content 1
content 1
content 1

#]cat for_test_dir2/for_test.txt
content 2
content 2
content 2

#]cat for_test_dir3/for_test.txt
content 3
content 3
content 3
...
...

#] ls
for_test_dir1 for_test_dir2 for_test_dir3 for_test_dir4 for_test_dir5
......
......


#]foreach var (`ls`)
foreach? ls -ltr $var/for_test*
foreach? end

O/P:
It will list all the for_test files present in for_test_dir's

Dec 1, 2012

pushd popd example

When you have  to work with navigate different directories quickly, its difficult to remember the whole path or type the whole thing again and again.

There is concept in Linux/Unix to manipulate directory stack, you can use directory stack to push directories into it and later pop directory from the stack.

1) dirs : Display the current directory stack without pushing or popping a new directory

2) pushd : Push directory into the Stack

3) popd : Pop directory from the Stack

Using pushd and popd effectively can help you get around your command-line environment quickly


E.g.,

]# mkdir /usr/local/dir1

]# mkdir /usr/local/dir2

]# mkdir /usr/local/dir3

]# dir
~

]# pushd /usr/local/dir1

]# dir
/usr/local/dir1   ~


]# pushd /usr/local/dir2


]# dir
/usr/local/dir2   /usr/local/dir1   ~



]# pushd /usr/local/dir3

]# dir
/usr/local/dir3   /usr/local/dir2   /usr/local/dir1   ~


]# popd

]# pwd
/usr/local/dir3

]# dir
/usr/local/dir2   /usr/local/dir1   ~


]# popd

]# pwd
/usr/local/dir2

 ]# dir
/usr/local/dir1   ~


]# popd


]# pwd
/usr/local/dir1

 ]# dir
~


Nov 28, 2012

How to find the size of a directory in linux

We all know "du" command gives the size of the files, but it not convenient to read or understand the size as "du" command gives the size in KB's

In order to  make is human readable format, please follow as mentioned below


du -lsh <directory_name>

du -lsh <file_name>

-h => display the size in human readable format

e.g.,
>> du  -lsh  prabhath_movies
5GB  prabhath_movies

>> du  -lsh  hero.avi
1GB   hero.avi


Thanks for going through this article, Have a good day :-)