linux:commands:file_directory

文書の過去の版を表示しています。


ファイル&ディレクトリ関連

filename.txt というファイルを / (ルートディレクトリ)配下のすべてのファイルから検索する。

$ find / -name filename.txt
/home/tomoyan/filename.txt
/home/tomoyan/Documents/filename.txt

dhcp-range を含むファイルをルート配下のサブディレクトリすべて検索する。ただし、バイナリファイルと /proc ディレクトリは除外する。

$ sudo grep -rI dhcp-range / --exclude-dir=dir

-r サブディレクトリも対象とする。
-I バイナリファイルは除外する。
–exclude-dir 指定されたディレクトリは除外する。

tar.gz、.tgz(圧縮)

$ tar zcvf filename.tar.gz targetdir/

tar.gz、.tgz(解凍)

$ tar zxvf filename.tar.gz

bzip2 コマンドが必要

$ sudo dnf install bzip2 -y

tar.bz2(圧縮)

$ tar jcvf filename.tar.bz2 targetdir/

tar.bz2(解凍)

$ tar jxvf filename.tar.bz2

-C オプションで出力先ディレクトリを指定

$ tar jxvf filename.tar.bz2 -C ~/work

tar.xz(圧縮)

$ tar Jcvf filename.tar.xz targetdir/

tar.xz(解凍)

$ tar Jxvf filename.tar.xz
$ split -b "数値" "分割したいfile名" "変更後のファイルの接頭語"

※ ファイルのバイト数で変換したい場合 -b 数値 を使用。
数値の最後にmをつけるとメガバイト、kをつけるとキロバイトで数値指定可能
例) split -b 8m “分割したいfile名” “変更後のファイルの接頭語” → 8メガバイトで分割

$ split -b 512m filename.tar.bz2 filename.tar.bz2.
$ ll filename.tar.bz2*
-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
$ sha256sum filename.tar.bz2
566a4e3f15a20b86ee1777664fcbda02df57ec6ad4a65a0340e8c9cd6c558cbe  filename.tar.bz2
$ cat filename.tar.bz2.* >> filename-1.tar.bz2
$ sha256sum filename-1.tar.bz2
566a4e3f15a20b86ee1777664fcbda02df57ec6ad4a65a0340e8c9cd6c558cbe  filename-1.tar.bz2
$ tar jcvf filename.tar.bz2 targetdir/ --exclude /exclude/dirname
$ 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
$ gzip filename1.txt filename2.txt
$ ls
filename1.txt.gz filename2.txt.gz
$ gzip -d filename1.txt.gz
$ ls
filename.txt

7zip コマンド を参照。

 ファイルを 15MB 単位に分割する。

$ split -b 15m filename.tar.bz2 filename.tar.bz2.

 以下のように分割される。

filename.tar.bz2.aa
filename.tar.bz2.ab
filename.tar.bz2.ac

 上記のファイルを結合する。

$ cat filename.tar.bz2.* filename.tar.bz2

書式:

$ mkdir [-pv] [-m mode] [--help] [--version] [--] directory...
オプション説明
-p directory に指定されたパスに存在しない親フォルダが含まれる場合はエラーとせずに作成する。
-v 作成したディレクトリごとにメッセージ表示。-p と一緒に使用する。
-m mode 作成されるディレクトリのアクセス権を指定する。mode は chmod と同じ。

実行例:

$ mkdir tmp
$ mkdir -m 755 cgi-bin
$ mkdir -pv usr/bin
mkdir: created directory `usr'
mkdir: created directory `usr/bin'

iso ファイルのデバイスへの書き込み。

$ sudo cp Windows\ 10\ version\ 1809.iso /dev/sdb
$ rm fileName.ext
rm: remove 通常ファイル 'fileName.ext'? y[Enter]
$ rm -f fileName.ext
$ rm -r directoryName
rm: descend into directory 'directoryName'? y[Enter]
rm: descend into directory 'directoryName/subDirectory'? y[Enter]
...
$ rm -rf directoryName

dir_name の容量表示

$ du -h -s dir_name
1.6G    dir_name

dir_name 配下のサブディレクトリ毎の容量表示

