Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I've got a directory with thousands of files and I want to delete those that contain specific text. When I try: Code: ls | grep -l "specific text" * ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    2

    "Argument list too long" - help with deleting files based on grep

    Hi,

    I've got a directory with thousands of files and I want to delete those that contain specific text.

    When I try:

    Code:
    ls | grep -l "specific text" * | xargs rm
    I get the error:

    Code:
    /bin/grep: Argument list too long
    Is there an easy way to get around this without having to move files into seperate folders and processing them in batches? I found an article on getting around this problem, but I'm kind of new to Linux and don't know how to apply this to my specific problem.

    Thanks

  2. #2
    Just Joined!
    Join Date
    Dec 2010
    Location
    India
    Posts
    45
    i dont think there is need of ls command in the front grep will search all the files in that directory .
    try without ls or you can try
    find . -maxdepth 1 -type f | xargs grep -l "specific text" | xargs rm -rf
    this might work . cant check as i dont have system ryt now

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    2
    Thanks Piyu, that worked perfectly !

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Location
    India
    Posts
    45
    welcome rob but let me know have you tried ur command without ls
    it worked or not

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...