Linux command line / Move files filtered by lexicographic order

linuxshellsortingunix

I have some files in a directory that are named IMG_0001.jpgIMG_9999.jpg. I want to move to another directory the files that lexicographically have a name greater than IMG_9431.jpg How can I do that?

Best Answer

You can use sort and sed to get a list of files grater than some-string like this:

$ ls -v
0?#Li  23?24  E.See  NULib  Yoush  ce-Su  edint  ethat  ibble  itwil  lines  of16   plesA  ryGen  t6?#C  witht  #ver
0?#mo  25?i   Examp  NYWAR  along  cribb  edist  ev     ibrar  ix B.  mapfo  ofthe  ppend  sdist  tetot  y8?#9  (atyo
0,22   27?#   FORAP  NextA  areFo  dacop  edwar  frees  ic11   lPubl  mored  oftwa  publi  sefor  theGN  yGene  )1995
1.scr  02111  Finla  Peter  aryis  datio  eful,  ftheG  ight(  landJ  mport  on23#  ralPu  se,or  tunde  yofth  ,USA.
1-200  ARTIC  GIMPT  RANTY  avere  difyi  eitan  ftwar  imbal  lbeus  ncerK  on,MA  raryG  shedb  t,wri  ytheF  ;ifno
2John  Appen  HANTA  Softw  bleof  dix B  enera  ght(C  impli  ld7?#  nc.,5  oolki  raryi  simpl  ublic  #19?#  ;with
4?#Th  BILIT  HOUTA  TNESS  blic2  e.py4  enthe  gtk26  ingar  lePla  ngpix  opyri  rdraw  sion.  ucanr  #Bost   Code
5?#GT  Backi  K-The  U17?#  brary  e.pyB  eralP  he21   ion)a  lesPr  nseas  oshMa  reeSo  sion2  undat  #Free
9Temp  Conte  Libra  ULARP  cDona  eExam  ermso  hehop  ion,I  lesTa  ntsB.  ouldh  re;yo  slibr  uropt  #Lice
13?#1  Copyr  Licen  URPOS  ceive  eFoun  erver  her12  islib  lescr  nylat  outev  ribut  s,Spe  utWIT  #MERC
15?#b  C)200  Matti  YorFI  cense  eGNUL  etail  hisli  ite33  lesim  n;eit  ple.p  rthet  s.18   vpyth  #Thi

$ mkdir greater-than-sion

Here is the magic:

$ find -type f -print0 | 
  sort -z |
  sed -z '1,/sion/d' | 
  xargs -0 mv -t greater-than-sion

Lines:

  1. print a list of files, separated by NUL instead of newlines (-print0)
  2. sort them
  3. delete lines lower than (inclusive) some string (here sion) – note that this only works with GNU sed, which implements a -z option to parse NUL-terminated input
  4. pass this list to mv with xargs

And the desired result:

$ ls -R
.:
 Code  ;ifno  25?i   avere  Conte  edist  erver  GIMPT               ic11   K-The  lines  nseas  opyri  raryG  se,or
#19?#  ;with  27?#   Backi  Copyr  edwar  etail  greater-than-sion/  ight(  landJ  lPubl  ntsB.  oshMa  raryi  sefor
#Bost  0?#Li  2John  BILIT  cribb  eExam  ethat  gtk26               imbal  lbeus  mapfo  NULib  ouldh  rdraw  shedb
#Free  0?#mo  4?#Th  bleof  dacop  eFoun  ev     HANTA               impli  ld7?#  Matti  nylat  outev  re;yo  simpl
#Lice  0,22   5?#GT  blic2  datio  eful,  Examp  he21                ingar  lePla  mored  NYWAR  Peter  reeSo  sion.
#MERC  02111  9Temp  brary  difyi  eGNUL  Finla  hehop               ion)a  lescr  mport  of16   ple.p  ribut
#Thi   1.scr  along  C)200  dix B  eitan  FORAP  her12               ion,I  lesim  n;eit  ofthe  plesA  rthet
#ver   1-200  Appen  cDona  e.py4  enera  frees  hisli               islib  lesPr  nc.,5  oftwa  ppend  ryGen
(atyo  13?#1  areFo  ceive  e.pyB  enthe  ftheG  HOUTA               ite33  lesTa  ncerK  on,MA  publi  s,Spe
)1995  15?#b  ARTIC  cense  E.See  eralP  ftwar  ibble               itwil  Libra  NextA  on23#  ralPu  s.18
,USA.  23?24  aryis  ce-Su  edint  ermso  ght(C  ibrar               ix B.  Licen  ngpix  oolki  RANTY  sdist

./greater-than-sion:
sion2  Softw  t6?#C  theGN  tunde  ublic  ULARP  uropt  utWIT  witht  yGene  YorFI  ytheF
slibr  t,wri  tetot  TNESS  U17?#  ucanr  undat  URPOS  vpyth  y8?#9  yofth  Yoush
Related Question