Small Cheese

How To Fix Linux Argument List Too Long Error?

The Linux system is running low on disk space and you would like to archive those log files created by various applications scheduled to run as daily cronjob.

Unfortunately, as you type ls *.lod in the log file directory, the ls command rejected with error message “-bash: /bin/ls : Argument list too long”. Besides, the rm and mv commands also failed with the same “argument list too long” message!

Resolve Linux ls argument list too long error.

The ls command works just fine if to list all files in the directory, i.e. without using wildcard characters such as asterisk (*), question mark (?), etc. That is why ls -ltr works!

But you have to use wildcard character to move a subset of log files in a directory to another directory and archive it with tar command. So how?

Trick to fix “argument list too long” error in Linux

There are many tricks to fix this “scary” error and here are just two of the tricks which I think are useful for most general cases:

1) The find command is not subjected to argument list too long error when using wildcard characters. So, you can use find command to do two actions in 1 go – return the subset of files and execute external command to work on the returned files.

For example, this sample find command list all log files suffixed with .lod file extension in current directory, each of the files is moved by mv command to LogFile folder in user’s home directory:
find . -type f -name "*.lod" -exec mv {} $HOME/LogFile \;

2) If for some reason the log file name doesn’t contains date information that can be used as pattern to move out log files of particular year, then this trick will help:
ls -ltr --full-time | awk '{if ($6 ~ /2008-??-??/){print "mv "$9" $HOME/LogFile"}}' > move2008logfile.sh

The ls command display complete time information and sort files by time of file created/changed. The ls command output is piped to awk command to construct the move file instruction using mv command, which is in turned redirected to a script file named move2008logfile.sh

Now, type less move2008logfile.sh to confirm the list of log files to be moved is correct. If that is correct, then just execute that script file will do.


Custom Search