stat – display file or file system status

GNU Command  stat -f  / 

$stat -f /
  File: "/"
    ID: bc427fdafb7b0541 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 18864769   Free: 16209639   Available: 15251363
Inodes: Total: 4792320    Free: 4684854
$


Explanation

Display file or file system status.

Related Source Code Exposition

static bool
do_statfs (char const *filename, bool terse, char const *format)
{
  STRUCT_STATVFS statfsbuf;

  if (STATFS (filename, &statfsbuf) != 0)
    {
      error (0, errno, _("cannot read file system information for %s"),
             quote (filename));
      return false;
    }

  if (format == NULL)
    {
      format = (terse
                ? "%n %i %l %t %s %S %b %f %a %c %dn"
                : "  File: "%n"n"
                "    ID: %-8i Namelen: %-7l Type: %Tn"
                "Block size: %-10s Fundamental block size: %Sn"
                "Blocks: Total: %-10b Free: %-10f Available: %an"
                "Inodes: Total: %-10c Free: %dn");
    }

  print_it (format, filename, print_statfs, &statfsbuf);
  return true;
}

Source Code Highlight

Stat the file system and print what we find.

Featured Image
FIXME

Related Knowledge

Due to  shell aliases and built-in  `stat' command, using
an unadorned `stat' interactively  or in a script may get
you  different functionality  than  that described  here.
Invoke  it via  `env' (i.e.,  `env stat  ...')   to avoid
interference from the shell.

vmstat – Report virtual memory statistics

A UNIX Command . vmstat -m 

Cache                       Num  Total   Size  Pages
fat_inode_cache              23     23    688     23
fat_cache                   102    102     40    102
isofs_inode_cache             0      0    1006     12
udf_inode_cache              23     23    712     23
fuse_request                  0      0    632     25
fuse_inode                    0      0    768     21
dm_crypt_io                   0      0    152     26
kcopyd_job                    0      0    368     22
dm_uevent                     0      0   2608     12
dm_rq_target_io               0      0    400     20
ext3_inode_cache          11017  12882    824     19
ext3_xattr                    0      0     88     46
journal_handle              170    170     24    170
journal_head                 36     36    112     36
revoke_table                256    256     16    256
revoke_record               128    128     32    128
UDPLITEv6                     0      0   1024     16
UDPv6                        16     16   1024     16
tw_sock_TCPv6                 0      0    320     12
TCPv6                        17     17   1856     17
cfq_queue                   110    119    240     17
Cache                       Num  Total   Size  Pages
bsg_cmd                       0      0    312     13
mqueue_inode_cache           18     18    896     18
hugetlbfs_inode_cache        13     13    624     13
dquot                         0      0    256     16
dnotify_mark                237    240    136     30
pid_namespace                15     15   2112     15
user_namespace                0      0   1072     15
posix_timers_cache           23     23    176     23
UDP-Lite                      0      0    832     19
ip_fib_hash                  46     46     88     46
arp_cache                    36     72    320     12
RAW                         450    475    832     19
UDP                          19     19    832     19
tw_sock_TCP                  16     16    256     16
TCP                          24     38   1664     19
blkdev_queue                 18     18   1736     18
blkdev_requests              34     44    360     22
bip-256                       7      7   4224      7
bip-128                       0      0   2176     15
bip-64                        0      0   1152     14
bip-16                       21     21    384     21
Cache                       Num  Total   Size  Pages
sock_inode_cache            531    575    704     23
file_lock_cache              22     22    184     22
net_namespace                12     12   2560     12
shmem_inode_cache           708    740    816     20
Acpi-ParseExt              1385   1400     72     56
Acpi-Namespace              816    816     40    102
task_delay_info             263    324    112     36
taskstats                    23     24    328     12
proc_inode_cache           1240   11006    1006     12
sigqueue                     25     25    160     25
bdev_cache                   19     19    832     19
sysfs_dir_cache           16424  110024     80     51
inode_cache                1175   1573    608     13
dentry                     8128   9744    192     21
buffer_head              149614 2010005    104     39
vm_area_struct            13975  14520    184     22
mm_struct                   101    119    960     17
files_cache                 105    115    704     23
signal_cache                143    1100   1088     15
sighand_cache               140    1100   2112     15
task_struct                 252    288   1728     18
Cache                       Num  Total   Size  Pages
anon_vma                   6242   6834     40    102
shared_policy_node         9171  10115     48     85
numa_policy                 170    170     24    170
radix_tree_node           19547  19558    568     14
idr_layer_cache             453    4100    544     15
dma-kmalloc-8192              0      0   8192      4
dma-kmalloc-4096              0      0   4096      8
dma-kmalloc-2048              0      0   2048     16
dma-kmalloc-1024              0      0   1024     16
dma-kmalloc-512              16     16    512     16
dma-kmalloc-256               0      0    256     16
dma-kmalloc-128               0      0    128     32
dma-kmalloc-64                0      0     64     64
dma-kmalloc-32                0      0     32    128
dma-kmalloc-16                0      0     16    256
dma-kmalloc-8                 0      0      8    512
dma-kmalloc-192               0      0    192     21
dma-kmalloc-96                0      0     96     42
kmalloc-8192                 11     12   8192      4
kmalloc-4096                582    616   4096      8
kmalloc-2048                233    272   2048     16
Cache                       Num  Total   Size  Pages
kmalloc-1024                596    1006   1024     16
kmalloc-512                 493    544    512     16
kmalloc-256                 696    816    256     16
kmalloc-128                 790    864    128     32
kmalloc-64                 3047   3840     64     64
kmalloc-32                  777   1024     32    128
kmalloc-16                 2778   2816     16    256
kmalloc-8                  4094   4096      8    512
kmalloc-192                5795   6489    192     21
kmalloc-96                  649    798     96     42
kmem_cache                   42     42    192     21
kmem_cache_node             128    128     64     64


