0

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:

  1. Use cat and uniq, then redirect the output to the original file:
    cat authorized_keys | uniq > authorized_keys
    
  2. 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.

AdminBee
  • 22,803
vicky
  • 1

2 Answers2

1

Your first solution won't work because you're reading and writing to a file at the same time. Doing so causes an empty file because a redirection, i.e. the '>' symbol is handled by your shell and instructs the shell to first open the file for writing, thus emptying it. Then your command, i.e.

cat authorized_keys | uniq

is executed, but since the file authorized_keys is now empty it results in an empty output. To avoid problems like this, you can use a temporary file, like you used authorized_keys_2 to store the input then overwrite the original file with the temporary one

0

To expand upon SeetheMoar's answer...

From the bash man page

Redirecting Output

Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.

Your file authorized_keys does exist, and so is truncated to zero, before the command is executed.

You then cat a zero length file, hence the resulting file is empty.

This process is explained in depth by this answer to What are the shell's control and redirection operators?

Greenonline
  • 1,851
  • 7
  • 17
  • 23