본문 바로가기

Engineering/__00. Linux

rm: Argument list too long - Solution (펌)


(펌) http://linuxconfig.org/bash-bin-rm-argument-list-too-long-solution


1. Symptoms

This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:
-bash: /bin/rm: Argument list too long
# getconf ARG_MAX 2097152 Example:
# rm *
-bash: /bin/rm: Argument list too long

2. Solution

There are number of solutions for this problem. Firstly, if there are no files within a directory we would like to keep, the best and fastest solution is to simply remove entire directory and recreate it again. Before you proceed make sure that the directory does not contain files you would like to keep and that you take a note regarding a permissions and ownership of this directory.
$ cd ../
$ ls -d mydirectory
$ rm -fr mydirectory
$ mkdir mydirectory
The other solution is to engage a for loop and remove all files one be one:
$ for i in *; do rm "$i"; done
or much faster by use of printf and xargs:
$ printf '%s\0' * | xargs -0 rm
or
$ time echo -n * | tr ' ' '\0' | xargs -0 rm
However, the above solution has its own limitation as it also may remove files we would like to keep if no proper regular expression in in place. As for an example here we will remove only *.txt files:
$ for i in *.txt; do rm "$i"; done
The last and proposed solution is to use regex to split all file into smaller batches. Find a similar pattern in all files and split them into a smaller groups. For example first remove,copy or move all files which start with a and have extension txt, after that all files which start with b and so on.:
$ rm a*.txt
$ rm b*.txt
You can also do the above for all letters using a for loop:
$ for i in $( echo {a..z} ); do rm $i*.txt ; done


'Engineering > __00. Linux' 카테고리의 다른 글

[Ubuntu] runlevel  (0) 2016.02.05
Ubuntu Upgrade  (0) 2016.01.08
[PHP] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted  (0) 2015.05.14
file 정보 확인  (0) 2015.04.07
[File System] (GPT) 16TB 이상 사용하기  (0) 2015.03.20