Saturday, February 5, 2022

[SOLVED] Filter files in all subdirectories and save them with the same name?

Issue

How to remove duplicate lines in every file in directory and save the fixed data in the same file with a single line command ?

the following do not work :

   find . -type f -execdir cat {} | sort | uniq > {} \; 

   find . -type f | xargs -I{} cat {} | sort | uniq > {}

   find . -type f | xargs -I{} cat {} | sort | uniq > {}.new && mv {}.new {}

the '>' symbol breaks the chain ..


Solution

Put a shell wrapper in the exec:

find . -type f -exec sh -c 'sort "$1" | uniq > "$1.new" && mv "$1.new" "$1"' _ {} \;


Answered By - William Pursell
Answer Checked By - David Marino (WPSolving Volunteer)