Quantcast
Channel: multiple file rename based on modification date - Ask Ubuntu
Viewing all articles
Browse latest Browse all 3

Answer by CAB for multiple file rename based on modification date

$
0
0

Having done something like this before, I can assure you it's easy to make a typo that will give you an even larger problem that you already have. Use this information at your own risk.

At bash prompt;

i=0 ; for f in `ls -rt` ; do i=$(expr $i + 1); mv $f $i.txt; done
  • i acts as a counter
  • ls -rt lists files in reverse order by modification time - if you've copied, moved or otherwise modified the files, this may not give you the file ordering you expect. Check it carefully by running ls -rt as a separate command and check it - twice.
  • for f inls -rt; do iterates of the list of files with 'f' taking on each file name
  • i=$(expr $i + 1) increments the counter
  • mv $f $i.txt renames the file using the counter, and adding the .txt extension
  • done closes the loop
  • note that commands in the loop are separated by semicolons, so if you add another command, follow it with a semicolon

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>