LoginSignup
34
52

More than 5 years have passed since last update.

よく使うfind,grep,sed組み合わせ

Posted at

よく忘れるので覚書です(随時更新)

実行環境

CentOS 6.7

find

ファイル名に完全一致するファイル一覧を取得する

find ./ -name '.htaccess'

結果例:

./example/.htaccess

複数ファイル名に部分一致するファイル一覧を取得する

find ./ -name "*Test.php" -o -name "*.xml"

※-o オプションで-nameをつなげることが可能
結果例:

./tests/CallableResolverTest.php
./tests/ContainerTest.php
./tests/RouteTest.php
./phpcs.xml

シンボリックリンクの一覧を表示する

find ./ -type l -ls

結果例:

1450260    0 lrwxrwxrwx   1 dev  dev   6 Apr 25 12:53 ./hogehoge -> tests/

grep

特定の文字列を含むファイル一覧を取得する

grep -r 'NotFoundException' ./

結果例:

./tests/AppTest.php:        $this->setExpectedException('Slim\Exception\NotFoundException');
./tests/AppTest.php:     * @throws \Slim\Exception\NotFoundException
./tests/AppTest.php:            throw new NotFoundException($req, $res);
./tests/AppTest.php:     * @expectedException \Slim\Exception\NotFoundException
./tests/AppTest.php:            throw new NotFoundException($req, $res);

特定の文字列を含むファイル一覧のファイル名のみ取得する

grep -r -l 'NotFoundException' ./

結果例:

./.git/index
./Slim/Container.php
./Slim/App.php
./Slim/Exception/NotFoundException.php
./Slim/Exception/ContainerValueNotFoundException.php
./tests/ContainerTest.php
./tests/AppTest.php

特定の文字列を含むファイル一覧のファイル名と行数を取得する

grep -r -n 'ContainerValueNotFoundException' ./

結果例:

./Slim/Container.php:14:use Slim\Exception\ContainerValueNotFoundException;
./Slim/Container.php:112:     * @throws ContainerValueNotFoundException  No entry was found for this identifier.
./Slim/Container.php:120:            throw new ContainerValueNotFoundException(sprintf('Identifier "%s" is not defined.', $id));

find+grep

特定のファイル名かつ特定文字列を含むファイル

find ./ -name "*Test.php" |xargs grep -l 'application/x-www-form-urlencoded'

結果例:

./tests/Http/RequestTest.php:            'Content-Type' => 'application/x-www-form-urlencoded',
./tests/Http/RequestTest.php:            'Content-Type' => 'application/x-www-form-urlencoded',
./tests/Http/RequestTest.php:        $request->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
./tests/Http/RequestTest.php:            'Content-Type' => 'application/x-www-form-urlencoded',

特定のファイル名かつ特定文字列を含むファイルのファイル名のみ

find ./ -name "*Test.php" |xargs grep -l 'application/x-www-form-urlencoded'

結果例:

./tests/Http/RequestTest.php

sed

特定のファイルの文字列を置換する

sed -i -e 's/name/new/g' index.php

※sedのデリミタは変更可能なので以下でも結果は同じになる

sed -i -e 's:name:new:g' index.php
sed -i -e 's@name@new@g' index.php

実行前:

index.php
 $app->get('/hello[/{name}]', function ($request, $response, $args) {$
     $response->write("Hello, " . $args['name']);$
     return $response;$
 })->setArgument('name', 'World!');$

実行後:

index.php
 $app->get('/hello[/{new}]', function ($request, $response, $args) {$
     $response->write("Hello, " . $args['new']);$
     return $response;$
 })->setArgument('new', 'World!');$

特定のファイルの文字列を行数を指定して置換する

sed -i -e '34 s/name/new/g' index.php

実行前:

index.php
34 $app->get('/hello[/{name}]', function ($request, $response, $args) {$
35     $response->write("Hello, " . $args['name']);$
36     return $response;$
37 })->setArgument('name', 'World!');$

実行後:

index.php
34 $app->get('/hello[/{new}]', function ($request, $response, $args) {$
35     $response->write("Hello, " . $args['name']);$
36     return $response;$
37 })->setArgument('name', 'World!');$

特定のファイルの文字列を複数行指定して置換する

sed -i -e '34,35 s/name/new/g' index.php

※この場合は34行目~35行目まで
実行前:

index.php
34 $app->get('/hello[/{name}]', function ($request, $response, $args) {$
35     $response->write("Hello, " . $args['name']);$
36     return $response;$
37 })->setArgument('name', 'World!');$

実行後:

index.php
34 $app->get('/hello[/{new}]', function ($request, $response, $args) {$
35     $response->write("Hello, " . $args['name']);$
36     return $response;$
37 })->setArgument('name', 'World!');$
34
52
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
52