I'm not going to start a holly war what shell is the best one - sh, ksh or whatever. I know how to cook bash tasty - it has lots in common with sh but some nuances happen.
That isn't a bash tutorial or so - it's more like commonly used hints and tips.
man anycommandEach man has to know unix command man. That's manual, dude.
$ pwd /home/user
ls pathlist of directory
$ ls /tmp myfileHint: . (dot) in unix-like systems is current directory
$ ls . myfileHint: when path isn't specified ls uses current directory
$ ls myfileHint: option -l prints a long file format with ownership info, file size and so on
$ ls -l -rw-r--r-- 1 user user 12 Jul 7 11:00 myfile
Hint: option -h prints file size in human readable format - Kilobytes, Megabytes etc
$ ls -lh Java\ Concurrency\ In\ Practice.pdf -rw-r--r-- 1 user user 3.9M May 22 2009 Java Concurrency In Practice.pdf
cd pathchange directory
$ cd /tmp
- cd path/of/my/destination/
Hint: I prefer use cd target/destitaton/path instead of several cd. It's much better, believe me. That's the benefit :
- cd -
return back to previous directory in the change directory history
$ pwd /tmp $ mkdir -p a/b/c/d/e $ cd a/b/c/d $ pwd /tmp/a/b/c/d $ cd - $ pwd /tmp
- revert search over bash command history
It's possible to search in already executed commands using bash.
Ctrl + r typing
// Ctrl + r cd a $ cd a/b/c/d
- cd -
- cd ~
move to home directory
$ cd ~ $ pwd /home/user
shortcut for cd ~
$ cd $ pwd /home/user
chown options owner:group filename(s)changes ownership of file(s). Every file in Unix has user ownership, group and other permissions.
$ ls -la myfile -rw-r----- 1 user user 12 Jul 7 11:00 myfile $ chown user2 myfile $ ls -la myfile -rw-r----- 1 user2 user 12 Jul 7 11:00 myfile
chmod options filename(s)changes mode file(s). Every file (in general directory is file too) in Unix has three permissions:
- user (owner)
- read = 4
- write = 2
- execute = 1
1st digit corresponds to user permissions.
2nd digit corresponds to group permissions.
3rd digit corresponds to other permissions.
Every digit is sum of permission type, e.g.
read + write = 4 + 2 = 6
read + write + execute = 4 + 2 + 1 = 7
Note: When change mode it doesn't keep previous permissions - permission type = 6 means that it has read + write but no executable.
$ chmod 640 myfile $ ls -la myfile -rw-r----- 1 user user 12 Jul 7 11:00 myfile $ chmod 754 myfile $ ls -la myfile -rwxr-xr-- 1 user user 12 Jul 7 11:00 myfile
Change permission in human readable way:
u+r grants readable permission to user
g-w removes writable permission from group
Note: In this case chmod keeps previous permissions.
$ ls -la myfile -rwxr-xr-- 1 user user 12 Jul 7 11:00 myfile $ chmod o-r myfile $ ls -la myfile -rwxr-x--- 1 user user 12 Jul 7 11:00 myfile $ chmod g+w myfile $ ls -la myfile -rwxrwx--- 1 user user 12 Jul 7 11:00 myfile
Background and foreground processes
- Hotkey Ctrl + z stops/resumes command execution
$ gzip application.log ^Z + Stopped gzip application.logIt means that we've got job #1 which is gzip application.log has state Stopped.
get the current bash session job list
$ jobs + Stopped gzip application.log
- fg job#
move job job# to foreground
$ fg 1 gzip application.log
- bg job#
move job job# to background
$ bg 1 + gzip application.log & $ + Done gzip application.log
- command &
move command execution to background
$ gzip application.log &  364 $ jobs + Running gzip application.log $ + Done gzip application.log
- wait pid
wait while background processes with pid will be finished
$ gzip application.log &  415 $ wait 415 + Done gzip application.log
Hint: use simple wait to await to finish all background processes.
history number-of-itemsprints last number-of-items (if specified or all otherwise) executed commands.
$ history 3 558 ls 559 cd ~/ 560 historyHint: to run n-th command in history use exclamation mark
$ !558 ls file myfileHint: to run last command use double exclamation mark
$ !! ls file myfile
sleep secondssleep for a specified number of seconds
$ date ; sleep 5; date Sat Jul 7 22:47:36 MSK 2012 Sat Jul 7 22:47:41 MSK 2012
Ctrl + a - move cursor to the beginning of the line Ctrl + e - move cursor to the end of the line Ctrl + u - delete from cursor position to the beginning of the line Ctrl + k - delete from cursor position to the end of the line Ctrl + w - delete from cursor position to the beginning of the word Ctrl + r - search in the history of executed commands Ctrl + l - clear screen Ctrl + d - shortcut for exit Ctrl + z - stop/resume command background execution Ctrl + c - cancel foreground command execution Esc . (dot) - add last argument of last executed commandNow, let's go and conquer the world.