2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

任意のディレクトリ以下すべてのファイルの拡張子一覧の取得

Last updated at Posted at 2020-04-26

結論

とりあえず下記で解決。

 find ディレクトリ -type f | awk -F/ '{print $NF}' | awk -F. '{print $NF}' | sort | uniq

ただし、拡張子のないファイルの場合はファイル名が表示される。
また、隠しファイルで且つ拡張子がないファイルの場合は先頭の.が削除されて表示されることに注意。

現在のディレクトリ以下の場合は、

find ./ -type f -maxdepth 1 | awk -F/ '{print $NF}' | awk -F. '{print $NF}' | sort | uniq

findコマンドで検索する階層を指定(maxdepth)

検証

railsのディレクトリ構成でやってみる。例えばapp

まず、find ディレクトリ -type fで、ディレクトリ以下のファイルを再帰的にパスも含めて表示。

findでファイル名のみ表示

$ find app -type f 
app/mailers/application_mailer.rb
app/models/mask.rb
app/models/application_record.rb
app/models/concerns/.keep
app/models/user.rb
app/jobs/application_job.rb
app/controllers/application_controller.rb
app/controllers/users_controller.rb
app/controllers/concerns/.keep
app/views/layouts/application.html.erb
app/views/layouts/mailer.html.erb
app/views/layouts/mailer.text.erb
app/views/users/index.html.erb
app/views/users/index.json.jbuilder
app/views/users/show.json.jbuilder
app/views/users/edit.html.erb
app/views/users/show.html.erb
app/views/users/_user.json.jbuilder
app/views/users/_form.html.erb
app/views/users/new.html.erb
app/assets/config/manifest.js
app/assets/images/.keep
app/assets/javascripts/cable.js
app/assets/javascripts/users.coffee
app/assets/javascripts/application.js
app/assets/javascripts/channels/.keep
app/assets/stylesheets/scaffolds.scss
app/assets/stylesheets/application.css
app/assets/stylesheets/users.scss
app/helpers/users_helper.rb
app/helpers/application_helper.rb
app/channels/application_cable/connection.rb
app/channels/application_cable/channel.rb

awk -F/ '{print $NF}'で、ファイル名のみ表示。

awkの組み込み変数NRとNFについて解説

$ find app -type f | awk -F/ '{print $NF}'
application_mailer.rb
mask.rb
application_record.rb
.keep
user.rb
application_job.rb
application_controller.rb
users_controller.rb
.keep
application.html.erb
mailer.html.erb
mailer.text.erb
index.html.erb
index.json.jbuilder
show.json.jbuilder
edit.html.erb
show.html.erb
_user.json.jbuilder
_form.html.erb
new.html.erb
manifest.js
.keep
cable.js
users.coffee
application.js
.keep
scaffolds.scss
application.css
users.scss
users_helper.rb
application_helper.rb
connection.rb
channel.rb

awk -F. '{print $NF}'で、拡張子のみ(拡張子がない場合はファイル名)を表示

$ find app -type f | awk -F/ '{print $NF}' | awk -F. '{print $NF}'
rb
rb
rb
keep
rb
rb
rb
rb
keep
erb
erb
erb
erb
jbuilder
jbuilder
erb
erb
jbuilder
erb
erb
js
keep
js
coffee
js
keep
scss
css
scss
rb
rb
rb
rb

sortしてからuniq

 $ find app -type f | awk -F/ '{print $NF}' | awk -F. '{print $NF}' | sort | uniq
coffee
css
erb
jbuilder
js
keep
rb
scss

sortしないでuniqだと、連続して同じ箇所のみuniqされる。

$ find app -type f | awk -F/ '{print $NF}' | awk -F. '{print $NF}' | uniq       
rb
keep
rb
keep
erb
jbuilder
erb
jbuilder
erb
js
keep
js
coffee
js
keep
scss
css
scss
rb

現在のディレクトリ以下

total 72
drwxr-xr-x  19 knog  staff   608 12 22 01:11 ./
drwxr-xr-x   8 knog  staff   256  3 11 01:32 ../
drwxr-xr-x  12 knog  staff   384 12 22 01:11 .git/
-rw-r--r--   1 knog  staff  1380 12 22 01:11 .gitignore
-rw-r--r--   1 knog  staff    10 12 22 01:11 .ruby-version
-rw-r--r--   1 knog  staff  2105 12 22 01:11 Gemfile
-rw-r--r--   1 knog  staff  5504 12 22 01:11 Gemfile.lock
-rw-r--r--   1 knog  staff   374 12 22 01:11 README.md
-rw-r--r--   1 knog  staff   227 12 22 01:11 Rakefile
drwxr-xr-x  10 knog  staff   320 12 22 01:11 app/
drwxr-xr-x   8 knog  staff   256 12 22 01:11 bin/
drwxr-xr-x  15 knog  staff   480 12 22 01:11 config/
-rw-r--r--   1 knog  staff   130 12 22 01:11 config.ru
drwxr-xr-x   3 knog  staff    96 12 22 01:11 db/
drwxr-xr-x   4 knog  staff   128 12 22 01:11 lib/
-rw-r--r--   1 knog  staff    64 12 22 01:11 package.json
drwxr-xr-x   9 knog  staff   288 12 22 01:11 public/
drwxr-xr-x   3 knog  staff    96 12 22 01:11 storage/
drwxr-xr-x   3 knog  staff    96 12 22 01:11 vendor/

$ find ./ -type f -maxdepth 1 | awk -F/ '{print $NF}' | awk -F. '{print $NF}' | sort | uniq
Gemfile
Rakefile
gitignore
json
lock
md
ru
ruby-version

標準のrailsだとファイルがあまりないのでやる意味ないかも。


参考にさせていただきました。

findでファイル名のみ表示
awkの組み込み変数NRとNFについて解説
findコマンドで検索する階層を指定(maxdepth)

2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?