#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    237
    Rep Power
    64

    Changing File Name Prefix


    I know this can be done, but it's a little too "guru level" for me...

    I have a directory with sub-directories...

    Most (but not all) of the files have a standard prefix: prefix-something.php (or js or css)

    I would like the change the prefix on all of them (recursively from the root of the tree): sometingelse.something.php

    Suggestions?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    105
    Decide how you'd do it for one file, and then use that as the -exec target for a find command, or use the find command to feed filenames to a loop.

    To change one file, you might try:

    file="filename" ; tail=${file#prefix-} ; mv $file newprefix-$tail

    So then, the loop would become:

    for i in $( find /directory -type f ) ; do tail=${i#prefix-} ; mv $i newprefix-$tail ; done

    This doesn't account for the path prior to the actual filename, but it could be handled by stripping it off into another variable and tacking it back in on the mv command. That would look like:

    for i in $( find /directory -type f ) ; do dir=${i%/*} ; file=${i##*/} ; tail=${file#prefix-} ; mv ${dir}/$i ${dir}/newprefix-$tail ; done

    (This is all off-the-cuff, and not tested in any known environment. Your mileage may vary.)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    105
    It's now tested. Given a series of directories (testdir, containing testdir1, testdir2, ...), each directory containing files in the form of prefix-file1.test, prefix-file2.test, ...:

    R5057499:~ rpn01$ for i in $( find testdir -type f ) ; do dir=${i%/*} ; file=${i##*/} ; tail=${file#prefix-} ; echo mv ${dir}/$i ${dir}/newprefix-$tail ; done
    mv testdir/testdir/prefix-file1.test testdir/newprefix-file1.test
    mv testdir/testdir/prefix-file2.test testdir/newprefix-file2.test
    mv testdir/testdir/prefix-file3.test testdir/newprefix-file3.test
    mv testdir/testdir/prefix-file4.test testdir/newprefix-file4.test
    mv testdir/testdir/prefix-file5.test testdir/newprefix-file5.test
    mv testdir/testdir1/testdir/testdir1/prefix-file1.test testdir/testdir1/newprefix-file1.test
    mv testdir/testdir1/testdir/testdir1/prefix-file2.test testdir/testdir1/newprefix-file2.test
    mv testdir/testdir1/testdir/testdir1/prefix-file3.test testdir/testdir1/newprefix-file3.test
    mv testdir/testdir1/testdir/testdir1/prefix-file4.test testdir/testdir1/newprefix-file4.test
    mv testdir/testdir1/testdir/testdir1/prefix-file5.test testdir/testdir1/newprefix-file5.test
    mv testdir/testdir2/testdir/testdir2/prefix-file1.test testdir/testdir2/newprefix-file1.test
    mv testdir/testdir2/testdir/testdir2/prefix-file2.test testdir/testdir2/newprefix-file2.test
    mv testdir/testdir2/testdir/testdir2/prefix-file3.test testdir/testdir2/newprefix-file3.test

    Change the prefixes, point to the right base directory, and remove the echo, and your job is done. Thanks for the exercise.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    105
    And a slight correction. I had the directory repeating in the source name. Make it:

    R5057499:~ rpn01$ for i in $( find testdir -type f ) ; do dir=${i%/*} ; file=${i##*/} ; tail=${file#prefix-} ; echo mv $i ${dir}/newprefix-$tail ; done
    mv testdir/prefix-file1.test testdir/newprefix-file1.test
    mv testdir/prefix-file2.test testdir/newprefix-file2.test
    mv testdir/prefix-file3.test testdir/newprefix-file3.test
    mv testdir/prefix-file4.test testdir/newprefix-file4.test
    mv testdir/prefix-file5.test testdir/newprefix-file5.test
    mv testdir/testdir1/prefix-file1.test testdir/testdir1/newprefix-file1.test
    mv testdir/testdir1/prefix-file2.test testdir/testdir1/newprefix-file2.test
    mv testdir/testdir1/prefix-file3.test testdir/testdir1/newprefix-file3.test
    mv testdir/testdir1/prefix-file4.test testdir/testdir1/newprefix-file4.test
    mv testdir/testdir1/prefix-file5.test testdir/testdir1/newprefix-file5.test
    mv testdir/testdir2/prefix-file1.test testdir/testdir2/newprefix-file1.test
    mv testdir/testdir2/prefix-file2.test testdir/testdir2/newprefix-file2.test
    mv testdir/testdir2/prefix-file3.test testdir/testdir2/newprefix-file3.test
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    105
    And I missed the "not all" files part. Let's make the final command:

    for i in $( find testdir -type f -iname "prefix-*" ) ; do dir=${i%/*} ; file=${i##*/} ; tail=${file#prefix-} ; echo mv $i ${dir}/newprefix-$tail ; done

    Sorry to drag you through my entire "train of thought" here, but at least you can follow along with the logic I used to solve the problem.

IMN logo majestic logo threadwatch logo seochat tools logo