Friday, February 24, 2017

Linux commands and tricks



1. Runing the last command as Root
sudo !!

2. To find your external IP address.
host myip.opendns.com resolver1.opendns.com
lynx --dump http://ipecho.net/plain
curl ifconfig.me

4. Auto-empty any file without removing it
> file.txt

5. Execute command without saving it in the history
<space>command

6. Slick way to copy or backup a file before you edit it.
cp filename{,.bak}

7. Traceroute is a nice command but how about a single network diagnostic tool that can combine traceroute with ping? mtr is your command.
mtr efytimes.com

8. To Clear your terminal's screen
ctrl-l

9. List of commands you use most often
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

10. Saving the file you edited in vim/vi without the required permissions
:w !sudo tee %
------------------------------------------------------
1. List all the files that are in current and sub directories

$ find
.
./abc.txt
./subdir
./subdir/how.php
./cool.php

You can also use:

$ find .
$ find . -print

2. Search through a particular directory or path

Check for files in test directory and sub directories.

$ find ./test
./test
./test/abc.txt
./test/subdir
./test/subdir/how.php
./test/cool.php

Alternatively, if you want to search by name, then use:

$ find ./test -name abc.txt
./test/abc.txt

You could easily make a mistake and end up searching through the entire file system. So be careful and use Ctrl+C if you happen to do this mistake.

3. Limit how many levels the find command should go

When traversing directories, you can choose how many levels the find command should go within directories.

$ find ./test -maxdepth 2 -name *.php
./test/subdir/how.php
./test/cool.php

$ find ./test -maxdepth 1 -name *.php
./test/cool.php

This is useful when you want to do a limited search in a directory and not the entire directory.

4. Invert match

If you know what files to exclude from your search or want to get filed following a certain pattern, use this.

$ find ./test -not -name *.php
./test
./test/abc.txt
./test/subdir

Here, it will exclude files with .php extensions.

5. Combine multiple criteria for search

You can also put in multiple criteria in some cases.

$ find ./test -name 'abc*' ! -name '*.php'
./test/abc.txt
./test/abc

This shows you files that have abc in their name and do not have .php extension.

You can also use the OR operator by using the '-o' switch.

$ find -name '*.php' -o -name '*.txt'
./abc.txt
./subdir/how.php
./abc.php
./cool.php

This will show you files that have .php or .txt extensions.

6. Search only files or only directories

You can also search for only files or only directories.

For files

$ find ./test -type f -name abc*
./test/abc.txt

For directories

$ find ./test -type d -name abc*
./test/abc

7. Search through more than one directories together

Searching inside two separate directories.

$ find ./test ./dir2 -type f -name abc*
./test/abc.txt
./dir2/abcdefg.txt

8. Find files that are hidden

If you mention the period, then you can easily find all hidden files. In Linux, all hidden files have a period in their name.

$ find ~ -type f -name ".*"

9. Find files with specific permissions

If you need to find files that have specific permissions.

$ find . -type f -perm 0664
./abc.txt
./subdir/how.php
./abc.php
./cool.php

10. Find files that are read only

This one finds you all of them.

$ find /etc -maxdepth 1 -perm /u=r
/etc
/etc/thunderbird
/etc/brltty
/etc/dkms
/etc/phpmyadmin
... output truncated ... 

tar command

The following command creates a new tar archive:

$ tar cvf archive_name.tar dirname/

Use this when you need to extract from an existing archive:

$ tar xvf archive_name.tar

This is the command that is used to view a tar archive:

$ tar tvf archive_name.tar

grep command

This command searches for a given string within a file:

$ grep -i "the" demo_file

This command prints a matched line and three lines after it:

$ grep -A 3 -i "example" demo_text

Recursively search for a string in all files:

$ grep -r "ramesh" *

find command

Use this to find files when the filename is known:

# find -iname "MyCProgram.c"

This command is used to execute command on files that have been found using find:

$ find -iname "MyCProgram.c" -exec md5sum {} \;

Empty files in the directory:

# find ~ -empty

ssh command
This command allows you to login to a remote host.
# ssh -l jsmith remotehost.example.com

Use this to debug ssh clients:
# ssh -v -l jsmith remotehost.example.com

For displaying the ssh client version:
$ ssh -V

sed command
Convert the DOS file format into Unix format:
$sed 's/.$//' filename

Print the contents of a file in reverse order:
$ sed -n '1!G;h;$p' thegeekstuff.txt

Add a line number for the non-empty lines in a particular file
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

awk command
Remove duplicate lines:
$ awk '!($0 in array) { array[$0]; print }' temp

Print all lines from a file , which have the same uid and gid:
$awk -F ':' '$3==$4' passwd.txt

Printing specific fields from a particular file:
$ awk '{print $2,$5;}' employee.txt

vim command
Go to the file�s 143rd line.
$ vim +143 filename.txt

Go to the first found match of the file specified:
$ vim +/search-term filename.txt

diff command
Ignoring white spaces when comparing files:
# diff -w name_list.txt name_list_new.txt

sort command
Ascending order:
$ sort names.txt

Descending order:
$ sort -r names.txt

