14 авг. 2012 г.

Bash: Let me bash that for you, Part 0

I was going to write about some Unix-like shell hints which I usually use like grep, sed, for loop, variables and so on. I noticed that I do usually use lots of bash kung-fu beyond the shadow of a doubt.

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 anycommand

Each man has to know unix command man. That's manual, dude.

pwd

prints path of working directory
$ pwd 
/home/user

ls path

list of directory
$ ls /tmp
myfile
Hint: . (dot) in unix-like systems is current directory
$ ls .
myfile
Hint: when path isn't specified ls uses current directory
$ ls 
myfile
Hint: 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 path

change 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
      For instance
      // Ctrl + r cd a
      $ cd a/b/c/d
      
  • cd ~
    move to home directory
    $ cd ~
    $ pwd
    /home/user


  • cd
    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)
  • group
  • others
There are three types of permissions:
  • read = 4
  • write = 2
  • execute = 1
Two ways how to change mode:
  • numeric:
    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.

    Example:
    $ 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
    
  • symbolic
    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
    [1]+  Stopped                gzip application.log
    
    It means that we've got job #1 which is gzip application.log has state Stopped.
  • jobs
    get the current bash session job list
    $ jobs
    [1]+  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
    [1]+ gzip application.log &
    $
    [1]+  Done                    gzip application.log
  • command &
    move command execution to background
    $ gzip application.log &
    [1] 364
    $ jobs
    [1]+ Running                 gzip application.log
    $
    [1]+  Done                   gzip application.log
  • wait pid
    wait while background processes with pid will be finished
    $ gzip application.log &
    [1] 415
    $ wait 415
    [1]+  Done                   gzip application.log
  • Hint: use simple wait to await to finish all background processes.

history number-of-items

prints last number-of-items (if specified or all otherwise) executed commands.
$ history 3
  558  ls
  559  cd ~/
  560  history 
Hint: to run n-th command in history use exclamation mark
$ !558
ls
file                              myfile
Hint: to run last command use double exclamation mark
$ !!
ls
file                              myfile

sleep seconds

sleep 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

Hot keys

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 command
Now, let's go and conquer the world.

1 комментарий:

Mikhail B комментирует...
Этот комментарий был удален автором.