0

Suppose i want to rename 3 files in the same directory. Example: test1.gzip test2.gzip test3.gzip

Now want to rename all the above files to something like,

test1_20180518.gzip test2_20180518.gzip test3_20180518.gzip

Now how can get the result? Someone please help!!

Please tell me, how to do that in bash scripting?

closed as unclear what you're asking by Jeff Schaller, Kusalananda, Isaac, G-Man, GAD3R May 29 '18 at 8:58

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    What happened when you tried? What are the zip file names? You know you can only rename one file at a time? – Jeff Schaller May 28 '18 at 16:22
  • 2
    In the title you say “gunzip” but in the body you say “zip” — what are they? – Jeff Schaller May 28 '18 at 16:23
  • 1
    Also, gunzip is a verb; it expands the contents of a gzip file. Are you trying to do that, or rename, or what? – Jeff Schaller May 28 '18 at 16:23
  • 2
    You might also want to read How do I ask a good question? – glenn jackman May 28 '18 at 16:40
  • 3
    Search this site for "rename multiple files" and you'll find lots of similar questions. – glenn jackman May 28 '18 at 16:46
1

Check your "rename" version with rename -V. If you see:

  • "util-linux" then

    rename .gzip _$(date "+%Y%m%d").gzip *.gzip
    
  • "File::Rename" then

    rename 'chomp(my $date = `date "+%Y%m%d"`); s/\.gzip/_$date.gzip/' *.gzip
    
0

Posible solution (you need chose one in loop body):

#!/bin/bash

pattern="pattern"
i=0

for file in `find <your_path> -type f -name '*.zip'`
do
    extension="${file##*.}"
    filename="${file%.*}"

    # without extenstion
    mv "$file" "$filename-$pattern.$extension"

    # whole filename
    mv "$file" "$pattern-$i"
    i=$((i + 1))
done

Not the answer you're looking for? Browse other questions tagged or ask your own question.