[EnglishFrontPage] [TitleIndex] [WordIndex

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

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

1.3. DNS - Domain Name Service

1.4. LDAP - Lightweight Directory Access Protocol

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.

5. How do I make a lock file in a script?

This is covered in Bash FAQ #45 and ProcessManagement.


CategoryUnix


2012-07-01 04:11