Unix commands

DNS lookupEver wondered who is behind a domain name? Dig might help you:

dig www.dnsduerinteresserti.etalleranet soa

eksample:

dig www.uib.no soa

yum og rpm

Easy install packages on Linux with yum and rpm.

Examples:

Find what packages are ready for updating:

yum check-update

List out the whole rpm database, and find those who have something with php:

rpm -qa | grep -i 'php'

screen

Did you loose your shell, you said!? Do you need more shell sessions?

Here is the scenario: you are logged into a server over a crappy internet link. You do some advanced stuff, and in the middle of a very important job, you loose the connection, and everything is lost!

Ach ja! and then you can start all over again.

Well, with ‘screen’ you can avoid this. Don’t loose your work anymore and use ‘screen’!

Some reading material:

Short version here. Start screen with:

screen

Open a new windows (shell) in screen:

CTRL-A c

Go back and forward from window to window:

CTRL-A n
CTRL-A p

(n=next, p=previous)

Disconnect:

CTRL-A d

(d=deattach)

Find a screen session:

screen -ls

Reconnect to existing screen session:

screen -r 31633.ttyp2.hosten

sed commando

If you need to replace words or characters in files, use sed!

sed -i 's/word/replacement/g' index.htm
  • where “/” is the chosen separator sign
  • where “word” is the word we like to replace, and “replacment” is the new word.
  • index.htm is the file that contains the text

Example with other separator characters:

sed -i 's;^c:;/mnt/drive_c;g' pathfile.ini && sed -i 's:\:/:g' pathfile.ini

This will change c:\windows\sys32 to /mnt/drive_c/windows/sys32

find commando

Have thousands of files on your linux server? Find is the most powerful tool you can ever use on a Linux server!

If you like to find all files with extension .htm:

find / -type f -name '*.htm'

You can then add the -exec option to do some more interesting stuff.

Example:

“Find all files from / where file extension is .htm and then run ‘ls -al’:

The command:

find / -type f -name '*.htm' -exec ls -al {} \;

“Find all files from /var/www/html where the word ‘password’ is present.”

Unix/Linux command:

find /var/www/html -type f -name '*' | xargs grep -i 'password'

Find all lines with ServerName or ServerAlias

Some times you like to go through all the configuration files in Apache to find registered servernames and serveraliases. You can do this with this command:

find /etc/httpd -type f -name '*.conf' -exec cat {} \; | egrep -i "servername|serveralias" \
| grep -v '^#.*' | sed -e 's/^[ \t]*//' | sort | uniq

which is doing something like:

Find all files in /etc/httpd with the name ‘something.conf’, print out (cat) the content of these files to your screen, then grab all lines with ‘servername’ or ‘serveralias’, not case-sensitiv, remove all lines that start with a ‘#’, then remove all empty lines. At the end, remove dublicate lines.

Search for words in PDF files:

If you have 100 pdf files, and you want to find which files contains a specific word, for instance: “technologies”, you can do that with this command:

 set -f; find . -name '*.pdf' -print0 | while IFS= read -r -d '' file; \
 do echo $file; pdftotext "$file" - | grep -i 'teknologier'; done

The comand lists alle the filenames, and under the lines which contains the requested word.

find and sed together

If you have X number of files, in several folder, and you need to replace on word with another, you can do this with:

find /path/to/folder type -f -iname 'filnavn' -exec sed -i 's/oldword/newword/g' {} \;

Example:

 find . type f -iname '*.htm*' -exec sed -i 's/\/radioweb\/teaching/\/teaching/g' {} \;

Leave a Reply

Your email address will not be published. Required fields are marked *