![]() |
Feel free with Open Source SoftwareAndries Filmer - Internet professional sinds 1996.
|
|
|
|
BashOneliners
Kill many processes
ps -acx | grep hfaxd | awk '{print $1}' | xargs kill
Remove many file in a directorie
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 commandsChanging 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:
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. 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
for i in *JPG ;do j="`echo ./$i |sed 's/JPG$/jpg/'`" ;mv -i -- "./$i" "./$j" ;done
Rename spaces in filenames to _
for i in *\* ;do j="`echo ./$i |sed 's/JPG$/jpg/'`" ;mv -i -- "./$i" "./$j" ;done
FindThere are many many ways to use find to find files, here a few exaplesCount 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
DigAsk dns info from a domain
dig domainname.nl Ask hostname from ipnr
dig -x ipnr
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 numbersMake 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 logsWhich 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"
cat filename.txt | awk '{print $1'@'$3}' > newfile.txt
.bashrcHistory back and forward. My first action on a unix/linux sytem.
bind '"\e[A"':history-search-backward bind '"\e[B"':history-search-forward
TcpdumpDon'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 informationTo 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 informationcat /etc/*release or more nice with LSB (Linux Standard Base)
lsb_release -a
Show Linux System InformationUse uname command to show Linux system informationuname -a
Network informationNetwork speedmii-tool -v
I appreciate if you give some comment about this page. Please go ahead. |
|
Andries Filmer | http://andries.filmer.nl | andries@filmer.nl | © 2011
|