Andries filmer

Feel free with Open Source Software

Andries Filmer - Internet professional sinds 1996.
Ik ben groot voorstander van Free- en Opensource Software (FOSS) en laat deze site jouw overtuigen waarom dit goed is.
Home Over deze website Kennisbank Ezelsoren Freelancer Online gereedschap

Bash

Index
  1. Oneliners
    1. File change commands
    2. Rename files
    3. Find
    4. Dig
    5. MAIL
    6. Manupulate numbers
    7. Analyse logs
  2. SSH copy public key
  3. .bashrc
  4. Tcpdump
  5. Hardware information
  6. Distribution information
  7. Show Linux System Information
  8. Network information
  9. Comments

Oneliners

Kill many processes
One's I started hylafax with deamontools and it started many procceses. To kill all processes of hfaxd the next command

 ps -acx | grep hfaxd | awk '{print $1}' | xargs kill

Remove many file in a directorie
When there are many thousand of files in a directory you can't remove them with 'rm *'. But the next command can.

 cd directorie; for f in * ; do rm -rf $f ; done

Zip a file and emails them to someone

 gzip -c FILENAME |uuencode FILENAME.gz | mail -s "SUBJECT" someone@domain.nl

Testing the load/response on a website

 for i in `jot 5000` ; do wget --save-headers http://www.filmer.nl/ ; done

Space report

 du -s * | sort -nr > $HOME/space_report.txt

File change commands

Changing Permissions Recursively. Be sure to be in the right directorie, be gareful !!!!. Gives write permissons for files and directories for group

 find ./www/ -type f -exec chmod 664 {} \;
 find ./www/ -type d -exec chmod 775 {} \;
 find ./www/ -exec chown nobody:users {} \;

Change all the phpfiles with the content 'huisNrToe' to 'huis_nr_toe'

 find ./dirname -name "*.php" | xargs perl -pi -e 's/huisNrToe/huis_nr_toe/g'

The 'i' option does a case-insensitive pattern matching (there much are more options).

 find ./dirname -name "*.php" | xargs perl -pi -e 's/huisNrToe/huis_nr_toe/gi'

Add something to the end of each line in a file:

 cat filename.txt | sed 's/$/something/g' > newfile.txt

Reformat DOS text files to Unix ones (the ^M chars)

 tr -d '\r' < dos-text-file > unix-file

or

 perl -i.bak -npe 's/\r\n/\n/g' file ...

Remove all text between the Quotes (").

 perl -p -i.bak -e 's/\"(.*)\"//g' example.txt

Rename files

Heres how to add a t_ to all the .jpg files in the current directory:
(Remember to remove the "echo" if you like the results.)

 for i in *jpg ;do echo mv -i -- "./$i" "./t_${i}" ;done

If that looks good.. then remove the "echo" to make the changes permanent.
(Remember to remove the "echo" if you like the results.)

Heres how to change those t_ files to tn_ files:

 for i in t_* ;do echo mv $i `echo $i |sed 's/^t_/tn_/'` ;done

Remove a t_ from the files starting with t_

 for i in t_*jpg ;do j=`echo $i |sed 's/t_//'` ; echo mv -i -- "./$i" "./$j" ;done

Change all the .JPG files to .jpg
(Remember to remove the "echo" if you like the results.)

 for i in *JPG ;do j="`echo ./$i |sed 's/JPG$/jpg/'`" ;mv -i -- "./$i" "./$j" ;done

Rename spaces in filenames to _
Example: pretty girl.jpg to pretty_girl.jpg
(Remember to remove the "echo" if you like the results.)

 for i in *\* ;do j="`echo ./$i |sed 's/JPG$/jpg/'`" ;mv -i -- "./$i" "./$j" ;done

Find

There are many many ways to use find to find files, here a few exaples

Count files in a directory

 find . | wc -l

Let's see how many of these files have a .old extension:

 find . -name "*.old" -print | wc -l

Let's look at a second way to delete these files, this time using xargs:

 find . -name "*.old" -print0 | xargs -0 rm

Find files in hour homedir who have been modified one day (24 hours) ago. Use -atime for accesstime

 find ~ -type f -mtime 1 -ls | less

Find files older than -x (-1) days created and show this with date and time

 find /directorie/ -type f -ctime -1 -ls | awk '{print $8 "\t" $9 "\t" $10 "\t" $11 " (" $7 ")" }'

Seach all the *.txt files in the current directory tree (recursive) for a certain word or phrase within the files:

 find . -type f -name '*.txt' -print0 | xargs -0 grep -r -i 'some.*thing'

Find files with 'sometext' who a not in de Sent Items map.

 find . -type f \! -regex '.*/.Sent*.*' -print0 | xargs -0 grep -r -i 'sometext'

Remove files older then 3 days

 find /tmp/mydir/ -type f \! -atime -3d -print0 | xargs -0 rm

Find and remove small files

 find /backup/tarballs/ -type f -exec ls -s {} \; | sort -n  | head -10 | xargs -0 rm

If you want to find all your writable directories, issue:

 find / -perm -0777 -type d -ls

Find all media outside 'Photo Library' directory

 find /path/to/media -name '*iPhoto Library*' -prune -o -iregex ".*\(jpg\|dv\|avi\|mp3\)$" | less

Dig

Ask dns info from a domain

 dig domainname.nl

Ask hostname from ipnr

 dig -x ipnr

MAIL

Ask mailexchange records from dns (ask default nameserver).

 dig domainname.nl mx

Ask other nameserver

 dig domainname.nl mx @nameserver-andere-provider.nl

Check the response of a mailserver

 telnet domainname.nl 25

Check to see how many SMTP connections are made from single ipnrs

 cat /var/log/qmail-smtpd/current | grep 'pid [0-9]* from ' | awk '{ print $6}' | sort | uniq -c | sort -n

Check to see how many mail is in queue for a (group by) domain.

 /var/qmail/bin/qmail-qread | grep remote | awk -F '@' '{ print $2 }' | sort | uniq -c | sort -n

Number of incoming mails per domain

 cat /var/log/qmail-send/current | grep 'starting delivery' | awk '{print $9}' | sed -r 's/^[^@]*@//' | sort | uniq -c | sort -n

Manupulate numbers

Make the phone/faxnumbers unique and remove 06,084 and 087 numbers Also remove the DOS returns (^M)

 sort -u numbers.txt | sed '/^06/d' | sed '/^084/d' | sed '/^087/d' | tr -d '\r' > numbers2.txt

Randomize the numbers from a text file.

 perl -e 'print map {$_->[1]} sort {$a->[0]<=>$b->[0]} map {[rand,$_]} <>' numbers2.txt > numbers3.txt

Analyse logs

Which website uses most hits

 ls -al /var/log/hostlog | awk '{ print $5 " \t" $8}' | sort -g

Which user has succesfully logged in on "Jun 04"?

 cat /var/log/proftpd/proftpd.log | grep "Jun 04" | grep USER |  grep Login | awk ' {print $8 " " $9 }' | sort -u

SSH copy public key

 cat ~/.ssh/id_dsa.pub | ssh you@other-host 'cat >> ~/.ssh/authorized_keys'

Show the hits from the logfile

 cut -f1 -d ' ' /var/log/httpd-access.log | sort | uniq -c > hits.txt

Say you have a file:row that is setup like "username|adres|domain"
That file consits of 3 fields (providing there are no spaces in a field!)
So say you wanted to create a new file with ONLY the email address, you could do:

 cat filename.txt | awk '{print $1'@'$3}' > newfile.txt

.bashrc

History back and forward. My first action on a unix/linux sytem.

 bind '"\e[A"':history-search-backward
 bind '"\e[B"':history-search-forward

Tcpdump

Don't convert addresses

 tcpdump -ni eth0 'port 22'

To print all packets arriving at or departing from sundown:

 tcpdump host hostname.filmer.nl

Print all tcp traffic not from filmer.xs4all.nl and stockholm

 tcpdump not host \(filmer.xs4all.nl or stockholm\)

To print traffic between helios and either hot or ace:

 tcpdump host helios and \( hot or ace \)

To print all IP packets between ace and any host except helios:

 tcpdump ip host ace and not helios

To print all traffic between local hosts and hosts at Berkeley:

 tcpdump net ucb-ether

To print all ftp traffic through internet gateway snup: (note that the expression is quoted to prevent the shell from (mis-)interpreting the parentheses).

 tcpdump 'gateway snup and (port ftp or ftp-data)'

To print traffic neither sourced from nor destined for local hosts (if you gateway to one other net, this stuff should never make it onto your local net).

 tcpdump ip and not net localnet

To print the start and end packets (the SYN and FIN packets) of each TCP conversation that involves a non-local host.

 tcpdump 'tcp[13] & 3 != 0 and not src and dst net localnet'

To print IP packets longer than 576 bytes sent through gateway snup:

 tcpdump 'gateway snup and ip[2:2] > 576'

To print IP broadcast or multicast packets that were not sent via ethernet broadcast or multicast:

 tcpdump 'ether[0] & 1 = 0 and ip[16] >= 224'

To print all ICMP packets that are not echo requests/replies (i.e., not ping packets):

 tcpdump 'icmp[0] != 8 and icmp[0] != 0'

Hardware information

To get de information about the hardware dmidecode (DMI table decoder) is a handy tool. To get al infromation:

 sudo dmidecode 

To get information about '''Base Board,Processor,Memory Module,Cache,Memory Device ,Memory Device'''.

 sudo dmidecode 2,4,6,7,17

There are more specification defines the following DMI numbers. See man pages.

Distribution information

 cat /etc/*release

or more nice with LSB (Linux Standard Base)

 lsb_release -a

Show Linux System Information

Use uname command to show Linux system information
 uname -a

Network information

Network speed
 mii-tool -v

This page is created on 2009-11-24 and updated on 2012-04-03

I appreciate if you give some comment about this page. Please go ahead.
Your e-mailaddress will not be published it is only to contact you (if needed).

 
Your name
Your e-mailaddress
To prefent robots to use this form I ask you kindly to type the next characters in the input field.
 

 


Mijn Curriculum vitae | De content op deze website heeft de Creativecommons 3.0 licentie | © 2011
Andries Filmer | http://andries.filmer.nl | andries@filmer.nl | © 2011
Deze website wordt gerealiseerd met Free- en Open Source Software: | | | | | |