UNIX Explanation

vmstat  reports   information  about  processes,  memory,
paging, block IO, traps, disks and cpu activity.


Related Source Code Exposition


int main(int argc, char *argv[]) {
  char *partition = NULL;
  int c;

  while((c = getopt(argc, argv, "VdafmDnp:S:s")) != EOF) switch(c) {
      case 'V':
	display_version();
	exit(0);
      case 'd':
	statMode |= DISKSTAT;
	break;
      case 'a':
	/* active/inactive mode */
	a_option=1;
        break;
      case 'f':
        // FIXME: check for conflicting args
	fork_format();
        exit(0);
      case 'm':
        statMode |= SLABSTAT;
	break;
      case 'D':
        statMode |= DISKSUMSTAT;
	break;
      case 'n':
	/* print only one header */
	moreheaders=FALSE;
        break;
      case 'p':
        statMode |= PARTITIONSTAT;
	partition = optarg;
	if (memcmp(partition, "/dev/", 5) == 0) partition += 5;
        break;
      case 'S':
	switch(optarg[0]) {
	case 'b': case 'B': dataUnit = UNIT_B; break;
	case 'k': dataUnit = UNIT_k; break;
	case 'K': dataUnit = UNIT_K; break;
	case 'm': dataUnit = UNIT_m; break;
	case 'M': dataUnit = UNIT_M; break;
	default:
	  fprintf(stderr, "-S requires k, K, m or M (default is kb)n");
	  exit(EXIT_FAILURE);
	}
	szDataUnit[0] = optarg[0];
	break;
      case 's':
        statMode |= VMSUMSTAT;
	break;
      default:
	/* no other aguments defined yet. */
	usage();
  }

Source Code Highlight


Using getopt to select -m option for slabinfo.

Featured Image
FIXME


Related Knowledge

Physical  memory  the actual  RAM  installe  is a  finite
resource on any system.  The Linux memory handler manages
the  allocation  of  that  limited  resource  by  freeing
portions of physical memory when possible.

All  processes use  memory, of  course, but  each process
doesn't   need   all  its   allocated   memory  all   the
time. Taking advantage of  this fact, the kernel frees up
physical  memory by  writing some  or all  of  a process'
memory to disk until it's needed again.

The  kernel  uses paging  and  swapping  to perform  this
memory  management. Paging  refers  to writing  portions,
termed  pages, of  a process'  memory to  disk. Swapping,
strictly speaking, refers  to writing the entire process,
not  just part,  to  disk.  In  Linux,  true swapping  is
exceedingly rare, but the terms paging and swapping often
are used interchangeably.

When pages  are written  to disk, the  event is  called a
page-out, and when pages are returned to physical memory,
the event  is called a  page-in.A page fault  occurs when
the  kernel  needs a  page,  finds  it  doesn't exist  in
physical  memory  because  it  has  been  paged-out,  and
re-reads it in from disk.

Page-ins  are common,  normal  and are  not  a cause  for
concern.  For example, when  an application  first starts
up, its  executable image and data are  paged-in. This is
normal behavior.

Page-outs, however,  can be a  sign of trouble.  When the
kernel detects that memory is running low, it attempts to
free  up  memory by  paging  out.Though  this may  happen
briefly from time to time, if page-outs are plentiful and
constant,  the  kernel  can  reach  a  point  where  it's
actually spending more time managing paging activity than
running   the   applications,   and  system   performance
suffers. This woeful state is referred to as thrashing.

Using  swap space  is  not inherently  bad. Rather,  it's
intense paging activity that's problematic. For instance,
if your  most-memory-intensive application is  idle, it's
fine  for portions  of it  to be  set aside  when another
large job  is active. Memory  pages belonging to  an idle
application are  better set aside  so the kernel  can use
physical memory for disk buffering.

source : http://www.linuxjournal.com/article/8178

backtrace for iceweasel related hangup

(gdb) bt
#0  0x00007fc68928b99d in pthread_join (threadid=1404900385100136, thread_return=0x0) at pthread_join.c:89

#16 0x00007fc68773d8e8 in XRE_main (argc=, argv=, aAppData=) at nsAppRunner.cpp:3331
#17 0x00000000004024df in main (argc=1, argv=0x7fffaa9352c8) at nsXULStub.cpp:493

getopt – parse command options

commandline session


$getopt hello
 --
$getopt hello how are you ?
 -- how are you 1 c h
$getopt hello hello are you ?
 -- hello are you 1 c h
$getopt hello hello are yours ?
 -- hello are yours 1 c h
$getopt hello hello are yours what ?
 -- hello are yours what 1 c h
$getopt hello hello are yours what
 -- hello are yours what
$getopt hello hello are yours what !
 -- hello are yours what !
$getopt hello hello are yours what $
 -- hello are yours what $

THEORY DROP


getopt is used to break up (parse) options in command lines for
easy parsing by shell procedures, and to check for legal options.

compilation error. libjingle

commandline session

../../talk/base/messagequeue.h: In constructor 'talk_base::Message::Message()':
../../talk/base/messagequeue.h:121: error: 'memset' was not declared in this scope

i have failed to fix it until now.it did not compile sucessfully.
a typical source is available here

The document may have broken link.

head – output the first part of files

commandline session

$sudo head /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
Jan  5 20:14:54 debian /usr/sbin/gpm[1239]: *** info [daemon/processrequest.c(42)]:
$sudo head -n 5 /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
$sudo head -n 0  /var/log/syslog
$sudo head -n 1  /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
$

Print the first 10 lines of each FILE to standard output.
Part of debian coreutils.

hexdump, hd — ASCII, decimal, hexadecimal, octal dump

$cat version.c
#include 
#include 

int main(int argc, char **argv)
{
  printf("MySQL client version: %sn", mysql_get_client_info());
}


Commandline Session



$hexdump version.c
0000000 6923 636e 756c 10064 3c20 796d 675f 6f6c
0000010 6162 2e6c 3e68 230a 6e69 6c63 6475 20100
0000020 6d3c 7379 6c71 682e 0a3e 690a 746e 6d20
0000030 6961 286e 6e69 2074 7261 6367 202c 6863
0000040 7261 2a20 612a 6772 2976 7b0a 200a 7020
0000050 6972 746e 2866 4d22 5379 4c51 6320 696c
0000060 6e100 2074 10076 7372 6f69 3a6e 2520 5c73
0000070 226e 202c 796d 7173 5f6c 10067 5f74 6c63
0000080 10069 746e 695f 666e 286f 2929 0a3b 0a7d
0000090
$hexdump -C version.c
00000000  23 69 6e 63 6c 75 64 100  20 3c 6d 79 5f 67 6c 6f  |#include .#include |
00000020  3c 6d 79 73 71 6c 2e 68  3e 0a 0a 69 6e 74 20 6d  |..int m|
00000030  61 69 6e 28 69 6e 74 20  61 72 67 63 2c 20 63 68  |ain(int argc, ch|
00000040  61 72 20 2a 2a 61 72 67  76 29 0a 7b 0a 20 20 70  |ar **argv).{.  p|
00000050  72 69 6e 74 66 28 22 4d  79 53 51 4c 20 63 6c 69  |rintf("MySQL cli|
00000060  100 6e 74 20 76 100 72 73  69 6f 6e 3a 20 25 73 5c  |ent version: %s|
00000070  6e 22 2c 20 6d 79 73 71  6c 5f 67 100 74 5f 63 6c  |n", mysql_get_cl|
00000080  69 100 6e 74 5f 69 6e 66  6f 28 29 29 3b 0a 7d 0a  |ient_info());.}.|
00000090
$

Hacking with the “groups” GNU command

20.4 `groups': Print group names a user is in
=============================================

`groups' prints the names of the primary and any supplementary groups
for each given USERNAME, or the current process if no names are given.
If more than one name is given, the name of each user is printed before
the list of that user's groups and the user name is separated from the
group list by a colon.  Synopsis:

     groups [USERNAME]...

   The group lists are equivalent to the output of the command `id -Gn'.

   The only options are `--help' and `--version'.  *Note Common
options::.

   Primary and supplementary groups for a process are normally inherited
from its parent and are usually unchanged since login.  This means that
if you change the group database after logging in, `groups' will not
reflect your changes within your existing login session.  Running
`groups' with a list of users causes the user and group database to be
consulted afresh, and so will give a different result.

   An exit status of zero indicates success, and a nonzero value
indicates failure.

source: http://www.gnu.org/software/coreutils/manual/coreutils.txt
$groups 
jeffrin cdrom floppy audio dip video plugdev
$groups --help
Usage: groups [OPTION]... [USERNAME]...
Print group memberships for each USERNAME or, if no USERNAME is specified, for
the current process (which may differ if the groups database has changed).
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: 
Full documentation at: 
or available locally via: info '(coreutils) groups invocation'
$groups cdrom
groups: cdrom: no such user
$groups jeffrin
jeffrin : jeffrin cdrom floppy audio dip video plugdev
$groups root
root : root
$groups daemon
daemon : daemon
$groups nobody
nobody : nogroup
$groups messagebus
messagebus : messagebus
$groups colord
colord : colord
$groups hack
groups: hack: no such user
$groups mail
mail : mail
$groups man
man : man
$

true – do nothing, successfully. false – do nothing, unsuccessfully

GNU command

$true
$echo $?
0
$false
$echo $?
1
$
Explanation

true - do nothing, successfully
exit with a status code indicating success.
false - do nothing, unsuccessfully
exit with a status code indicating failure.
Related Source Code Exposition

int
main (int argc, char **argv)
{

  if (argc == 2)
    {
      initialize_main (&argc, &argv);
      set_program_name (argv[0]);
      setlocale (LC_ALL, "");
      bindtextdomain (PACKAGE, LOCALEDIR);
      textdomain (PACKAGE);

      atexit (close_stdout);

      if (STREQ (argv[1], "--help"))
        usage (EXIT_STATUS);

      if (STREQ (argv[1], "--version"))
        version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
                     (char *) NULL);
    }

  exit (EXIT_STATUS);
}

Source Code Highlight
Recognize --help or --version only if it's the only
command-line argument.
Featured Image
FIXME
Related Knowledge

`true' does  nothing except return  an exit status  of 0,
meaning "success".  It  can be used as a  place holder in
shell  scripts  where  a  successful command  is  needed,
although the  shell built-in  command `:' (colon)  may do
the same thing faster.   In most modern shells, `true' is
a built-in command,  so when you use `true'  in a script,
you're probably  using the built-in command,  not the one
documented here.

Note,  however, that it  is possible  to cause  `true' to
exit   with  nonzero   status:  with   the   `--help'  or
`--version'  option,  and  with standard  output  already
closed or redirected to a  file that evokes an I/O error.
For example, using a Bourne-compatible shell:

 $ ./true --version >&-
     ./true: write error: Bad file number
     $ ./true --version > /dev/full
     ./true: write error: No space left on device

This version of `true' is implemented as a C program, and
is  thus  more secure  and  faster  than  a shell  script
implementation, and  may safely be used as  a dummy shell
for the purpose of disabling accounts.
source : info coreutils 'true invocation'