Guide to Unix/Commands/Miscellaneous
sync[edit | edit source]
sync write memory buffers to disk
Example: Sync has no options, doesn't display any messages
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.
echo[edit | edit source]
echo outputs its parameters to the standard output.
$ 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
printf[edit | edit source]
Produces formatted output, richer than echo.
- 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.
cal[edit | edit source]
Outputs a calendar for the current month. If followed by a date (a month or a year), outputs a calendar for that period.
$ 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
- cal, opengroup.org
- cal man page, man.cat-v.org
- cal, freebsd.org
- cal in util-linux, man7.org
- GNU gcal manual, gnu.org
date[edit | edit source]
date displays the current date and time.
$ date Mon Jun 26 12:34:56 CDT 2006
time[edit | edit source]
time time a program
$ time real 0m1.818s user 0m0.770s sys 0m0.210s
from[edit | edit source]
from display the names of those who sent you mail recently
$ from From firstname.lastname@example.org Sat Feb 05 08:52:37 2005 From email@example.com 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.
mail[edit | edit source]
mail allows you to read and write emails.
$ 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 firstname.lastname@example.org Tue Jun 27 12:34 16/674 "What's up?" &
Tips: Press enter to read.
clear[edit | edit source]
Clears the screen of the terminal. Takes no arguments. Keywords: cls.
- clear man page, man.cat-v.org
seq[edit | edit source]
Outputs sequences of numbers, even floating point numbers. Seems not covered by POSIX.
- 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.
- seq man page, man.cat-v.org
- 26.3 seq: Print numeric sequences in GNU Coreutils manual, gnu.org
- W:seq (Unix)
tee[edit | edit source]
Writes the input stream into a file while at the same time passing it to standard output.
- dir | tee dirout.txt | less
- tee, opengroup.org
- tee man page, man.cat-v.org
- 17.1 tee: Redirect output to multiple files or processes in GNU Coreutils manual, gnu.org
- W:tee (Unix)
sleep[edit | edit source]
Pauses for a period of time.
- 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.
- sleep, opengroup.org
- sleep man page, man.cat-v.org
- sleep, freebsd.org
- 25.1 sleep in GNU Coreutils manual, gnu.org
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.
- Outputs a stream of repeated "y" strings, separated by newline.
- yes hey
- Outputs a stream of repeated "hey" strings, separated by newline.
- yes, freebsd.org
- yes Linux man page, man7.org
- 15.3 yes in GNU Coreutils manual, gnu.org
- yes (Unix), wikipedia.org
true[edit | edit source]
Does nothing and finishes with zero exit code, indicating success.
- true, opengroup.org
- true man page, man.cat-v.org
- true, freebsd.org
- 16.2 true in GNU Coreutils manual, gnu.org
false[edit | edit source]
Does nothing and finishes with non-zero exit code (often 1), indicating failure.