Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform sort on all files in a directory?

How do I perform sort on all files in the directory?

I could have done it in python, but it seems to be too much of a hassle.

import os, glob
d = '/somedir/'

for f in glob.glob(d+"*"):
  f2 = f+".tmp"
  # unix~$ cat f | sort > f2; mv f2 f
  os.system("cat "+f+" | sort > "+f2+"; mv "+f2+" "+f)
like image 928
alvas Avatar asked Dec 02 '22 22:12

alvas


1 Answers

Use find and -exec:

find /somedir -type f -exec sort -o {} {} \;

For limiting the sort to the files in the directory itself, use -maxdepth:

find /somedir -maxdepth 1 -type f -exec sort -o {} {} \;
like image 182
devnull Avatar answered Dec 05 '22 10:12

devnull