Sort a file (passwd) the third field:
$ sort -t: -k 3n /etc/passwd | more

export command
Use this for viewing oracle related environment variables:
$ export | grep ORACLE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"

Export environment variable:
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
------------------------------------------
Tar files from multiple directories to a single tar file
cat listoffiles.txt | xargs tar czvf yourbackupname.tgz - or
tar zcvf yourbackupname.tgz $(cat listoffiles.txt)
cat filePath_2004.txt | xargs cp -t compressedfiles/filePath_2004/
--------------------

perl -pi -e 's/\[PDRrr_v3]/\[PDR_v3_pilot]/g' *
grep -rinl "\[PDR_v3_pilot" *
-----------------------

tar command
The following command creates a new tar archive:
$ tar cvf archive_name.tar dirname/

Use this when you need to extract from an existing archive:
$ tar xvf archive_name.tar

This is the command that is used to view a tar archive:
$ tar tvf archive_name.tar

grep command
This command searches for a given string within a file:
$ grep -i "the" demo_file

This command prints a matched line and three lines after it:
$ grep -A 3 -i "example" demo_text

Recursively search for a string in all files:
$ grep -r "ramesh" *

find command
Use this to find files when the filename is known:
# find -iname "MyCProgram.c"

This command is used to execute command on files that have been found using find:
$ find -iname "MyCProgram.c" -exec md5sum {} \;

Empty files in the directory:
# find ~ -empty

ssh command
This command allows you to login to a remote host.
ssh -l jsmith remotehost.example.com

Use this to debug ssh clients:
ssh -v -l jsmith remotehost.example.com

For displaying the ssh client version:
$ ssh �V

sed command
Convert the DOS file format into Unix format:
$sed 's/.$//' filename

Print the contents of a file in reverse order:
$ sed -n '1!G;h;$p' thegeekstuff.txt

Add a line number for the non-empty lines in a particular file
$ sed '/./=' thegeekstuff.txt | sed 'N; s/\n/ /'

awk command
Remove duplicate lines:
$ awk '!($0 in array) { array[$0]; print }' temp

Print all lines from a file , which have the same uid and gid:
$awk -F ':' '$3==$4' passwd.txt

Printing specific fields from a particular file:
$ awk '{print $2,$5;}' employee.txt

vim command
Go to the file's 143rd line.
$ vim +143 filename.txt

Go to the first found match of the file specified:
$ vim +/search-term filename.txt

diff command
Ignoring white spaces when comparing files:
# diff -w name_list.txt name_list_new.txt

sort command
Ascending order:
$ sort names.txt

Descending order:
$ sort -r names.txt

Sort a file (passwd) the third field:
$ sort -t: -k 3n /etc/passwd | more

export command
Use this for viewing oracle related environment variables:
$ export | grep ORACLE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"

Export environment variable:
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

--------------------------
Random password generator
-------------------------
# egrep -ioam1 '[a-z0-9]{10}' /dev/urandom

# sh -x ./configure ... configure_options ... 

network copy with ssh and tar
# ssh bsmith@apple tar cf - -C /home/bsmith . | tar xvf - 

download only with yum command
# yum update httpd -y --downloadonly --downloaddir=/opt
# rpm -Uivh *.rpm

Undo your changes even after quitting the VIM editor
:set undofile
:set undodir=/tmp

This is to be done every time you start editing a file. In case you need the configuration to be there for all files that you open in VIM, create a file called '.exrc' or '.vimrc' in $HOME directory. In my case, it is /myhome.
Open the just created file and add the following commands:
# vi /myhome/.exrc
set undofile
set undodir=/tmp
Save and close the file.
:wq
From now onwards, the Undo history is maintained in the background for all files that you edit with VIM. 


check php version of webserver from browser
phpinfo.php
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>

++
\\\\\\\\\
library dependency missing error
-----------------
# yum clean all   
# yum clean metadata
# yum list all   
# yum grouplist


check you external ip address
# lynx http://whatismyip.com

check port 443
# netstat -anp|findstr 443


To check and flush postfix mail queue:
to check
# mailq
to flush queue
# postfix flush
to delete queue  
# postsuper -d ALL

To check configure error/debug
# sh -x ./configure

Starting apache fails (could not bind to address 0.0.0.0:80)
# fuser -k -n tcp 80


Check CPU usage from command line:
# top -b -n 1 | grep "Cpu(s)\:"
# ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
# sar
# mpstat


Search and replace text in multiple files in a directory:
#perl -pi -e 's/PDR_v3/PDRrr_v3/g' *
#find . -name "*.*" -print | xargs sed -i 's/PDR_v3/PDR_v3_qa/g'
#grep -rl 'PDR_v3' ./ | xargs sed -i 's/PDR_v3/PDR_v3_qa/g'


To find repeated words/lines in a text file
# cat /var/httpd/logs/web-access_log | sort | uniq -c | sort -nr | grep '11/Jul' | less


device eth0 does not seem to be present/
# rm -f /etc/udev/rules.d/70-persistent-net.rules 
# reboot

Delete files older than 60 days
# find . -mtime +60 | xargs rm

No comments:

Post a Comment