Guide to Unix/Commands/Miscellaneous

From Wikibooks, open books for an open world
Jump to navigation Jump to search

sync

sync write memory buffers to disk

Example: Sync has no options, doesn't display any messages

$ sync 

Tips:

It is always good to type sync a couple of times, one the important functions of sync is to update your superblock information.

The sync calls sync Unix system call and exits with success code '0' or '1' if it fails. These exit codes stored in $? variable.

$ sync
$ echo $?
 0 

The above example shows that sync was successful.

Links:

  • sync, freebsd.org
  • sync, manpages.ubuntu.com
  • 14.4 sync in GNU Coreutils manual, gnu.org

echo

echo outputs its parameters to the standard output.

Examples:

$ echo "hello world"
hello world

Tips: Some common echo usage:

Check a shell variable:

$ echo $EDITOR
emacs

Check the parameters passed in the previous command:

$ ls -l 
 .........
$ echo $_
-l

Check the current parent process:

$ echo $0
bash

Check the exit code of the last command:

$ echo $?
0

Create a empty file (same as touch /tmp/newfile):

$ echo "" > /tmp/newfile

Create a new file with some text:

$ echo "exec fluxbox" > ~/.xinitrc

Add (append) a new line to end of file:

$ echo "A New Line" >> /tmp/newfile

Links:

printf

Produces formatted output, richer than echo.

Examples:

  • printf Hello world
    • Outputs Hello world, with no final newline.
  • printf "Hello "; printf "world"
    • As above, with no newline to separate Hello and world.
  • printf "Hello world\n"
    • Outputs Hello world, with a final newline via \n.
  • printf "Hello\tworld"
    • Outputs Hello, then tab character, then world. Other escape sequences supported include \\, \n, \r, and more.
  • printf "Hello\011world"
    • As above: 011 is the octal number of the tab character.
  • printf "%0x" 255
    • Outputs ff, that is, 255 converted to hex.
  • printf "%s %0x" Hex: 255
    • Takes "Hex:" as the 1st argument to be formatted using %s and 255 to be the 2nd argument to be formatted using %0x.
  • printf "%s\n" 1 2 3 4 5
    • Outputs the integers 1,..., 5, one integer per line. Thus, feeds the 2nd to last argument into the format string as if in a loop.
  • printf "%s-%s\n" 1 2 3 4 5 6
    • Outputs lines containing 1-2, 3-4, and 5-6. Thus, repeatedly lets the format string consume as many arguments as it needs.
  • printf "%s-%s-%s\n" 1 2 3 4
    • Outputs lines containing 1-2-3 and 4--. Thus, the arguments missing to provide complete triplets are treated as empty.
  • printf "%i-%i-%i\n" 1 2 3 4
    • Outputs lines containing 1-2-3 and 4-0-0. Thus, the arguments missing to provide complete triplets are treated as zeros.
  • printf "%o" 255
    • Outputs 377, octal of 255.
  • printf "%X" 255
    • Outputs FF, hex of 255 with capital letters.

Links:

cal

Outputs a calendar for the current month. If followed by a date (a month or a year), outputs a calendar for that period.

Examples:

$ cal 
     April 2004
Su Mo Tu We Th Fr Sa 
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
$ cal 01 2007
    January 2007
Su Mo Tu We Th Fr Sa 
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

More examples, not all of which are supported by all commonly used versions of cal:

  • cal 2004
    • Shows a calendar for the whole year 2004
  • cal -3
    • Shows current, previous and next month (default on some implementations)
  • cal -3 04 2004
    • Shows April 2004, as well as the previous (March) and following (May) month
  • cal -1
    • Shows the current month (default on some implementations)
  • cal -j
    • Shows this months calendar with day-of-year number (counted from January 1st) rather than the date
  • cal -w
    • In util-linux cal, outputs week number in addition to the daily calendar.
  • gcal -K
    • In GNU gcal, outputs week number in addition to the daily calendar.

Tips: The Gregorian Calendar was adopted in the British Empire in 1752. The 2nd day of September 1752 was immediately followed by the 14th day of September, as shown by the example below.

$ cal 9 1752
  September 1752   
Su Mo Tu We Th Fr Sa
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Links:

date

date displays the current date and time.

Example:

$ date
Mon Jun 26 12:34:56 CDT 2006

Links:

time

time time a program

Example:

$ time

 real    0m1.818s
 user    0m0.770s
 sys     0m0.210s

Links:

from

from display the names of those who sent you mail recently

Example:

$ from
  From andy@box.po Sat Feb 05 08:52:37 2005
  From andy@box.po Sat Feb 05 08:53:52 2005

Count the number of mail in your mailbox

$ from -c
There are 2 messages in your incoming mailbox.

Links:

mail

mail allows you to read and write emails.

Example:

$ mail
No mail for user.
$ mail user2
Subject: What's up?
Hi user2, you can delete this rubbish by pressing 'd'.
Cc: user

Tips: Note that you need to press enter then ctrl+d to confirm.

$ mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/user": 1 message 1 new
>N 1 user@unix.com Tue Jun 27 12:34 16/674 "What's up?"
&

Tips: Press enter to read.

Links:

clear

Clears the screen of the terminal. Takes no arguments. Keywords: cls.

Links:

seq

Outputs sequences of numbers, even floating point numbers. Seems not covered by POSIX.

Examples:

  • seq 10
    • Outputs integers 1 to 10, one per line.
  • seq 2 10
    • Outputs integers 2 to 10.
  • seq 3 2 10
    • Outputs integers 3 to 10 with step 2, and therefore, 3, 5, 7, and 9.
  • seq -5 5
    • Outputs integers -5 to 5
  • seq 0.5 0.1 1
    • Outputs numbers 0.5 to 1 with step 0.1: 0.5, 0.6, 0.7, 0.8, and 0.9 (why not 1?). The decimal point vs. comma of the output seems to be locale specific.
  • seq 1E3
    • Outputs integers 1 to 1000, using the exponential notation.

Links:

tee

Writes the input stream into a file while at the same time passing it to standard output.

Examples:

  • dir | tee dirout.txt | less

Links:

sleep

Pauses for a period of time.

Examples:

  • sleep 5
    • Pauses for 5 seconds.
  • sleep 10800
    • Pauses for 3 hours: for 3 * 60 * 60 seconds.
  • sleep 1m
    • In GNU sleep, pauses for 1 minute.
  • sleep 1h
    • In GNU sleep, pauses for 1 hour.
  • sleep 1d
    • In GNU sleep, pauses for 1 day.
  • sleep 0.5m
    • In GNU sleep, pauses for half a minute.
  • sleep 1m 30s
    • In GNU sleep, pauses for 1 minute and 30 seconds.

Links:

yes

Outputs an indefinite stream of newline-separated strings "y", or other strings as specified. One of the uses is feeding the output into a command that requires user confirmation.

Examples:

  • yes
    • Outputs a stream of repeated "y" strings, separated by newline.
  • yes hey
    • Outputs a stream of repeated "hey" strings, separated by newline.

Links:

true

Does nothing and finishes with zero exit code, indicating success.

Links:

false

Does nothing and finishes with non-zero exit code (often 1), indicating failure.

Links: