I have a file authorized_keys
and want to deduplicate the content, i.e. remove duplicate entries.
I found two possible solutions to achieve this:
- Use
cat
anduniq
, then redirect the output to the original file:cat authorized_keys | uniq > authorized_keys
- Use
sort
with the-o
option:sort -u ~/.ssh/authorized_keys -o ~/.ssh/authorized_keys
However, only the second option works. The first approach will result in an empty file.
If I redirect the output to a different file, as in
cat authorized_keys | uniq > authorized_keys_2
the new file with has correct content, while
cat authorized_keys | uniq > authorized_keys
leaves me with an empty authorized_keys
.
Please help me understand the difference between these two methods.
uniq
, so the first way should actually besort authorized_keys | uniq
– muru Mar 31 '22 at 02:26