Files and folders

Count lines, sort them and remove duplicates

How many lines a file has, the lines in order, each line once and how often each one appears.

Fill these in once
  1. Count the lines
    Terminal
    wc -l list.txt
  2. Show the lines in order
    Terminal
    sort list.txt

    Add -n to sort numbers by size or -r to reverse.

  3. Show each line once
    Terminal
    sort -u list.txt
  4. Count how often each line appears, most first
    Terminal
    sort list.txt | uniq -c | sort -rn
  5. Save the clean list to a new file
    Terminal
    sort -u list.txt > unique.txt
  1. Count the lines
    Terminal · Ubuntu / Debian
    wc -l list.txt
  2. Show the lines in order
    Terminal · Ubuntu / Debian
    sort list.txt

    Add -n to sort numbers by size or -r to reverse.

  3. Show each line once
    Terminal · Ubuntu / Debian
    sort -u list.txt
  4. Count how often each line appears, most first
    Terminal · Ubuntu / Debian
    sort list.txt | uniq -c | sort -rn
  5. Save the clean list to a new file
    Terminal · Ubuntu / Debian
    sort -u list.txt > unique.txt
  1. Count the lines
    PowerShell
    (Get-Content list.txt).Count
  2. Show the lines in order
    PowerShell
    Get-Content list.txt | Sort-Object

    Add -Descending to reverse.

  3. Show each line once
    PowerShell
    Get-Content list.txt | Sort-Object -Unique

    This ignores capital letters, so Apple and apple count as the same line.

  4. Count how often each line appears, most first
    PowerShell
    Get-Content list.txt | Group-Object | Sort-Object Count -Descending | Select-Object Count, Name
  5. Save the clean list to a new file
    PowerShell
    Get-Content list.txt | Sort-Object -Unique | Set-Content unique.txt