2014年9月30日 星期二

[Linux 文章收集] Linux ls Command: Sort Files By Size

Source From Here 
Preface 
How do I sort all *.avi or *.py files in $HOME/Download/ directory by file size using Linux ls command line utility? 

The ls command is used to list directory contents under Linux and Unix like operating systems. If no options or operands are given, the contents of the current directory are displayed on the screen. By default entries are sorted alphabetically if none of the -cftuvSUX nor --sort option passed to the ls command. 

The default output (sort by alphabetically) 
Type the following command: 
$ ls
$ ls *.py
$ ls *.avi

Sample outputs: 
 

Force sort by size option 
You need to pass the -S or --sort=size option as follows: 
$ ls -S
$ ls -S -l
$ ls --sort=size -l
$ ls --sort=size *.avi
$ ls -S -l *.avi

Sample outputs: 
 

You will see largest file first before sorting the operands in lexicographical order. The following command will sort file size in reverse order: 
$ ls -l -S | sort -k 5 -n

OR try (see comments below, thanks!): 
$ ls -lSr

Sample Output: 
 

Sort output and print sizes in human readable format (e.g., 1K 234M 2G) 
Pass the -h option to the ls command as follows: 
$ ls -lSh
$ ls -l -S -h *.avi
$ ls -l -S -h ~/Downloads/*.mp4 | more

Supplement 
Linux/Unix Command - sort

[ Ruby Gossip ] Basic : 基本指令與觀念 - irb 與 ruby 指令

Source From Here 
Preface 
你 可以至 Ruby 的官方網站 下載安裝 Ruby (JRuby),初學 Ruby,可以執行Ruby安裝目錄中的 irb 指令,啟動指令互動環 境來作些簡單的程式練習,可以自行進入文字模式,設定 PATH 中包括Ruby安裝目錄的bin目錄,再執行 irb 指令以進入指令互動環境: 
# irb
...
irb(main):001:0> 1 + 2
=> 3
irb(main):002:0>

這是Ruby的指令互動環境,可以讓你很快地撰寫一些小指令進行測試(經常的,你只是要看看某個指令這麼用對不對,或會有什麼結果),上例執行 1 + 2,=> 3 表示結果為3,如果想離開irb,可以輸入exit。預設irb提示(Prompt)較為冗長,可以執行irb時加上 --simple-prompt,顯示簡單提示字元,順便來看幾個簡單的互動: 
>> 1 + 2
=> 3
>> _
=> 3
>> 1 + _
=> 4
>> _
=> 4
>> print "Hello! Ruby!\n"
Hello! Ruby!
=> nil

這執行了 1 + 2,顯示結果為3,_ 代表了互動環境中上一次運算結果,方便你在下一次的運算中直接取用上一次的運算結果。最後一次執行了 print,這個方法可顯示指定的文字,print 不會換行,所以字串最後加上 \n 表示要換行,最後 => nil 表示 print 執行結束沒有傳回結果,這是蠻有用的資訊,如果真的不想看到,可以在執行 irb 時加上 --noecho,不過這也就不會顯示如 1+2 的執行結果: 
# irb --simple-prompt --noecho
>> print "Hello! Ruby!\n"
Hello! Ruby!
>> 1 + 2
>>

More Example 
再來看看其它的一些互動: 
 

你可以在互動中直接觀察程式碼的執行結果,Ruby 的許多定義都是以 end 結尾,如果在 irb 中輸入錯誤了,可以嘗試輸入 end 回到正常提示字元。 

你可以撰寫一個純文字檔案,建議副檔名為 .rb,在當中撰寫 Ruby 程式碼: 
- hello.rb 
  1. puts "Hello! Ruby!"  
程式中 puts 會將指定的文字輸出後換行,接著如下執行 ruby 指令啟動 Ruby 直譯器,載入指令稿直譯並執行: 
# ruby hello.rb
Hello!Ruby!

如果只是要測試一小段簡單的指令稿,不一定要寫 .rb 檔案,也不一定要進入 irb,可以在執行 ruby 指令時,於 -e 之後用單引號括住指令稿,多行程式碼時以分號區隔。例如: 
# jruby -e 'print "Hello! Ruby!\n"; puts "Hello! Ruby"'
Hello! Ruby!
Hello! Ruby

在執行 ruby 指令時,可以指定 -c 僅檢查語法但不執行程式,指定 -w 顯示額外警訊,由於 -c-w 經常一起指定,所以有個 -cw 可以達到分別指定 -c、-w 的效果。例如: 
# ruby -c -w hello.rb
Syntax OK

# ruby -cw -e 'x = 10; print (1 + x)'
-e:1: warning: (...) interpreted as grouped expression
Syntax OK

由於 Ruby 在呼叫方法時可以省略括號,上例第二個指令稿中,(1 + x) 的括號被直譯器解釋為優先執行 1 + x,而不會是 print 方法的括號,雖然就這個例子而言,哪個解釋的執行結果都相同,但有些情況下這類的解釋可能不是你想要的,於是直譯器提出了警告訊息。 

以上是常用的 ruby 指令選項,如果想知道更多選項,可以執行 ruby 時鍵入 --help 或 -h 顯示說明清單: 

Supplement: 
Next Basic : 基本指令與觀念 - load 與 require 
JRuby API 
Ruby 使用手冊

2014年9月28日 星期日

[ Groovy Doc ] AST : Singleton transformation

Source From Here 
Whether the singleton is pattern or an anti-pattern, there are still some cases where we need to create singletons. We're used to create a private constructor, a getInstance() method for a static field or even an initialized public static final field. So instead of writing code like this in Java: 
  1. public class T {  
  2.     public static final T instance = new T();  
  3.     private T() {}  
  4. }  
You just need to annotate your type with the @Singleton annotation: 
  1. @Singleton class T {}  
The singleton instance can then simply be accessed with T.instance (direct public field access). You can also have the lazy loading approach with an additional annotation parameter: 
  1. @Singleton(lazy = trueclass T {}  
Would become more or less equivalent to this Groovy class: 
  1. class T {  
  2.     private static volatile T instance  
  3.     private T() {}  
  4.     static T getInstance () {  
  5.         if (instance) {  
  6.             instance  
  7.         } else {  
  8.             synchronized(T) {  
  9.                 if (instance) {  
  10.                     instance  
  11.                 } else {  
  12.                     instance = new T ()  
  13.                 }  
  14.             }  
  15.         }  
  16.     }  
  17. }  
Lazy or not, once again, to access the instance, simply do T.instance (property access, shorcut for T.getInstance()). 

Below is the singleton usage example: 
  1. @Singleton(strict=false,lazy=true)  
  2. class T {  
  3.     static int i=0  
  4.     public T(){i++}  
  5. }  
  6.   
  7. printf("T.i=%d\n", T.i)  // lazy=false:1, lazy=true:0  
  8. printf("T.instance.i=%d\n", T.instance.i)  
  9. printf("T.instance.i=%d\n", T.instance.i)  
  10. T t = new T()  // Not access T object through T.instance  
  11. printf("T.instance.i=%d\n", T.instance.i)  
Execution Result: 
T.i=0
T.instance.i=1
T.instance.i=1
T.instance.i=2

Supplement 
Compile-time Metaprogramming - AST Transformations

[ Groovy Doc ] Category and Mixin transformations

Source From Here
If you've been using Groovy for a while, you're certainly familiar with the concept of Categories. It's a mechanism to extend existing types (even final classes from the JDK or third-party libraries), to add new methods to them. This is also a technique which can be used when writing Domain-Specific Languages. Let's consider the example below:
  1. final class Distance {  
  2.     def number  
  3.     String toString() { "${number}m" }  
  4. }  
  5.   
  6. class NumberCategory {  
  7.     static Distance getMeters(Number self) {  
  8.         new Distance(number: self)  
  9.     }  
  10. }  
  11.   
  12. use(NumberCategory) {  
  13.     def dist = 300.meters  
  14.   
  15.     assert dist instanceof Distance  
  16.     assert dist.toString() == "300m"  
  17. }  
We have a simplistic and fictive Distance class which may have been provided by a third-party, who had the bad idea of making the class final so that nobody could ever extend it in any way. But thanks to a Groovy Category, we are able to decorate the Distance type with additional methods. Here, we're going to add a getMeters() method to numbers, by actually decorating the Number type. By adding a getter to a number, you're able to reference it using the nice property syntax of Groovy. So instead of writing 300.getMeters(), you're able to write 300.meters.

The downside of this category system and notation is that to add instance methods to other types, you have to create static methods, and furthermore, there's a first argument which represents the instance of the type we're working on. The other arguments are the normal arguments the method will take as parameters. So it may be a bit less intuitive than a normal method definition we would have added to Distance, should we have had access to its source code for enhancing it. Here comes the @Categoryannotation, which transforms a class with instance methods into a Groovy category:
  1. @Category(Number)  
  2. class NumberCategory {  
  3.     Distance getMeters() {  
  4.         new Distance(number: this)  
  5.     }  
  6. }  
No need for declaring the methods static, and the this you use here is actually the number on which the category will apply, it's not the real this of the category instance should we create one. Then to use the category, you can continue to use the use(Category) {} construct. What you'll notice however is that these kind of categories only apply to one single type at a time, unlike classical categories which can be applied to any number of types.

Now, pair @Category extensions to the @Mixin transformation, and you can mix in various behavior in a class, with an approach similar to multiple inheritance:
  1. @Category(Vehicle) class FlyingAbility {  
  2.     def fly() { "I'm the ${name} and I fly!" }  
  3. }  
  4.   
  5. @Category(Vehicle) class DivingAbility {  
  6.     def dive() { "I'm the ${name} and I dive!" }  
  7. }  
  8.   
  9. interface Vehicle {  
  10.     String getName()  
  11. }  
  12.   
  13. @Mixin(DivingAbility)  
  14. class Submarine implements Vehicle {  
  15.     String getName() { "Yellow Submarine" }  
  16. }  
  17.   
  18. @Mixin(FlyingAbility)  
  19. class Plane implements Vehicle {  
  20.     String getName() { "Concorde" }  
  21. }  
  22.   
  23. @Mixin([DivingAbility, FlyingAbility])  
  24. class JamesBondVehicle implements Vehicle {  
  25.     String getName() { "James Bond's vehicle" }  
  26. }  
  27.   
  28. assert new Plane().fly() ==  
  29.        "I'm the Concorde and I fly!"  
  30. assert new Submarine().dive() ==  
  31.        "I'm the Yellow Submarine and I dive!"  
  32.   
  33. assert new JamesBondVehicle().fly() ==  
  34.        "I'm the James Bond's vehicle and I fly!"  
  35. assert new JamesBondVehicle().dive() ==  
  36.        "I'm the James Bond's vehicle and I dive!"  
You don't inherit from various interfaces and inject the same behavior in each subclass, instead you mixin the categories into your class. Here, our marvelous James Bond vehicle gets the flying and diving capabilities through mixins.

An important point to make here is that unlike @Delegate which can inject interfaces into the class in which the delegate is declared, @Mixin just does runtime mixing — as we shall see in the metaprogramming enhancements further down in this article.

Supplement
Delegate Transformation
Java doesn't provide any built-in delegation mechanism, and so far Groovy didn't either. But with the @Delegate transformation, a class field or property can be annotated and become an object to which method calls are delegated...

Dynamic object orientation - Using power features
This section presents three power features that Groovy supports at the language level: GPath, the Spread operator, and the use keyword...


[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...