文書の表示以前のリビジョンバックリンク文書の先頭へ この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。 ====== ファイル&ディレクトリ関連 ====== ===== ファイル検索[find] ===== / (ルートディレクトリ)配下の filename.txt というファイルをすべて検索する...🤔\\ <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> filename.txt </pre></html></WRAP> <WRAP color_result><html><pre> /home/tomoyan/filename.txt /home/tomoyan/Documents/filename.txt </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] ===== dhcp-range を含むファイルをルート配下のサブディレクトリすべて検索する。ただし、バイナリファイルと /proc ディレクトリは除外する。\\ <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">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** サブディレクトリも対象とする。\\ **-I** バイナリファイルは除外する。\\ **--exclude-dir** 指定されたディレクトリは除外する。\\ </WRAP> ===== ファイルの圧縮/解凍[tar] ===== ==== tar.xz ==== **tar.xz(圧縮)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> Jcvf filename.tar.xz targetdir/ </pre></html></WRAP> </WRAP> **tar.xz(解凍)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> Jxvf filename.tar.xz </pre></html></WRAP> </WRAP> ==== tar.bz2 ==== **bzip2** コマンドのインストールが必要\\ <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>sudo</b> dnf install bzip2 <b class=HIK>-y</b> </pre></html></WRAP> </WRAP> **tar.bz2(圧縮)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> jcvf filename.tar.bz2 targetdir/ </pre></html></WRAP> </WRAP> **tar.bz2(解凍)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> jxvf filename.tar.bz2 </pre></html></WRAP> </WRAP> **-C** オプションで出力先ディレクトリを指定\\ <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> jxvf filename.tar.bz2 <b class=HIK>-C</b> ~/work </pre></html></WRAP> </WRAP> ==== tar.gz、.tgz ==== **tar.gz、.tgz(圧縮)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> zcvf filename.tar.gz targetdir/ </pre></html></WRAP> </WRAP> **tar.gz、.tgz(解凍)** <WRAP color_term> <WRAP color_command><html><pre> <b class=GRN>$</b> <b class=HIY>tar</b> zxvf filename.tar.gz </pre></html></WRAP> </WRAP> ==== split (ファイルの分割) ==== <code> $ split -b "数値" "分割したいfile名" "変更後のファイルの接頭語" </code> ※ ファイルのバイト数で変換したい場合 -b 数値 を使用。\\ 数値の最後にmをつけるとメガバイト、kをつけるとキロバイトで数値指定可能\\ 例) split -b 8m "分割したいfile名" "変更後のファイルの接頭語" → 8メガバイトで分割\\ <code> $ split -b 512m filename.tar.bz2 filename.tar.bz2. $ ll filename.tar.bz2* </code> <WRAP prewrap 100% #result> <code> -rw-r--r-- 1 root root 4051803928 6月 9 16:27 filename.tar.bz2 -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:32 filename.tar.bz2.aa -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:32 filename.tar.bz2.ab -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:32 filename.tar.bz2.ac -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:32 filename.tar.bz2.ad -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:32 filename.tar.bz2.ae -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:33 filename.tar.bz2.af -rw-r--r-- 1 tomoyan tomoyan 536870912 6月 9 16:33 filename.tar.bz2.ag -rw-r--r-- 1 tomoyan tomoyan 293707544 6月 9 16:33 filename.tar.bz2.ah </code> </WRAP> <code> $ sha256sum filename.tar.bz2 </code> <WRAP prewrap 100% #result> <code> 566a4e3f15a20b86ee1777664fcbda02df57ec6ad4a65a0340e8c9cd6c558cbe filename.tar.bz2 </code> </WRAP> <code> $ cat filename.tar.bz2.* >> filename-1.tar.bz2 $ sha256sum filename-1.tar.bz2 </code> <WRAP prewrap 100% #result> <code> 566a4e3f15a20b86ee1777664fcbda02df57ec6ad4a65a0340e8c9cd6c558cbe filename-1.tar.bz2 </code> </WRAP> ==== 特定のディレクトリやファイルを除外 ==== <code> $ tar jcvf filename.tar.bz2 targetdir/ --exclude /exclude/dirname </code> ==== 特定のファイルを含める ==== <code> $ find /var/www/vhosts -type f -name "*access_log*" -print | grep statistics/logs > apache_logs.txt $ tar jcvf apache_logs.tar.bz2 --files-from apache_logs.txt </code> ===== ファイルの圧縮/解凍[gzip] ===== ==== .gz(圧縮) ==== <code> $ gzip filename1.txt filename2.txt $ ls filename1.txt.gz filename2.txt.gz </code> ==== .gz(解凍) ==== <code> $ gzip -d filename1.txt.gz $ ls filename.txt </code> ===== ファイルの圧縮/解凍[7zip] ===== [[linux:7zip|7zip コマンド]] を参照。\\ ===== ファイル分割&結合[split, cat] ===== ファイルを 15MB 単位に分割する。 <code> $ split -b 15m filename.tar.bz2 filename.tar.bz2. </code> 以下のように分割される。 <code> filename.tar.bz2.aa filename.tar.bz2.ab filename.tar.bz2.ac </code> 上記のファイルを結合する。 <code> $ cat filename.tar.bz2.* filename.tar.bz2 </code> ===== ディレクトリの作成[mkdir] ===== 書式: <code> $ mkdir [-pv] [-m mode] [--help] [--version] [--] directory... </code> ^オプション^説明^ |-p |directory に指定されたパスに存在しない親フォルダが含まれる場合はエラーとせずに作成する。 | |-v |作成したディレクトリごとにメッセージ表示。-p と一緒に使用する。 | |-m //mode// |作成されるディレクトリのアクセス権を指定する。mode は chmod と同じ。 | 実行例: <code bash> $ mkdir tmp $ mkdir -m 755 cgi-bin $ mkdir -pv usr/bin mkdir: created directory `usr' mkdir: created directory `usr/bin' </code> ===== ファイルのコピー[cp] ===== iso ファイルのデバイスへの書き込み。 <code> $ sudo cp Windows\ 10\ version\ 1809.iso /dev/sdb </code> ===== ファイルの削除[rm] ===== <code bash> $ rm fileName.ext rm: remove 通常ファイル 'fileName.ext'? y[Enter] </code> ===== ファイルの削除(確認なし) [rm -f] ===== <code bash> $ rm -f fileName.ext </code> ===== ディレクトリをサブディレクトリを含めて削除[rm -r] ===== <code bash> $ rm -r directoryName rm: descend into directory 'directoryName'? y[Enter] rm: descend into directory 'directoryName/subDirectory'? y[Enter] ... </code> ===== ディレクトリをサブディレクトリを含めて削除(確認なし)[rm -rf] ===== <code bash> $ rm -rf directoryName </code> ===== ディレクトリの容量 [du] ===== du コマンドのヘルプ\\ <accordion id="du_help" collapsed="false"> <panel id="du_help_panel" type="default" title="+ ヘルプ [--help]"> <WRAP color_term> <WRAP color_command><html><pre> <span style="color:#FF8700"><b>$</b></span> <span style="color:#26A269">du</span> <span style="color:#A347BA">--help</span> </pre></html></WRAP> <WRAP color_result_long><html><pre> 使用法: du [OPTION]... [FILE]... または: du [OPTION]... --files0-from=F 各ファイルのデバイス使用量を集計します。ディレクトリは再帰的に処理されます。 長いオプションで必須となっている引数は短いオプションでも必須です。 -0, --null 出力行の区切りとして改行文字ではなく NUL を使用する -a, --all ディレクトリだけでなく、全てのファイルについて表示する --apparent-size デバイス使用量ではなく実際のサイズを表示する。実際の サイズは通常小さくなるが、(スパース) ファイル内のホール、 内部フラグメンテーション、間接ブロックなどの理由で 大きくなることもある。 -B, --block-size=SIZE SIZE の倍数として表示する。例: '-BM' は 1,048,576 バイト単位でサイズを表示する -b, --bytes '--apparent-size --block-size=1' と同じ -c, --total 総合計量を表示する -D, --dereference-args コマンドラインで指定されたシンボリック のみをたどる -d, --max-depth=N コマンド実行場所から N 階層以内のディレクトリの (--all 指定時はファイルも) 合計を表示する。 --max-depth=0 は --summarize と同じ --files0-from=F ファイル F で指定された NUL で区切られた名前の ファイルからデバイス使用量を読み取る F に - が指定された場合標準入力から読み込む -H --dereference-args (-D) と同様 -h, --human-readable 人間が読みやすい形式でサイズを表示する (例: 1K 234M 2G) --inodes ブロック使用量ではなく inode 使用量を表示する -k --block-size=1K と同様 -L, --dereference 全てのシンボリックリンクをたどる -l, --count-links ハードリンクされた場合その個数分サイズを数える -m --block-size=1M と同様 -P, --no-dereference シンボリックリンクをたどらない (デフォルト) -S, --separate-dirs 子ディレクトリのサイズを含めない --si -h と同様だが、1024 ではなく 1000 の累乗を表示する -s, --summarize 各引数の合計容量のみ表示する -t, --threshold=SIZE SIZE が正の場合は SIZE より小さいエントリを無視する。 SIZE が負の場合は SIZE より大きなエントリを無視する --time ディレクトリとその子ディレクトリに含まれる全て のファイルでの、最終更新時間を表示する --time=WORD 更新時間の代わりに WORD の時間を表示する。WORD には 次を指定できる: atime, access, use, ctime, status --time-style=STYLE 時間を STYLE の形式で表示する。STYLE には 次を指定できる: full-iso, long-iso, iso, +FORMAT FORMAT は 'date' と同様に解釈される -X, --exclude-from=FILE FILE 内のいずれかのパターンに一致するファイルを除外する --exclude=PATTERN PATTERN に一致するファイルを除外する -x, --one-file-system 異なるファイルシステム上のディレクトリはスキップする --help display this help and exit --version output version information and exit --block-size で指定した SIZE, DU_BLOCK_SIZE, BLOCK_SIZE およびBLOCKSIZE 環境変数 のうち、最初に指定されているサイズ単位で値が表示されます。それ以外の場合、デフォ ルトの単位は 1024 バイトになります (POSIXLY_CORRECT が設定されている場合 512 バ イト)。 SIZE 引数は整数で、追加で単位を指定できます (例えば 10M は 10*1024*1024 です)。 単位には K, M, G, T, P, E, Z, Y (1024 の累乗) や KB, MB, ... (1000 の累乗) が使用できます。 二進接頭辞も使用することもできます: KiB=K, MiB=M など。 GNU coreutils のオンラインヘルプ: <https://www.gnu.org/software/coreutils/> 翻訳に関するバグは <https://translationproject.org/team/ja.html> に連絡してください。 詳細な文書 <https://www.gnu.org/software/coreutils/du> (ローカルでは info '(coreutils) du invocation' で参照可能)。 </pre></html></WRAP> </WRAP> </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 コマンド集 一覧表]]\\ [[http://tech.clickyourstyle.com/articles/260|ファイズの大きいファイルを特定する:du, find, sort:Technical tips:Media hub]]\\ [[https://orebibou.com/ja/home/201507/20150721_001/|duコマンドで覚えておきたい使い方8個 | 俺的備忘録 〜なんかいろいろ〜]]\\ [[https://atmarkit.itmedia.co.jp/ait/articles/1607/20/news024.html|【 find 】コマンド(応用編その2)――検索したファイルを指定したフォーマットで表示する]]\\ [[https://www.rough-and-cheap.jp/linux/find_result_formatting/|findした結果を指定したフォーマットで表示したい | 雑廉堂の雑記帳]]\\ ===== シンボルリンクを張る[ln] ===== <code> $ ln -s [src] [dist] </code> 例: <code> $ ln -s /var/www www_root $ ls -al lrwxrwxrwx 1 tomoyan tomoyan 42 11月 1 13:15 2012 www_root-> /var/www $ ln -s /var/www $ ls -al lrwxrwxrwx 1 tomoyan tomoyan 42 11月 1 13:15 2012 www-> /var/www </code> ===== 前方/後方移動が可能なファイル表示[less] ===== lessコマンドはmoreコマンドに似ているが、とても高機能なページャである。\\ \\ 書式: <code> less -? less --help less -V less --version less [-[+]aBcCdeEfgGiImMnNqQrsSuUVWwXZ] [-b bufs] [-h lines] [-j line] [-k keyfile] [-K character set] [-{oO} logfile] [-p pattern] [-P prompt] [-t tag] [-T tagsfile] [-x tab] [-y lines] [-[z] lines] [+[+]cmd] [--] [filename]... </code> オプションの詳細は、man lessを参照。\\ \\ 実行例: <code> $ less filename.txt <- ファイルの内容が表示されて、↑↓キーで前方/後方移動ができる。 〜省略〜 (END) q <- 表示を終了するにはqキーを入力する。 $ ls -al | less <- コマンドの出力結果を、↑↓キーで移動表示ができる。 〜省略〜 (END) q <- 表示を終了するにはqキーを入力する。 $ less /usr <- ls -al | less と同じ。 合計 408 drwxr-xr-x 16 root root 4096 2008-12-08 14:47 ./ drwxr-xr-x 25 root root 4096 2009-01-22 21:10 ../ drwxr-xr-x 2 root root 4096 2007-06-08 17:07 X11R6/ drwxr-xr-x 2 root root 94208 2009-01-19 21:53 bin/ 〜省略〜 /usr (END) q <- 表示を終了するにはqキーを入力する。 </code> ===== ファイルのみ、ディレクトリのみに[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|翻訳]]\\ [[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/|翻訳]]\\ ファイルのみ - ❌ダメな例💩🤪\\ <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> ディレクトリのみ <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 d -print0 | xargs -0 chmod 755'</font> </pre></html></WRAP> </WRAP> ===== 巨大ファイルを作成[dd] ===== 中身がランダムなデータの巨大ファイルを作成する。\\ if には /dev/random または /dev/urandom を指定する。\\ <code> $ sudo dd if=/dev/urandom of=/mnt/vdb1/bigdata.bin bs=1M count=1024 1024+0 レコード入力 1024+0 レコード出力 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 9.50597 s, 113 MB/s </code> ===== プロセスがオープンしているファイルを表示 [lsof] ===== <code> $ lsof </code> [[https://ja.wikipedia.org/wiki/Lsof|lsof - Wikipedia]]\\ ===== 参考文献 ===== [[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|翻訳]]\\ linux/commands/file_directory.txt 最終更新: 2025/02/28 04:49by ともやん