2011-11-12 2 views
1

私はデプロイスクリプトを作成しています。ディレクトリ内のすべての.lessファイルに対してlessコンパイラを実行する必要があります。これは、次のfindコマンドで行うのは簡単です:Bash find:-execで使用するために一致する名前を変更する

find -name "*.less" -exec plessc {} {}.css \; 

main.lessという名前のファイルとフォルダに対して、このコマンドを実行した後、私はmain.less.cssという名前のファイルが残っていますが、私はそれがmain.cssになりたいです。

このコマンドでは、結果のファイルの無限部分を簡単に取り除くことができます:rename 's/\.less//' *.cssしかし、私は-execの使用について新しいことを学びたいと考えています。

-execパラメータで使用しているファイルの名前を変更することはできますか?

ありがとうございます!

答えて

3

あなたのfindコマンドは、非標準のGNU拡張のカップルを使用している:

  • あなたはどこ見つけるの状態はありません、これはPOSIXでエラーですGNU findは現在のディレクトリを選択します。
  • 孤立していない{}を使用すると、POSIXの検索では展開されません。ここで

ほとんどで動作するはず1つのライナーは実装を見つけ、あなたの二重の延長問題解決です:Solaris 10およびそれ以上の年齢で

find . -name "*.less" -exec sh -c "plessc \$0 \$(dirname \$0)/\$(basename \$0 less)css" {} \; 

をPATHでない場合、sh -cksh -cに置き換える必要がありますPOSIX準拠。

2

いいえ、直接行うことはできません。 {}のみを使用して、完全なファイル名を直接挿入することができます。しかし、エグゼクティブでは、あなたはawkのようなものを入れなければなりません。または、出力をパイプ経由で別のプログラムにリダイレクトすることもできます。

findmanからページ:

-exec command ; 
      Execute command; true if 0 status is returned. All following 
      arguments to find are taken to be arguments to the command until 
      an argument consisting of `;' is encountered. The string `{}' 
      is replaced by the current file name being processed everywhere 
      it occurs in the arguments to the command, not just in arguments 
      where it is alone, as in some versions of find. Both of these 
      constructions might need to be escaped (with a `\') or quoted to 
      protect them from expansion by the shell. See the EXAMPLES 
      section for examples of the use of the -exec option. The 
      specified command is run once for each matched file. The command 
      is executed in the starting directory. There are unavoidable 
      security problems surrounding use of the -exec action; you 
      should use the -execdir option instead. 
関連する問題