Write a script that renames each file in the directory to 1.mp3,2.mp3, ..., 7.mp3

    1 answer 1

    #!/bin/sh export count=0; for i in `find -type f`; do mv ${i} `basename ${count}`.mp3; export count=`expr ${count} + 1`; done 
    1. backup data
    2. put in the file folder ..
    3. pray..

    Tests

     sh-4.1$ touch file{1..10} sh-4.1$ ls -l total 0 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file1 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file10 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file2 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file4 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file5 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file6 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file7 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file8 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 file9 sh-4.1$ export count=0; for i in `find -type f`; do mv ${i} `basename ${count}`.mp3; export count=`expr ${count} + 1`; done sh-4.1$ ls -l total 0 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 0.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 1.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 2.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 3.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 4.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 5.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 6.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 7.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 8.mp3 -rw-r--r-- 1 shev DomainUsers 0 Фев 20 12:20 9.mp3 
    • Anton, thank you very much, unfortunately only tomorrow I can run the script. - ks6692
    • @Anton Shevtsov Interestingly, in the conditions of the problem, only one directory or several? If one, then what for find ? Yes, and there is no need to export count. count = 1 for i in *; do mv $ {i} $ {count} .mp3 count = expr ${count} + 1 done - alexlz
    • 2
      @alexlz is like that, but we don’t know anything about the conditions of the problem, so this is an oak solution, and besides, I wrote in one line, gradually filling out a file .. That's right, yours and ours. And another hundred thousand options that descendants will write after us .. - Anton Shevtsov
    • 2
      Well, the secrecy of the conditions of the problem is a feature of hashcode.ru - alexlz
    • I forgot to check that it is a file, not a directory (although, depending on the situation, you can check for writing, and for belonging, etc.) count = 1 for i in *; do if [-f "$ {i}"] then mv $ {i} $ {count} .mp3 count = expr ${count} + 1 fi done "${i}" , apostrophes - in case of spaces in the name the file - alexlz