Entries Tagged 'Site updates' ↓

man - view manual pages for Unix commands

Hi! If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

man is one of the very first Unix commands everyone learns. It shows a brief manual for most of the commands available on your Unix-like OS and provides cross-references to other similar manuals.

What is man command used for

Many Unix commands are quite useful even if run without command line parameters, but as soon as you reach the next comfort level, you start wondering about extracting even more from the same command. This is when reading manuals becomes vital, and man offers a great way to explore them.

All Unix-like systems are provided with extensive manuals. Even if you don't have the material printed on paper, you still have plenty of information installed with your OS.

Most usually, manual pages are found under /usr/share/man directory. Manuals are not presented in clear text - there's a markup language commands which should be interpreted by the man command.

What is a Unix manual page (manpage)

All the manuals for Unix commands are split into clearly marked sections:

  • NAME - command name as it should be typed
  • SYNOPSIS - syntax for running a command - all the possible command line options
  • DESCRIPTION - textual description of what a command is used for
  • OPTIONS - full list of command line options with thorough explanations
  • FILES - files which are used by a command
  • SEE ALSO - other relevant commands you might want to look at
  • BUGS - known bugs and limitations of a command
  • AUTHOR - list of command authors, developers and most current maintainers

A manual page generated by man command is nothing but a clear text you can access from most basic Unix shell session, formatted as per your terminal capabilities (most often you see that section names are shown in bold font, but that's about it).

Using man command in Unix

The simplest way to get help in Unix is to run man command and specify a name of another Unix command as a parameter. Naturally, the first thing you should do is to have man show you a manual for itself:

ubuntu$ man man

This is how it would look (click the image o get the full resolution):

See also:

locate - quickly find files in Linux

Today I'd like to show you one more option you have when searching for files in Linux. If you have a locate tool installed, you'll be able to find any file almost instantly.

How does locate command work?

locate uses a pretty simple principle - instead of going through your filesystem directory tree every time you need a certain file found, it consults a database which stores locations of most files in your system. The locate database (locatedb) is updated nightly with a separate command. The update occurs during night hours when peak usage of your system is very unlikely, but this means that using such a database through the day will provide instant results.

Continue reading →

file - show the type of a file in Unix

file is one of the basic Unix commands which helps you confirm exactly what kind of files you're working with. Using a special database of signatures for various types of files, it reads the first few bytes of a specified file and shows you whether it matches one of the signatures, thus confirming the file type.

file command: basic usage

The simplest way to use the file command is to run it and specify one or more file names as command line parameters. The file type description you expect will be given in plan English, showing you whether a file is a script or an executable, an ASCII text or a binary data:

rhel$ file /etc/passwd
rhel$ file /bin/csh
rhel$ file /bin/tcsh
/bin/tcsh: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), stripped

Examples of file types in Linux

Playing with the file command is fun. You can identify all sorts of files with this little command, but it's important to be able to read its output correctly.

Below I give you just a few most common file types examples, taken from a Red Hat Enterprise System (RHEL).

This is how a typical shared library will be identified:

rhel$ file /lib/libc-2.3.2.so
/lib/libc-2.3.2.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), not stripped

Note the 32-bit which confirms the bit-ness of this file and the Intel 80386 which confirms the architecture.

That's the same library in 64bit:

rhel$ file /lib64/libc-2.3.2.so
/lib64/libc-2.3.2.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped

See how 32-bit is now changed to 64-bit, and AMD x86-64 shows the new architecture for it?

Binaries will look somewhat different, although the bit-ness and architecture will still be shown:

rhel$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), stripped

