sort -u filename > newfilename

Read, sort and confirm uniqueness of entries in files and directories

For users looking to simplify management of large entries in files and directories, this command is the key to fun and simplicity. Using the power sort, only a couple of seconds are necessary to accomplish what would take minutes or hours in ?standard? client applications.

-7
2009-04-16 20:00:34

These Might Interest You

  • Returns true if user presses the key. Use it like Confirm "Continue" && do action


    12
    Confirm() { read -sn 1 -p "$1 [Y/N]? "; [[ $REPLY = [Yy] ]]; }
    frans · 2010-11-22 11:01:14 4
  • This version works across on all POSIX compliant shell variants.


    3
    Confirm() { echo -n "$1 [y/n]? " ; read reply; case $reply in Y*|y*) true ;; *) false ;; esac }
    eikenberry · 2010-11-22 16:54:16 0
  • I can remember "cp -av" on Unix like systems to copy files and directories. The same can be done on Windows without extra software, somewhat. The switches mean: /E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T. /H Copies hidden and system files also. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. /Z Copies networked files in restartable mode. /I If destination does not exist and copying more than one file, assumes that destination must be a directory. /K Copies attributes. Normal Xcopy will reset read-only attributes. /F Displays full source and destination file names while copying. I don't type that all the time, I stick it into a file called "cpav.cmd" and run that. echo xcopy /e/h/y /z/i /k /f %1 %2 > cpav.cmd cpav zsh zsh2 http://support.microsoft.com/kb/323007 Show Sample Output


    -3
    xcopy /e/h/y /z/i /k /f src dest
    piyo · 2009-02-13 16:25:09 0
  • A lot of files in one dir is not so cool for filesystem.


    0
    find . -type d | while read dir ; do num=`ls -l $dir | grep '^-' | wc -l` ; echo "$num $dir" ; done | sort -rnk1 | head
    stderr · 2012-12-13 13:13:16 0
  • recursively delete empty directories and directories which only contain empty directories using zsh globbing syntax. ** is for recursive globbing. *(/^F) matches all entries that are directories which are not full. If you only want to delete empty directories and not those directories which contained only empty directories and will be empty afterwards, just leave out the options to rmdir: rmdir **/*(/^F)


    0
    rmdir --ignore-fail-on-non-empty -p **/*(/^F)
    xro · 2012-09-18 20:28:46 0
  • This sorts files in multiple directories by their modification date. Note that sorting is done at the end using "sort", instead of using the "-ltr" options to "ls". This ensures correct results when sorting a large number of files, in which case "find" will call "ls" multiple times.


    2
    find . -type f -exec ls -l --full-time {} + | sort -k 6,7
    quadcore · 2012-08-03 22:22:51 0

What Others Think

less is being misused here. What you really mean is cat filename | sort, or instead: sort filename | uniq > newfilename If I am reading the manpage correctly, I think using the -u flag on sort will do the job of uniq: this will also do it: sort -u filename > newfilename
bwoodacre · 475 weeks ago
yeah that use of less didn't look right. -n sorts numerically -r reverses
tobiasboon · 475 weeks ago
confirming bwoodacre's reading of the man page (I use "sort -u" all the time). His second code snippet does the job.
amosshapira · 475 weeks ago
Nice recommendations guys, always very helpfull! Long live command line!
deijmaster · 474 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: