Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ' Argument list too long' error?

Tags:

bash

shell

perl

ive executed this command to delete all occurrences of "#" from a huge amount of files (about 3000)in folder train,

perl -pi -e "s/#//g" /Users/Kian/Desktop/Acsegment/espslabs/train/*

but I got this error: /bin/bash: /usr/bin/perl: Argument list too long

Can anyone suggest a way to avoid this error?

like image 434
KianStar Avatar asked Dec 12 '22 01:12

KianStar


1 Answers

That's what xargs is all about.

ls /Users/Kian/Desktop/Acsegment/espslabs/train |
   xargs perl -i -pe's/#//g'

find can do it too, and it allows you to select which files to modify much more flexibly.

find /Users/Kian/Desktop/Acsegment/espslabs/train \
   -type f -maxdepth 1 \
   -exec perl -i -pe's/#//g' {} +

Of course, you could also generate the file list in Perl.

perl -i -pe'BEGIN { @ARGV = map glob, @ARGV } s/#//g' \
    '/Users/Kian/Desktop/Acsegment/espslabs/train/*'

But you have to escape spaces in the path unless you use bsd_glob.

perl -i -pe'
   use File::Glob qw( bsd_glob );
   BEGIN { @ARGV = map bsd_glob($_), @ARGV }
   s/#//g
' '/Users/Kian/Desktop/Acsegment/espslabs/train/*'
like image 94
ikegami Avatar answered Jan 01 '23 05:01

ikegami