Unix/Usefull Commands

From S23Wiki
Jump to: navigation, search

just a quick dump of some usefull unix commands

List the top ten largest files

du -sk * | sort -rn | head -10


nslookup will return infomation about domain names.


nslookup -d {server}


host will return infomation about domain names.

host domain name

-t querytype

                Allows you to specify a particular querytype of information
                to be looked up.  The arguments are defined in the man page
                for named(8).  Currently-supported types include: ``a,
                ``ns, ``md, ``mf, ``cname, ``soa, ``mb, ``mg,
                ``mr, ``null, ``wks, ``ptr, ``hinfo, ``minfo,
                ``mx, ``uinfo, ``uid, ``gid, ``unspec. Additional-
                ly, the wildcard, which may be written as either ``any or
                ``*, can be used to specify any (all) of the above types.
                Types must be given in lower case.  Note that the default is
                to look first for ``a, and then ``mx, except that if the
                verbose option is turned on, the default is only ``a. The
                ``-t option is particularly useful for filtering informa-
                tion returned by host; see the explanation of the ``-l op-
                tion, below, for more information.


Dig gets infomations from DNS

dig @{servers NS} {server} [axfr|MX|NS|any]

axfr is a full zone tranfeer

any will retreave every thing it can

example of finding out about a domain name

$ host -t NS microsoft.com
microsoft.com name server ns3.msft.net.
microsoft.com name server ns4.msft.net.
microsoft.com name server ns5.msft.net.
microsoft.com name server ns1.msft.net.
microsoft.com name server ns2.msft.net.

$ dig @ns3.msft.net microsoft.com any

; <<>> DiG 9.3.2 <<>> @ns3.msft.net microsoft.com any
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 259
;; flags: qr aa rd; QUERY: 1, ANSWER: 12, AUTHORITY: 0, ADDITIONAL: 5

;microsoft.com.                 IN      ANY

microsoft.com.          3600    IN      A
microsoft.com.          3600    IN      A
microsoft.com.          172800  IN      NS      ns4.msft.net.
microsoft.com.          172800  IN      NS      ns5.msft.net.
microsoft.com.          172800  IN      NS      ns1.msft.net.
microsoft.com.          172800  IN      NS      ns2.msft.net.
microsoft.com.          172800  IN      NS      ns3.msft.net.
microsoft.com.          3600    IN      SOA     dns.cp.msft.net. msnhst.microsoft.com. 2007010701 300 600 2419200 3600
microsoft.com.          3600    IN      MX      10 mailb.microsoft.com.
microsoft.com.          3600    IN      MX      10 mailc.microsoft.com.
microsoft.com.          3600    IN      MX      10 maila.microsoft.com.
microsoft.com.          3600    IN      TXT     "v=spf1 mx include:_spf-a.microsoft.com include:_spf-b.microsoft.com include:_spf-c.microsoft.com include:_spf-ssg.microsoft.com ~all"

ns4.msft.net.           3600    IN      A
ns5.msft.net.           3600    IN      A
ns1.msft.net.           3600    IN      A
ns2.msft.net.           3600    IN      A
ns3.msft.net.           3600    IN      A

;; Query time: 12 msec
;; WHEN: Mon Jan  8 04:25:11 2007
;; MSG SIZE  rcvd: 502

Ill try to add info about waht all this means.

List processes and ports:-

ps -e -o pid= | xargs -i pfiles {} | xargs -i perl -e ' if
(@ARGV[0]=~/^\d+.*$|port/){if (@ARGV[0]!~/mode/) {print @ARGV[0]."\n"}}' "{}"

Rename files with 'tr':-

for i in *.xml; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

Recursively chgrp:-

find . -group 501 -exec chgrp 500 {} \;

Grep recursively ASCII files only:-

find . -type f -print -exec file {} \; | grep ASCII | awk -F: '{print $1}'
| xargs -i grep --with-filename searchstring {}

Sum memory usage of process:-

ps -aux | grep java | awk '{sum = sum + $5} END {print sum}'

Copying files from a remote machine without scp:-

ssh <user>@<remote> "cat /foo/bar/random.tar.gz" | cat >/local/dir/random.tar.gz

Move Selected Files with find and tar

This command is quite nice if you want to move file/dir's around a system preserving there directory tree and permissions. I have made this to output to a temporary file first that you can then check on and run after. (you could of course remove the 'echo' and the move would happen right away) The 'E' flag in the first tar is a Solaris specific command and will need to be removed if you are running on Linux. (it is used to work with files larger then 8G and be more precise about time stamps)

this fist example will just copy the files/dir's:

for x in $(find . -type d -name "[1234].[0-9]*" -prune) ; do echo "tar cpEf - ${x} | (cd archive/ && tar xfp -)" ; done > /var/tmp/TEMP-ARCHIVE-COPY

this second example will delete the files/dir's after the copy:

for x in $(find . -type d -name "[1234].[0-9]*" -prune) ; do echo "tar cpEf - ${x} | (cd archive/ && tar xfp - && cd .. && rm -rf ${x} )" ; done > /var/tmp/TEMP-ARCHIVE-MOVE

After running the above you can then less the output 'vi /var/tmp/TEMP-ARCHIVE-COPY', to check only the right files/dir's have been found, then run the file 'bash /var/tmp/TEMP-ARCHIVE-COPY'