$ du -h -s dir_name/*
1.6G    dir1
0.2G    dir2
0.3G    dir3

dir_name 配下のすべてのサブディレクトリとファイル容量表示(降順, 上位50件)
※ファイルのみの表示はできない😅

$ du -kha dir_name/* --exclude={exclude_dir1,exclude_dir2,exclude_dir3} | sort -rn | head -50

find コマンドならファイルのみサイズ表示できる🤤

$ 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

容量の昇順ソート

$ du -h -s dir_name/* | sort -h
0.2G    dir2
0.3G    dir3
1.6G    dir1

容量の降順ソート

$ du -h -s dir_name/* | sort -hr
1.6G    dir1
0.3G    dir3
0.2G    dir2

du コマンドのヘルプ

$ du --help
Usage: du [OPTION]... [FILE]...
  or:  du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
  -0, --null            end each output line with NUL, not newline
  -a, --all             write counts for all files, not just directories
      --apparent-size   print apparent sizes, rather than disk usage; although
                          the apparent size is usually smaller, it may be
                          larger due to holes in ('sparse') files, internal
                          fragmentation, indirect blocks, and the like
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -b, --bytes           equivalent to '--apparent-size --block-size=1'
  -c, --total           produce a grand total
  -D, --dereference-args  dereference only symlinks that are listed on the
                          command line
  -d, --max-depth=N     print the total for a directory (or file, with --all)
                          only if it is N or fewer levels below the command
                          line argument;  --max-depth=0 is the same as
                          --summarize
      --files0-from=F   summarize disk usage of the
                          NUL-terminated file names specified in file F;
                          if F is -, then read names from standard input
  -H                    equivalent to --dereference-args (-D)
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
      --inodes          list inode usage information instead of block usage
  -k                    like --block-size=1K
  -L, --dereference     dereference all symbolic links
  -l, --count-links     count sizes many times if hard linked
  -m                    like --block-size=1M
  -P, --no-dereference  don't follow any symbolic links (this is the default)
  -S, --separate-dirs   for directories do not include size of subdirectories
      --si              like -h, but use powers of 1000 not 1024
  -s, --summarize       display only a total for each argument
  -t, --threshold=SIZE  exclude entries smaller than SIZE if positive,
                          or entries greater than SIZE if negative
      --time            show time of the last modification of any file in the
                          directory, or any of its subdirectories
      --time=WORD       show time as WORD instead of modification time:
                          atime, access, use, ctime or status
      --time-style=STYLE  show times using STYLE, which can be:
                            full-iso, long-iso, iso, or +FORMAT;
                            FORMAT is interpreted like in 'date'
  -X, --exclude-from=FILE  exclude files that match any pattern in FILE
      --exclude=PATTERN    exclude files that match PATTERN
  -x, --one-file-system    skip directories on different file systems
      --help     display this help and exit
      --version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/du>
or available locally via: info '(coreutils) du invocation'

du - コマンド (プログラム) の説明 - Linux コマンド集 一覧表
ファイズの大きいファイルを特定する:du, find, sort:Technical tips:Media hub
duコマンドで覚えておきたい使い方8個 | 俺的備忘録 〜なんかいろいろ〜
【 find 】コマンド(応用編その2)――検索したファイルを指定したフォーマットで表示する
findした結果を指定したフォーマットで表示したい | 雑廉堂の雑記帳

$ ln -s [src] [dist]

例:

$ 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

lessコマンドはmoreコマンドに似ているが、とても高機能なページャである。

書式:

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]...

オプションの詳細は、man lessを参照。

実行例:

$ 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キーを入力する。

ファイルのみ

$ sudo sh -c 'find . -type f -print | xargs chmod 644'

ディレクトリのみ

$ sudo sh -c 'find . -type d -print | xargs chmod 755'

中身がランダムなデータの巨大ファイルを作成する。
if には /dev/random または /dev/urandom を指定する。

$ 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
$ lsof

lsof - Wikipedia

特定のディレクトリ間を素早く移動するには

  • linux/commands/file_directory.1663798954.txt.gz
  • 最終更新: 2022/09/22 07:22
  • by ともやん