In this example, the important field is executable, which confirms this is a file you can run (vs previous example with shared objects which you can't run directly)

Examples of file types in SPARC Solaris

Just to show you that Unix file formats aren't this different, here's a few examples from SPARC Solaris 10 system.

First, a library - in 32bit and 64 bit forms:

sparc$ file /lib/libc.so
/lib/libc.so:   ELF 32-bit MSB dynamic lib SPARC32PLUS Version 1, V8+ Required, dynamically linked, not stripped, no debugging information available
sparc$ file /lib/64/libc.so
/lib/64/libc.so:        ELF 64-bit MSB dynamic lib SPARCV9 Version 1, dynamically linked, not stripped, no debugging information available

Observe how bit-ness is still shown where you'd expect to see it, as well as SPARC-based architecture (SPARC32PLUS for 32bit and SPARCV9 for 64-bit).

Here's how a kernel driver will look:

sparc$ file /kernel/drv/sparcv9/zfs
/kernel/drv/sparcv9/zfs:        ELF 64-bit MSB relocatable SPARCV9 Version 1

And that's how a typical SPARC Solaris binary is identified:

sparc$ file /bin/ls
/bin/ls:        ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped

See also:

basename - strip directory and suffix from a full path to a file

basename is a very simple but useful Unix command which is used mostly in shell scripting. Taking a full path to a file as a parameter, it then stips the directory names from it and returns you just the name of a file.

Note: basename is a string stripping command - it simply returns you the last portion of a provided full path. It does not verify whether this portion is an actual file.

How to use basename command

This is how basename is invoked:

ubuntu$ basename /etc/network/if-down.d/postfix
postfix

basename in shell scripting

An example of using the basename command is shown below. This command line takes the input from a standard output from the find command and outputs only the names of the files. While there's not much practicality in doing this, it's a great way to show how basename works.

Here's the standard find output:

ubuntu$ find /etc/network/
/etc/network/
/etc/network/if-down.d
/etc/network/if-down.d/wpasupplicant
/etc/network/if-down.d/postfix
/etc/network/if-post-down.d
/etc/network/if-post-down.d/wpasupplicant
/etc/network/if-post-down.d/wireless-tools
/etc/network/if-pre-up.d
/etc/network/if-pre-up.d/wpasupplicant
/etc/network/if-pre-up.d/wireless-tools
/etc/network/if-up.d
/etc/network/if-up.d/wpasupplicant
/etc/network/if-up.d/mountnfs
/etc/network/if-up.d/ntp
/etc/network/if-up.d/ntpdate
/etc/network/if-up.d/postfix
/etc/network/interfaces

And this is how only the filenames are extracted. Note, how for some parameters you see not the filenames, but directory names like if-down.d, simply because they're the last part of a string returned by the find output:

ubuntu$ for f in `find /etc/network/`; do basename $f; done;
network
if-down.d
wpasupplicant
postfix
if-post-down.d
wpasupplicant
wireless-tools
if-pre-up.d
wpasupplicant
wireless-tools
if-up.d
wpasupplicant
mountnfs
ntp
ntpdate
postfix
interfaces

See also:

  • dirname - extract the directory name from a full path
  • cd - change directory
  • pwd - confirm the current directory you’re in

dirname - extract the directory name from a full path

dirname is a very simple but useful Unix command which is used mostly in shell scripting. Taking a full path to a file as a parameter, it then stips the filename off and returns you just the directory name.

How to use dirname command

This is how dirname is invoked:

ubuntu$ dirname /etc/network/if-down.d/postfix
/etc/network/if-down.d

dirname in shell scripting

The obvious use of dirname command is shown below. This script, based on the full path to a file, prints the parent directory name and a list of all the files in this directory:

#!/bin/bash
FILE=$1
DIR=`dirname $FILE`
FILES=`ls $DIR`

echo "Filename specified: $FILE"
echo "Parent directory: $DIR"

for f in $FILES; do
echo "- $f"
done

See also:

  • basename - strip directory and suffix from a full path to a fil
  • cd - change directory
  • pwd - confirm the current directory you’re in

			

rmdir - remove empty directories

rmdir is one of the basic unix commands, which serves a sole purpose of removing empty directories. You may need this kind of functionality to attempt removing directories, and be sure that any files still existing in them will be safe. For removing any directory irregardless of any files in it, you should use the rm command.

To demonstrate how rmdir works, let's create two directories and two files in them:

ubuntu$ mkdir -p /tmp/dir1/dir2
ubuntu$ touch /tmp/dir1/file1
ubuntu$ touch /tmp/dir1/dir2/file2

This gives us the following file and directory structure:

ubuntu$ find /tmp/dir1
/tmp/dir1
/tmp/dir1/dir2
/tmp/dir1/dir2/file2
/tmp/dir1/file1

So, we have /tmp/dir1 directory, which contains a file1 file and a dir2 directory. /tmp/dir1/dir2 contains another file, called file2.

Basic rmdir usage

The simplest way to remove an empty directory is to run rmdir and supply the desired directory name as a command line parameter:

ubuntu$ rmdir /tmp/dir1/dir2
rmdir: `/tmp/dir1/dir2': Directory not empty

In our example, we got the error because dir2 contains a file2 file, so it cannot be removed until the file exists.

Now, if we remove the file2 in dir2 directory, rmdir will happily destroy dir2 if we try again:

ubuntu$ rm /tmp/dir1/dir2/file1
ubuntu$ rmdir /tmp/dir1/dir2

Cascade directory removal with mkdir

If you want, you can attempt to do a cascade directory removal - if removing a specified directory succeeds, rmdir will try to remove its parent directory, if it's empty, and move up the directory tree until it meets a directory which isn't empty or which your use doesn't have permission to remove.

This kind of directory removal is achieved using rmdir -p option. If we recreate the dir2 directory under dir1 from our initial setup, you can see how rmdir will remove dir2 and then attempt to remove dir1, its parent directory:

ubuntu$ mkdir /tmp/dir1/dir2
ubuntu$ rmdir -p /tmp/dir1/dir2
rmdir: `/tmp/dir1': Directory not empty

Now, the /tmp/dir1 removal is failed because it has file1 file left it it. So if we remove it and recreate the empty dir2, rmdir -p will successfully remove both directories.

First, let's prepare our 2 empty directories:

ubuntu$ rm /tmp/dir1/file1
ubuntu$ mkdir /tmp/dir1/dir2

This is how we double-check it's only dir1 with dir2 subdirectory:

ubuntu$ find /tmp/dir1
/tmp/dir1
/tmp/dir1/dir2

And now let's see how rmdir -p manages to remove both and even attempt to remove /tmp, cause it's the parent of /tmp/dir1:

ubuntu$ rmdir -p /tmp/dir1/dir2
rmdir: `/tmp': Permission denied

As usual, unix find command can testify that there's no /tmp/dir1 directory anymore:

ubuntu$ find /tmp/dir1
find: /tmp/dir1: No such file or directory

See also:

lsb_release - find Linux Standard Base (LSB) information

LSB - Linux Standard Base - is a joint project by a number of Linux vendors to standardize the OS environment. Apart from sharing many common principles in their structure, LSB participating Linux distributions share quite a few commands. lsb_release is one of them, and it allows you to find out all the LSB information about your Linux distribution.

I'm going to use a Ubuntu Feisty (7.04) in my examples.

Continue reading →

Basic Unix System Status Commands

I've just updated the Unix system status commands section on the Basic Unix commands page.

Please have a look if you're interested:

Welcome to Unix Commands!

I'm thrilled to present you with Unix Commands - a sister blog of the Unix Tutorial project.

This website will be an easy-to-navigate index of Unix commands you'll need for everyday use.

So far, I've only got the skeleton of the website up, but will be adding more content regularly.

Here are the main index pages (I'm sure you've seen them on the main page of this website):

Initially, each Unix command will get its own separate page like this one: ls command. If any of the pages get too hard for you to scan through, I'll start splitting them into shorter ones.