-1

I have a directory which contains multiple files. I need to rename these files.

This is how the file names looks like:

snap-file-name-1.txt
snap-file-name-2.txt
snap-file-name-3.txt

I need to remove "snap" and ".txt" from these files.

-file-name-1
-file-name-2
-file-name-3

How do I do that with mv command?

Asdfg
  • 11,362
  • 24
  • 98
  • 175

3 Answers3

1

Use sed to manipulate the file name:

ls | while read file; do
    mv -- ${file} $(sed -n 's/snap\(.*\).txt/\1/p' <<<${file})
done
MosheZada
  • 2,189
  • 1
  • 14
  • 17
0

With Bash you could do something like this to rename those files:

#!/bin/bash
files=$(find -type f -name 'snap-file-name-*.txt')
for f in $files
do
  mv "$f" "$(echo $f | sed -n 's/snap\(.*\).txt/\1/p')"
done
Striezel
  • 3,693
  • 7
  • 23
  • 37
0

Use rename command with specific regex pattern:

rename 's/snap([-a-z0-9]+)\.txt$/$1/' *.*
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105