Issue
i have a directory contains text files which contains raw http request as following
POST /test HTTP/1.1
Host: host.com
Content-Type: application/x-www-form-urlencoded
Accept-Language: en-US, gHNEY;q=0.9, *;q=0.5
Accept-Charset: utf-8, iso-8859-1;q=0.5, 3AG19;q=0.2, *;q=0.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5)
Accept: */*, text/XppPa
param1=ffff&
as you see that file named as 2b0c9d9254abb4a775023177380c2598.txt
which is the md5sum of url and this is not the case now but i want to mention it
what i want is search for files which is not containing parameters
and delete it so the rest of files in that dir would be only files contains params
what i tried is grep unique value found in those files containing params and i found the common thing is char &
which is written beside param value
so i tried the following command and dosn`t work
grep -lrIvE '&' . | xargs -0 rm -f --
the result was
./2b0c9d9254abb4a775023177380c2598.txt'$'\n': File name too long
and when i added capital Z to grep deleted all files
so is there anyway to do this and if i could filter the requests (files) contain unique params would be greaaaat
Thanks
Solution
Note: you cannot use the -0
option of xargs
without the -Z
option of grep
. The record separators would not match.
Your problem is that grep -v
matches all lines not containing &
. And all your files have at least one. Instead of searching non-matching lines you can search for matching ones and delete the files only if none is found:
find . -type f -print0 | while IFS= read -r -d '' f; do
grep -Iq '&' "$f" || printf 'rm -f %s\n' "$f"
done
Replace printf 'rm -f %s\n' "$f"
by rm -f "$f"
once you will be convinced that it does what you want.
Answered By - Renaud Pacalet Answer Checked By - Marie Seifert (WPSolving Admin)