LoginSignup
7
4

More than 1 year has passed since last update.

chmod や mv すると "bash: Argument list too long" が出るようになった

Last updated at Posted at 2019-07-22

bash: /bin/chmod: Argument list too long
bash: /bin/mv: Argument list too long

macOS や Linux などの bash で、特定のディレクトリで chmod 0744 *mv すると、-bash: /bin/chmod: Argument list too long エラーが出るようになった。他のディレクトリでは動く

エラー内容
$ cd ~/path/to/my/heavy/dir
$ chmod 0744 *
-bash: /bin/chmod: Argument list too long
エラー内容
$ mv ~/path/to/my/heavy/dir/*.png ~/path/to/be/moved
-bash: /bin/mv: Argument list too long

TL; DR (今北産業)

  1. 対象ファイルの数が多い場合に Argument   引数list too long大杉エラー     が出ます。
  2. find コマンドで1つ1つ chmodmv コマンドに渡すと処理できます。
  3. find の末尾に \; を付けるのがポイント。
基本構文
find . -type f -exec <コマンド> {} \;
実行例(chmodの場合)
$ cd ~/path/to/my/heavy/dir
$ find . -type f -exec chmod 0744 {} \;
  • find . でカレント・ディレクトリ以下のファイル/ディレクトリを出力
  • -type f で対象をファイルに限定
  • -exec chmod 0744chmod コマンドを指定。パーミッションは 744
  • {}find から渡されたファイル名に置き換わる。ここでは chmod の引数として扱われる。
  • \; でファイル1つ1つに対して処理。+ にすると一括処理になるので同じエラーになる。

mv(移動)の場合は、一旦 find のみで動作確認(パスの出力を確認)してから、残りの -exec 〜 を付けるようにするとミスがなくていいと思います。

大量の特定ファイルをmvする例
$ # ちゃんと一覧を返すか確認
$ find ./path/dir/from -name "*.png" 
$ # 移動
$ find ./path/dir/from -name "*.png" -exec mv {} /path/dir/to \;
...

TS; DR

1万件以上のファイルやディレクトリを mv もしくは chmod したい

ファイル数が1万件以上もあるディレクトリのファイルのパーミッションを chmod コマンドで一括変更しようとしました。環境は macOS Mojave と CentOS 7 の bash です。

作業したいディレクトリに移り、いつもどおり単純に chmod 0744 * すると -bash: /bin/chmod: Argument list too long エラーが出ました。

「えっ?! 引数、なんかおかしい?」と思うも、構文を何度確認しても問題が見当たらず悩みました。

調べてみると、どうやら * を指定したため処理対象のファイル一覧が一気に渡されたことが原因のようです。つまり、「Argument list too long」は「引数(Argument)が多い」のではなく、「引数として渡された一覧(Argument list)が多い」というエラーでした。

$ # ディレクトリ内のファイル数(1.4万件以上)
$ ls -U1 | wc -l
   14754

$ # パーミッションがファイルによってバラバラ
$ ls -la | head -10
total 637288
drwxrwxrwx  14756 admin  staff   501704  7 21 18:53 .
drwxrwxrwx     18 admin  staff      612 10 19  2018 ..
-rw-r--r--      1 admin  staff     7346  7 10 05:47 10014788acc2adbf6c4b.json
-rwxr--r--      1 admin  staff     4571  7  7 21:34 100149cb080117f43f4c.json
-rw-r--r--      1 admin  staff     2139  7 10 13:14 1001600e8b45634fcf4e.json
-rwxr--r--      1 admin  staff     4122  7  7 00:34 1001add7479c31fbae67.json
-rwxrwxrwx      1 admin  staff      952  2  5 07:20 1001c974bd0ecc29b012.json
-rwxrwxrwx      1 admin  staff    33438  7 15 18:49 10026d96f2634ba36362.json
-rwxrwxrwx      1 admin  staff    13730  7 17 07:53 1002745259893326ccfd.json

$ # ファイル数が多すぎて chmod できない
$ chmod 0744 *
-bash: /bin/chmod: Argument list too long

$ # find で1ファイルごとに処理 (いささか時間がかかる)
$ find . -type f -exec chmod 0744 {} \;

$ # パーミッションが揃った
$ ls -la | head -10
total 637288
drwxrwxrwx  14756 admin  staff   501704  7 21 18:53 .
drwxrwxrwx     18 admin  staff      612 10 19  2018 ..
-rwxr--r--      1 admin  staff     7346  7 10 05:47 10014788acc2adbf6c4b.json
-rwxr--r--      1 admin  staff     4571  7  7 21:34 100149cb080117f43f4c.json
-rwxr--r--      1 admin  staff     2139  7 10 13:14 1001600e8b45634fcf4e.json
-rwxr--r--      1 admin  staff     4122  7  7 00:34 1001add7479c31fbae67.json
-rwxr--r--      1 admin  staff      952  2  5 07:20 1001c974bd0ecc29b012.json
-rwxr--r--      1 admin  staff    33438  7 15 18:49 10026d96f2634ba36362.json
-rwxr--r--      1 admin  staff    13730  7 17 07:53 1002745259893326ccfd.json
$ # 🎉

動作確認環境

  • macOS Mojave(OSX 10.14.5)
    • bash: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
    • chmod: BSD General Commands, July 08, 2004
  • CentOS Linux 7 (Core)
    • bash: GNU bash, バージョン 4.2.46(2)-release (x86_64-redhat-linux-gnu)
    • chmod: GNU coreutils 8.22

参考文献

7
4
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
7
4