Guide to Unix/Commands/Miscellaneous

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

sync[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

Outputs a calendar, per default for the current month. If followed by a year, outputs calendar for that year; if followed by a month and a year, outputs a calendar for that period. POSIX supports no options: all options are extensions. The options of linux-util cal, GNU gcal and FreeBSD cal are generally incompatible.

Introductory examples, covered by POSIX:

$ 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 for POSIX cal:

  • cal 2004
    • Outputs a calendar for the whole year 2004.

Examples for util-linux cal:

  • cal Mar 2000
    • Outputs March of year 2000.
  • cal -3
    • Outputs current, previous and next month.
  • cal -3 04 2004
    • Outputs April 2004, as well as the previous (March) and following (May) month. Works in util-linux and FreeBSD.
  • cal -1
    • Outputs the current month, default per POSIX.
  • cal -n 2 06 2020
    • Outputs the calendar for June (06) and July, thus, for 2 months starting with the month entered.
  • cal -j
    • Outputs this months calendar with day-of-year number (counted from January 1st) rather than the date
  • cal -w
    • Outputs week number in addition to the daily calendar.

Examples for GNU gcal:

  • gcal
    • Outputs current month.
  • gcal .
    • Outputs current, previous and next month.
  • gcal Jan
    • Outputs current January.
  • gcal Jan-Mar
    • Outputs current January through March.
  • gcal Jan 2000
    • Outputs the January of 2000.
  • gcal -K
    • 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:

$ 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[edit | edit source]

date displays the current date and time.

Example:

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

Links:

time[edit | edit source]

time time a program

Example:

$ time

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

Links:

from[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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

Links:

seq[edit | edit source]

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:

shuf[edit | edit source]

Randomly shuffles file or input lines. Not covered by POSIX.

Examples:

  • seq 10 | shuf
    • Outputs numbers from 1 to 10 randomly shuffled, one item per line.
  • shuf -e a b c
    • Randomly shuffles the a, b and c items, outputting one item per line.
  • shuf -i 1-10
    • Outputs numbers from 1 to 10 randomly shuffled, one item per line.

Links:

tee[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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

Links:

false[edit | edit source]

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

Links:

env[edit | edit source]

Sets the environment for a command or outputs all environment variables. See also Environment Variables.

Examples:

  • env
    • Outputs all environment variables.
  • env printf Hello
    • Ensures command printf is called rather than the possible shell builtin.

Links:

  • env, opengroup.org
  • env, man7.org
  • env, freebsd.org
  • env, wikipedia.org

printenv[edit | edit source]

Outputs values of one or more environment variables or all variables if none are passed. Not covered by POSIX. Alternatives are #env to output all variables and "echo $HOME" to output a single variable, here HOME. See also Environment Variables.

Examples:

  • printenv HOME
  • printenv HOME USER
  • printenv
    • Outputs all variables.

Links: