The reason behind this Error
The main cause of this error lies in the argument buffer space, when we try to search for a file or try to remove a file the asterisk(*) expands the number of arguments in the shell. If the number of those arguments is greater than the argument buffer space then this error occurs, as the bash is not able to handle it.
The file command is expanded as follows:
ls -lrt file1 file2 file3 file4 ..................... | wc -l
Resolving the Error
Let us explore the various ways that we can use to handle this error.
- Method 1: Using the find command to iterate over the files
- Method 2: Splitting the file manually
- Method 3: Using For Loops
- Method 4: Completely delete the directory
Let’s go through all these methods:
Method 1: Using the find command to iterate over the files
find. -iname "file*" | xargs ls -lrt | wc -l
First, with the help of the find command, we find all the files starting with the word file and then with the help of xargs command the wc and -l flags are executed.
The error is not showing this time.
Method 2: Splitting the file manually
By using this way we make the length of the argument provided shorter so that it fits in buffer space.
ls -lrt file12* | wc -l
ls -lrt file22* | wc -l
Since the values are much smaller than earlier the error is resolved.
Method 3: Using For Loops
This approach takes the use of loops for iterating over the required files, skipping away the unwanted results.
for k in file*; do echo “$k”; done | wc -l
Note: This method is comparatively slower than the other methods.
Method 4: Completely delete the directory
If we want to get rid of all the files in the directory, but we are unable to do so because of the large number of files then we can use this way. Simply delete the complete directory and recreate it again, the error will not occur.
rm -rf /home/osboxes/temp
cd home/osboxes
mkdir temp
Note: After the creation of the new directory all the previous permissions and rights of users and groups will be lost and will have to be created again.
So this was the explanation of the error Argument List Too Long in Linux along with the cause and ways to resolve it.
The “Argument List Too Long” Error in Linux Commands
There are times when we encounter directories that contain a large number of files, if we try to perform certain operations on those particular directories we may get the error Argument List Too Long. In this article, we will see the cause of this error and how to resolve this error.
We have created a directory(temp) with a large number of files (>200K). All the operations will be performed on that directory. The OS used will be Ubuntu for the demonstration.