Differences between revisions 2 and 7 (spanning 5 versions)
Revision 2 as of 2007-03-15 14:25:12
Size: 3788
Editor: avk84-1-82-243-5-59
Comment: Tarax's suggestions for host and dig usage
Revision 7 as of 2009-12-24 02:41:04
Size: 5734
Editor: GreyCat
Comment: more on dig; mention rsync and rdiff-backup
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
This FAQ answers frequent questions related to the Unix operating system. If you have questions related to the ["BASH"] shell, the BashFaq is a better place to look. This FAQ answers frequent questions related to the Unix operating system. If you have questions related to the [[BASH]] shell, the [[BashFAQ]] is a better place to look.
Line 7: Line 7:
[[TableOfContents]] <<TableOfContents>>
Line 9: Line 9:
[[Anchor(faq1)]] <<Anchor(faq1)>>
Line 11: Line 11:
Depending on '''where''' the (host, ip_address) relationship is stored, different ways of resolving a host name to an ip address (or vice versa) exist. The most common ones are /etc/hosts, NIS, and DNS. As if that wasn't enough, many systems use more than one method, usually configured in the file /etc/nsswitch.conf. Depending on '''where''' the (host, ip_address) relationship is stored, different ways of resolving a host name to an IP address (or vice versa) exist. The most common ones are /etc/hosts, NIS, and DNS. As if that wasn't enough, many systems use more than one method, usually configured in the file /etc/nsswitch.conf.
Line 42: Line 42:
  ''This should probably be using `ypmatch` instead of scanning the entire output of `ypcat`, but since I don't have any NIS systems configured to do this, I can't test it myself.''
Line 54: Line 55:
 TODO: how to use {{{host}}} or {{{dig}}}
Line 62: Line 61:
      dig a +noall cerbere +answer | cut -f6 | sort | uniq       dig a +noall "$1" +answer | cut -f6 | sort | uniq
Line 64: Line 63:
  (You may have to change `-f6` to `-f5` in some versions of `dig`.)

  Newer versions of `dig` allow this:
   {{{
      dig +short a "$1" | grep -v '\.$'
   }}}
  The `grep` here is to discard any CNAME records that show up.
Line 74: Line 80:
[[Anchor(faq2)]] <<Anchor(faq2)>>
Line 90: Line 96:
[[Anchor(faq3)]] If you're copying across a network, this is an indispensable technique:

{{{
    cd /the/source
    tar cf - dir1 dir2 ... | ssh user@host "cd /the/dest && tar xvf -"
}}}

Antique Unix systems could achieve a similar effect using `rsh` (or `remsh` on some platforms) instead of `ssh`. `rsh` is ''extremely'' deprecated, but if you're stuck on such an old platform, you ''can'' use it in an emergency. Just be sure to undo your rhosts stuff after you're done.

Some systems may have [[http://www.samba.org/rsync/|rsync]] and/or [[http://rdiff-backup.nongnu.org/|rdiff-backup]] installed. These are extremely powerful alternatives to the `tar` over `ssh` pipeline.

''TODO: pax(1) alternatives to tar/cpio''

<<Anchor(faq3)>>
Line 92: Line 111:
Either Safest way:
Line 104: Line 123:
The "--" argument is a standard way to denote the end of the command line options. The "--" argument is a standard way to denote the end of the command line options for virtually all POSIX utilities.
Line 106: Line 125:
[[Anchor(faq4)]] <<Anchor(faq4)>>
Line 108: Line 127:
{{{time}}} is a special command that writes to standard error (file descriptor 2). It's not possible to directly redirect its output to a file, e.g. This is complicated by the fact that there are multiple implementations of `time` as a command. It might be a standalone program (e.g. `/usr/bin/time`) or it might be a shell keyword with special features.

In Bash, `time` is a special keyword that executes a pipeline (or a simple command) and writes timing information to standard error (file descriptor 2). It's not possible to directly redirect its output to a file, because the redirections are applied to the pipeline or command that's being timed. If you are attempting to save `time` output to a file in Bash, please see [[BashFAQ/032|Bash FAQ #32]].
Line 111: Line 132:
    # Bash -- this doesn't work
Line 114: Line 136:
would only redirect the {{{ls}}} standard error output, not the {{{time}}} output. A solution is to either call {{{time}}} in a subshell with redirected output, e.g. If your `time` command is a standalone program, then it will not have the special powers of a shell keyword. Redirections will apply to `time` rather than to the command being timed (which must be a simple command, not a compound one or a pipeline).
Line 117: Line 139:
    sh -c "time ls" 2>time.out     ~$ /usr/bin/time sleep 1 2>time.out
    ~$ cat time.out
    0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+174minor)pagefaults 0swaps
Line 120: Line 145:
or use braces: If you want to capture standalone `time` output in a variable, see [[BashFAQ/002|Bash FAQ #2]].
Line 122: Line 147:
{{{
    { time ls; } 2>time.out
}}}
----
CategoryUnix

Unix Frequently Asked Questions

This FAQ answers frequent questions related to the Unix operating system. If you have questions related to the BASH shell, the BashFAQ is a better place to look.

1. How can I get the IP address for a host name?

Depending on where the (host, ip_address) relationship is stored, different ways of resolving a host name to an IP address (or vice versa) exist. The most common ones are /etc/hosts, NIS, and DNS. As if that wasn't enough, many systems use more than one method, usually configured in the file /etc/nsswitch.conf.

In the easiest case the system has the getent command, which will use /etc/hosts, NIS, DNS, or whatever name resolution was configured in the right sequence:

    $ getent hosts www.shelldorado.com
    127.0.0.1   www.shelldorado.com

If we don't have this very useful command (_XXX is there an OpenSource version available?_), we are on our own and have to access the respective database ourselves. Note that we don't show here how to find out which name resolutions are configured, or in which sequence they are used.

1.1. /etc/hosts

  •     # lookup hostname using /etc/hosts
        lookup_files () { # usage: lookup_files hostname
            awk '{ for ( i=2; i<=NF; ++i ) if ( $i == "'"$1"'" ) print $1 }' /etc/hosts
        }

1.2. NIS - Network Information System, also known as Yellow Pages

  •     # lookup hostname using NIS
        lookup_nis () {
            ypcat hosts |
                awk '{ for ( i=2; i<=NF; ++i ) if ( $i == "'"$1"'" ) print $1 }'
        }
    Note: the use of NIS to store hostname-to-IP mappings is deprecated.
    • This should probably be using ypmatch instead of scanning the entire output of ypcat, but since I don't have any NIS systems configured to do this, I can't test it myself.

1.3. DNS - Domain Name Service

  •     # lookup hostname using DNS
        lookup_dns () {
            nslookup "$1" 2>/dev/null |
                awk '{ cnt [$1]++; val [cnt [$1] $1] = $2 }
                        END { print val ["2Address:"] }'
        }
    • Suggestions by Tarax: with bind9-host and dig, following pipelines seem to work quite well. Just replace the nslookup/awk instructions above with one of them... and thank you for this page :-)

      •       host "$1" | cut -d" " -f4 | sort | uniq
              dig a +noall "$1" +answer | cut -f6 | sort | uniq

      (You may have to change -f6 to -f5 in some versions of dig.)

      Newer versions of dig allow this:

      •       dig +short a "$1" | grep -v '\.$'

      The grep here is to discard any CNAME records that show up.

1.4. LDAP - Lightweight Directory Access Protocol

  •     # lookup hostname using LDAP
        lookup_ldap () {
           # TODO: add query
        }

2. How can I copy a directory with all files and subdirectories?

    src=/home/$USER         # source
    dst=/tmp/backup         # destination

    cd "$src"
    find . -print | cpio -pdmv "$dst"

This method can be used to receate a directory hierarchy, without files, too:

    find . -type d -print | cpio -pdmv "$dst"

If you're copying across a network, this is an indispensable technique:

    cd /the/source
    tar cf - dir1 dir2 ... | ssh user@host "cd /the/dest && tar xvf -"

Antique Unix systems could achieve a similar effect using rsh (or remsh on some platforms) instead of ssh. rsh is extremely deprecated, but if you're stuck on such an old platform, you can use it in an emergency. Just be sure to undo your rhosts stuff after you're done.

Some systems may have rsync and/or rdiff-backup installed. These are extremely powerful alternatives to the tar over ssh pipeline.

TODO: pax(1) alternatives to tar/cpio

3. How can I remove a file with a name starting with '-'?

Safest way:

    rm ./-filename

(./ is the name of the current directory). Another way (for newer Unix systems):

    rm -- -filename

The "--" argument is a standard way to denote the end of the command line options for virtually all POSIX utilities.

4. How can I redirect the output of the time(1) command?

This is complicated by the fact that there are multiple implementations of time as a command. It might be a standalone program (e.g. /usr/bin/time) or it might be a shell keyword with special features.

In Bash, time is a special keyword that executes a pipeline (or a simple command) and writes timing information to standard error (file descriptor 2). It's not possible to directly redirect its output to a file, because the redirections are applied to the pipeline or command that's being timed. If you are attempting to save time output to a file in Bash, please see Bash FAQ #32.

    # Bash -- this doesn't work
    time ls 2>time.out

If your time command is a standalone program, then it will not have the special powers of a shell keyword. Redirections will apply to time rather than to the command being timed (which must be a simple command, not a compound one or a pipeline).

    ~$ /usr/bin/time sleep 1 2>time.out
    ~$ cat time.out
    0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+174minor)pagefaults 0swaps

If you want to capture standalone time output in a variable, see Bash FAQ #2.


CategoryUnix

UnixFaq (last edited 2014-10-06 11:25:54 by 46)