両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
linux:commands:file_directory [2024/01/22 13:36] – [編集 - Panel] ともやん | linux:commands:file_directory [2025/02/28 04:49] (現在) – [ファイルのみ、ディレクトリのみに[chmod]] ともやん |
---|
| |
===== ファイル検索[find] ===== | ===== ファイル検索[find] ===== |
filename.txt というファイルを / (ルートディレクトリ)配下のすべてのファイルから検索する。 | / (ルートディレクトリ)配下の filename.txt というファイルをすべて検索する...🤔\\ |
<code> | <WRAP color_term> |
$ find / -name filename.txt | <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">find</font> <u style="text-decoration-style:single">/</u> <font color="#A347BA">-name</font> filename.txt |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
/home/tomoyan/filename.txt | /home/tomoyan/filename.txt |
/home/tomoyan/Documents/filename.txt | /home/tomoyan/Documents/filename.txt |
</code> | </pre></html></WRAP> |
| </WRAP> |
| |
| . (カレントディレクトリ)配下の *.orig というファイルをすべて検索する...🤔\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">find</font> <u style="text-decoration-style:single">.</u> <font color="#A347BA">-name</font> <font color="#12488B"><b>*</b></font>.orig <font color="#A347BA">-type</font> f |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| ./kernel_b/nvidia-drm/nvidia-drm-drv.c.orig |
| ./kernel_b/nvidia/nv-acpi.c.orig |
| ./kernel_b/conftest.sh.orig |
| ./kernel/nvidia/nv-acpi.c.orig |
| ./kernel/nvidia-drm/nvidia-drm-drv.c.orig |
| ./kernel/Kbuild.orig |
| ./kernel/conftest.sh.orig |
| </pre></html></WRAP> |
| </WRAP> |
| |
| それらを削除する😱\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">find</font> <u style="text-decoration-style:single">.</u> <font color="#A347BA">-name</font> <font color="#12488B"><b>*</b></font>.orig <font color="#A347BA">-type</font> f <font color="#12488B"><b>|</b></font> <font color="#26A269">xargs</font> rm <font color="#A347BA">-rf</font> |
| </pre></html></WRAP> |
| </WRAP> |
| ※注意: パイプで xargs に渡すだけだと、パスにスペースを含むファイルが削除できない🤪\\ |
| |
| <html><code>-delete</code></html> オプションを使用すると削除できる😊\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">find</font> <u style="text-decoration-style:solid">.</u> <font color="#A347BA">-name</font> <font color="#A2734C">'*.orig'</font> <font color="#A347BA">-type</font> f <font color="#A347BA">-delete</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
===== 文字列を含むファイルを検索[grep] ===== | ===== 文字列を含むファイルを検索[grep] ===== |
dhcp-range を含むファイルをルート配下のサブディレクトリすべて検索する。ただし、バイナリファイルと /proc ディレクトリは除外する。\\ | dhcp-range を含むファイルをルート配下のサブディレクトリすべて検索する。ただし、バイナリファイルと /proc ディレクトリは除外する。\\ |
<code> | <WRAP color_term> |
$ sudo grep -rI dhcp-range / --exclude-dir=dir | <WRAP color_command><html><pre> |
</code> | <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">grep</font> <font color="#A347BA">-rI</font> dhcp-range <u style="text-decoration-style:solid">/</u> <font color="#A347BA">--exclude-dir=dir</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
| <WRAP mincode> |
**-r** サブディレクトリも対象とする。\\ | **-r** サブディレクトリも対象とする。\\ |
**-I** バイナリファイルは除外する。\\ | **-I** バイナリファイルは除外する。\\ |
**--exclude-dir** 指定されたディレクトリは除外する。\\ | **--exclude-dir** 指定されたディレクトリは除外する。\\ |
| </WRAP> |
| |
===== ファイルの圧縮/解凍[tar] ===== | ===== ファイルの圧縮/解凍[tar] ===== |
| |
===== ディレクトリの容量 [du] ===== | ===== ディレクトリの容量 [du] ===== |
dir_name の容量表示\\ | |
<WRAP prewrap 100%> | |
<code> | |
$ du -h -s dir_name | |
1.6G dir_name | |
</code> | |
</WRAP> | |
| |
dir_name 配下のサブディレクトリ毎の容量表示\\ | |
<WRAP prewrap 100%> | |
<code> | |
$ du -h -s dir_name/* | |
1.6G dir1 | |
0.2G dir2 | |
0.3G dir3 | |
</code> | |
</WRAP> | |
| |
dir_name 配下のすべてのサブディレクトリとファイル容量表示(降順, 上位50件)\\ | |
※ファイルのみの表示はできない😅\\ | |
<code> | |
$ du -kha dir_name/* --exclude={exclude_dir1,exclude_dir2,exclude_dir3} | sort -rn | head -50 | |
</code> | |
find コマンドならファイルのみサイズ表示できる🤤\\ | |
<code> | |
$ find ./dir_name -ls | sort -rh -k 7 | head -50 | |
$ find ./dir_name -printf "%s %M %n %u %g %b %TD %TR %p\n" | sort -hr | head -50 | |
</code> | |
| |
容量の昇順ソート\\ | |
<WRAP prewrap 100%> | |
<code> | |
$ du -h -s dir_name/* | sort -h | |
0.2G dir2 | |
0.3G dir3 | |
1.6G dir1 | |
</code> | |
</WRAP> | |
| |
容量の降順ソート\\ | |
<WRAP prewrap 100%> | |
<code> | |
$ du -h -s dir_name/* | sort -hr | |
1.6G dir1 | |
0.3G dir3 | |
0.2G dir2 | |
</code> | |
</WRAP> | |
| |
du コマンドのヘルプ\\ | du コマンドのヘルプ\\ |
<panel id="du_help_panel" type="default" title="+ ヘルプ [-h,--help]"> | <accordion id="du_help" collapsed="false"> |
| <panel id="du_help_panel" type="default" title="+ ヘルプ [--help]"> |
<WRAP color_term> | <WRAP color_term> |
<WRAP color_command><html><pre> | <WRAP color_command><html><pre> |
</WRAP> | </WRAP> |
</panel> | </panel> |
| </accordion> |
| |
| dir_name の容量表示\\ |
| <WRAP prewrap 100%> |
| <code> |
| $ du -h -s dir_name |
| 1.6G dir_name |
| </code> |
| </WRAP> |
| |
| dir_name 配下のサブディレクトリ毎の容量表示\\ |
| <WRAP prewrap 100%> |
| <code> |
| $ du -h -s dir_name/* |
| 1.6G dir1 |
| 0.2G dir2 |
| 0.3G dir3 |
| </code> |
| </WRAP> |
| |
| dir_name 配下のすべてのサブディレクトリとファイル容量表示(降順, 上位50件)\\ |
| ※ファイルのみの表示はできない😅\\ |
| <code> |
| $ du -kha dir_name/* --exclude={exclude_dir1,exclude_dir2,exclude_dir3} | sort -rn | head -50 |
| </code> |
| find コマンドならファイルのみサイズ表示できる🤤\\ |
| <code> |
| $ find ./dir_name -ls | sort -rh -k 7 | head -50 |
| $ find ./dir_name -printf "%s %M %n %u %g %b %TD %TR %p\n" | sort -hr | head -50 |
| </code> |
| |
| 容量の昇順ソート\\ |
| <WRAP prewrap 100%> |
| <code> |
| $ du -h -s dir_name/* | sort -h |
| 0.2G dir2 |
| 0.3G dir3 |
| 1.6G dir1 |
| </code> |
| </WRAP> |
| |
| 容量の降順ソート\\ |
| <WRAP prewrap 100%> |
| <code> |
| $ du -h -s dir_name/* | sort -hr |
| 1.6G dir1 |
| 0.3G dir3 |
| 0.2G dir2 |
| </code> |
| </WRAP> |
| |
[[https://kazmax.zpp.jp/cmd/d/du.1.html|du - コマンド (プログラム) の説明 - Linux コマンド集 一覧表]]\\ | [[https://kazmax.zpp.jp/cmd/d/du.1.html|du - コマンド (プログラム) の説明 - Linux コマンド集 一覧表]]\\ |
[[https://www.rough-and-cheap.jp/linux/find_result_formatting/|findした結果を指定したフォーマットで表示したい | 雑廉堂の雑記帳]]\\ | [[https://www.rough-and-cheap.jp/linux/find_result_formatting/|findした結果を指定したフォーマットで表示したい | 雑廉堂の雑記帳]]\\ |
| |
===== シンボルリンクをはる[ln] ===== | ===== シンボルリンクを張る[ln] ===== |
<code> | <code> |
$ ln -s [src] [dist] | $ ln -s [src] [dist] |
</code> | </code> |
| |
===== ファイルのみ、ディレクトリのみに[chmod] ===== | ===== ファイルのみ、ディレクトリのみに[find, xargs, chmod] ===== |
ファイルのみ | 参考: [[so>questions/76331097/how-can-i-make-xargs-execute-for-all-files-found-by-find-command-in-bash-scr|linux - How can I make 'xargs' execute for all files found by 'find' command in bash script? - Stack Overflow]] [[gtr>https://stackoverflow.com/questions/76331097/how-can-i-make-xargs-execute-for-all-files-found-by-find-command-in-bash-scr|翻訳]]\\ |
<code> | [[https://www.linuxtoday.com/blog/xargs-find-commands/|How to Use the xargs and find commands | Linux Today]] [[gtr>https://www.linuxtoday.com/blog/xargs-find-commands/|翻訳]]\\ |
$ sudo sh -c 'find . -type f -print | xargs chmod 644' | |
</code> | ファイルのみ - ❌ダメな例💩🤪\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">sh</font> <font color="#A347BA">-c</font> <font color="#A2734C">'find . -type f -print | xargs chmod 644'</font> |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| chmod: './lib/plugins/indexmenu/scripts/fancytree/readme/icons_layout__from' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: 'ods.png' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: './lib/plugins/indexmenu/scripts/fancytree/skin-xp/icons-rtl__old__blue' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: 'laxy' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: 'expanders.gif' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: './lib/plugins/indexmenu/scripts/fancytree/skin-xp/icons__old__blue' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: 'lazy' にアクセスできません: そのようなファイルやディレクトリはありません |
| chmod: 'expanders.gif' にアクセスできません: そのようなファイルやディレクトリはありません |
| </pre></html></WRAP> |
| </WRAP> |
| |
| ファイルのみ - 🙆🏻♀️良い例🥰\\ |
| <WRAP round tip 90%> |
| 空白文字を含むファイル名は問題を引き起こす可能性がある🤔\\ |
| xargs と find は、空白文字ではなくヌル文字で区切る GNU 拡張機能を使用し、この問題に対処できる😉\\ |
| </WRAP> |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">sh</font> <font color="#A347BA">-c</font> <font color="#A2734C">'find . -type f -print0 | xargs -0 chmod 644'</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
ディレクトリのみ | ディレクトリのみ |
<code> | <WRAP color_term> |
$ sudo sh -c 'find . -type d -print | xargs chmod 755' | <WRAP color_command><html><pre> |
</code> | <font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">sh</font> <font color="#A347BA">-c</font> <font color="#A2734C">'find . -type d -print0 | xargs -0 chmod 755'</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
===== 巨大ファイルを作成[dd] ===== | ===== 巨大ファイルを作成[dd] ===== |
===== 参考文献 ===== | ===== 参考文献 ===== |
[[http://www.atmarkit.co.jp/flinux/rensai/linuxtips/012easycd.html|特定のディレクトリ間を素早く移動するには]]\\ | [[http://www.atmarkit.co.jp/flinux/rensai/linuxtips/012easycd.html|特定のディレクトリ間を素早く移動するには]]\\ |
| [[so>questions/42828021/removing-files-with-rm-using-find-and-xargs|linux - Removing files with rm using find and xargs - Stack Overflow]] [[gtr>https://stackoverflow.com/questions/42828021/removing-files-with-rm-using-find-and-xargs|翻訳]]\\ |
| |