title: How to sort files by name using a shell script
body: <p>I would like to sort all files by date with a Shell script.</p>
  
  <p>For example, in <code>/Users/KanZ/Desktop/Project/Test/</code> there are the files <code>M1.h</code>, <code>A2.h</code> and <code>F4.h</code>. </p>
  
  <p>Each file has a different time. How do I sort all these files from oldest to current by date and time?</p>
  
  <p>Currently I have a rename script:</p>
  
  <pre><code>cd /Users/KanZ/Desktop/Project/Test/ 
  n=1
  for file in *.jpg;
  do 
    echo $file prefix=M file_name=M$n.jpg 
    echo $file_name n=$(( $n+1 ))
    mv $file $file_name 
  done 
  </code></pre>
  
  <p>The first time I run script the JPG files will be <code>M1.jpg</code>, <code>M2.jpg</code> and <code>M3.jpg</code> but if I add a new file named <code>A1.jpg</code> to this directory and run the script again, <code>M1.jpg</code>, <code>M2.jpg</code> and <code>M3.jpg</code> will be replaced by <code>M4.jpg</code> (before running the script, this file was named <code>A1.jpg</code>) because the first letter is <code>A</code> and came before <code>M</code>.</p>
  
  <p>I would like to get <code>M1</code>, <code>M2</code>, <code>M3</code> and <code>M4.jpg</code>.</p>