Shell commands to know besides the usual ones.
pwd Print name of current/working directory
SYNOPSIS pwd [OPTION]…
DESCRIPTION Print the full filename of the current working directory.
-L, --logical
use PWD from environment, even if it contains symlinks
-P, --physical
avoid all symlinks
ps - report a snapshot of the current processes.
SYNOPSIS ps [options]
DESCRIPTION ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.
See –help or man for all options
type - find out where a command (app) is located in the directory structure.
EXAMPLE
type bash Outputs “bash is /bin/bash”
OR use
which
SYNOPSIS which [-a] filename …
history history 8 shows the last 8 commands. !n runs the command in the list with the history number n
See –help or man for all options.
declare - Show the currently assigned environment variables (and more)
OR Just write set
Put a & after the shell command makes it run in the background
Put a current running session in the background and stop it:
ctrl-ZBring a session back to the forgrund: fg %n. E-g.fg %1.
jobs - Show all jobs
alias
E.g. ```alias lsc='pwd;ls -laLC'```
NAME bc - An arbitrary precision calculator language
SYNTAX bc [ -hlwsqv ] [long-options] [ file … ]
DESCRIPTION bc is a language that supports arbitrary precision numbers with inter‐ active execution of statements. There are some similarities in the syntax to the C programming language. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.)
This version of bc contains several extensions beyond traditional bc
implementations and the POSIX draft standard. Command line options can
cause these extensions to print a warning or to be rejected. This doc‐
ument describes the language accepted by this processor. Extensions
will be identified as such.
Another way to do floating point arithmetic is to use AWK:
Mac-n-Cheese:~ nicolas$ awk “BEGIN {print 100/3}” 33.3333 You can use printf to adjust the precision of the results:
Mac-n-Cheese:~ nicolas$ awk “BEGIN {printf "%.2f\n", 100/3}” 33.33 When using negative values, make sure to leave a white space between signs.
Mac-n-Cheese:~ nicolas$ awk “BEGIN {print -8.4–8}” awk: cmd. line:1: BEGIN {print -8.4–8} awk: cmd. line:1: ^ syntax error Mac-n-Cheese:~ nicolas$ awk “BEGIN {print -8.4 - -8}” -0.4
And, there are lots of options!!!
Summary
$ echo “$((20.0/7))” # (ksh93/zsh/yash, not bash) $ awk “BEGIN {print (20+5)/2}” $ zcalc $ bc «< 20+5/2 $ bc «< “scale=4; (20+5)/2” $ dc «< “4 k 20 5 + 2 / p” $ expr 20 + 5 $ calc 2 + 4 $ node -pe 20+5/2 # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI $ echo 20 5 2 / + p | dc $ echo 4 k 20 5 2 / + p | dc $ perl -E “say 20+5/2” $ python -c “print 20+5/2” $ python -c “print 20+5/2.0” $ clisp -x “(+ 2 2)” $ lua -e “print(20+5/2)” $ php -r ’echo 20+5/2;' $ ruby -e ‘p 20+5/2’ $ ruby -e ‘p 20+5/2.0’ $ guile -c ‘(display (+ 20 (/ 5 2)))’ $ guile -c ‘(display (+ 20 (/ 5 2.0)))’ $ slsh -e ‘printf("%f",20+5/2)’ $ slsh -e ‘printf("%f",20+5/2.0)’ $ tclsh «< ‘puts [expr 20+5/2]’ $ tclsh «< ‘puts [expr 20+5/2.0]’ $ sqlite3 «< ‘select 20+5/2;’ $ sqlite3 «< ‘select 20+5/2.0;’ $ echo ‘select 1 + 1;’ | sqlite3 $ psql -tAc ‘select 1+1’ $ R -q -e ‘print(sd(rnorm(1000)))’ $ r -e ‘cat(pi^2, “\n”)’ $ r -e ‘print(sum(1:100))’ $ smjs $ jspl
Details
Shells
You can use POSIX arithmetic expansion for integer arithmetic echo “$((…))”:
$ echo “$((20+5))” 25 $ echo “$((20+5/2))” 22 ksh93, yash and zsh do support floats there:
$ echo “$((1.2 / 3))” 0.4 $ echo “$((4*atan(1)))” 3.14159265358979324 (in zsh, you need zmodload zsh/mathfunc to get the math functions like atan above, yash doesn’t support them).
Interactively with zsh:
$ autoload zcalc $ zcalc 1> PI/2 1.5708 2> cos($1) 6.12323e-17 3> :sci 12 6.12323399574e-17 With (t)csh (integer only):
% @ a=25 / 3; echo $a 8 In the rc shell family, akanga is the one with arithmetic expansion:
; echo $:25/3 8 POSIX toolchest
bc (see below for interactive mode), manual here
Mnemonic: best calculator (though the b is in fact for basic).
$ echo 20+5/2 | bc 22 $ echo ‘scale=4;20+5/2’ | bc 22.5000 (supports arbitrary precision numbers)
bc interactive mode:
$ bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty’. 5+5 10
2.2+3.3 5.5 Rush’s solution, expr (no interactive mode):
$ expr 20 + 5 25 $ expr 20 + 5 / 2 22 Joshua’s solution: awk (no interactive mode):
$ calc() { awk “BEGIN{print $*}”; } $ calc 1/3 0.333333 Other more or less portable tools
Arcege’s solution, dc (interactive mode: dc):
Which is even more fun since it works by reverse polish notation.
$ echo 20 5 2 / + p | dc 22 $ echo 4 k 20 5 2 / + p | dc 22.5000 But not as practical unless you work with reverse polish notation a lot.
Note that dc predates bc and bc has been historically implemented as a wrapper around dc but dc was not standardised by POSIX
DQdims’s calc (required sudo apt-get install apcalc):
$ calc 2 + 4 6 General purpose language interpreters:
manatwork’s solution, node (interactive mode: node; output function not needed):
$ node -pe 20+5/2 # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI 22.5 Perl (interactive mode: perl -de 1):
$ perl -E “say 20+5/2” 22.5 Python (interactive mode: python; output function not needed):
$ python -c “print(20+5/2)” 22 # 22.5 with python3 $ python -c “print(20+5/2.0)” 22.5 Also supports arbitrary precision numbers:
$ python -c ‘print(2**1234)’ 295811224608098629060044695716103590786339687135372992239556207050657350796238924261053837248378050186443647759070955993120820899330381760937027212482840944941362110665443775183495726811929203861182015218323892077355983393191208928867652655993602487903113708549402668624521100611794270340232766099317098048887493809023127398253860618772619035009883272941129544640111837184 If you have clisp installed, you can also use polish notation:
$ clisp -x “(+ 2 2)” Marco’s solution, lua (interactive mode: lua):
$ lua -e “print(20+5/2)” 22.5 PHP (interactive mode: php -a):
$ php -r ’echo 20+5/2;’ 22.5 Ruby (interactive mode: irb; output function not needed):
$ ruby -e ‘p 20+5/2’ 22 $ ruby -e ‘p 20+5/2.0’ 22.5 Guile (interactive mode: guile):
$ guile -c ‘(display (+ 20 (/ 5 2)))’ 45/2 $ guile -c ‘(display (+ 20 (/ 5 2.0)))’ 22.5 S-Lang (interactive mode: slsh; output function not needed, just a ; terminator):
$ slsh -e ‘printf("%f",20+5/2)’ 22.000000 $ slsh -e ‘printf("%f",20+5/2.0)’ 22.500000 Tcl (interactive mode: tclsh; output function not needed, but expr is):
$ tclsh «< ‘puts [expr 20+5/2]’ 22 $ tclsh «< ‘puts [expr 20+5/2.0]’ 22.5 Javascript shells:
$ smjs js> 25/3 8.333333333333334 js>
$ jspl JSC: 25/3
RP: 8.33333333333333 RJS: [object Number] JSC: Good bye…
$ node
25/3 8.333333333333334
Various SQL’s:
SQLite (interactive mode: sqlite3):
$ sqlite3 «< ‘select 20+5/2;’ 22 $ sqlite3 «< ‘select 20+5/2.0;’ 22.5 MySQL:
mysql -BNe ‘select 1+1’ PostgreSQL:
psql -tAc ‘select 1+1 _The options on mysql and postgres stop the ‘ascii art’ image !
Specialised math-oriented languages:
R in plain mode - lets generate 1000 Normal random numbers and get the standard deviation and print it
$ R -q -e ‘print(sd(rnorm(1000)))’
print(sd(rnorm(1000))) [1] 1.031997 R using the littler script - lets print pi squared
$ r -e ‘cat(pi^2, “\n”)’ 9.869604 $ r -e ‘print(sum(1:100))’ [1] 5050 PARI/GP, an extensive computer algebra system for number theory, linear algebra, and many other things
$ echo “prime(1000)"|gp -q 7919 // the 1000th prime $ echo “factor(1000)” | gp -q [2 3] [5 3] // 2^3*5^3 $ echo “sum(x=1,5,x)” | gp -q 15 // 1+2+3+4+5 GNU Octave (a high-level interpreted language, primarily intended for numerical computations)
Also supports complex numbers:
$ octave
1.2 / 7 ans = 0.17143 sqrt(-1) ans = 0 + 1i
Use Samba’s net usershare command to share directories from the command line, IMHO this is less clunky and much safer than fiddling around in the main smb.conf file:
net usershare add share_name /home/user/somefolder "Share description" everyone:F guest_ok=yTo make these shares permanent (i.e. persist over reboots) create a file for each share under /var/lib/samba/usershares/ like this:
net usershare info --long share_name > /var/lib/samba/usershares/share_nameThis has a further advantage that if you’re using a desktop like KDE or GNOME/MATE/Unity the directories will show up as shared in the file manager (Dolphin, Nautilus etc.). That’s cause the net username//var/lib/samba/usershares/ thing is the same mechanism that Dolphin and Nautilus use when you share directories in the GUI.
See also: http://ubuntuforums.org/showthread.php?t=1659816
I needed sudo for this to work.
You can edit /etc/samba/smb.conf and enter the share name, location, and who should access it. There are several examples in the file itself.
Here’s online guide for entering new shares in the smb.conf file: