0

We have a repository of about 3000 MP3 files. Many of these files have our old domain name within their name. For example: somesong_oldDomainName.mp3

I need to SSH into the site, find all instances of those files and rename them with the new domain name. Example: somesong_NEWDomainName.mp3

I know the basic SSH commands but not something advanced like this.

Pretty sure it'll be a combination of multiple commands.

x29a
  • 1,761
  • 1
  • 24
  • 43
user1661286
  • 133
  • 1
  • 3
  • 12
  • ssh is just the way to establish communication with the remote site, there, if it is linux based you probably have some sort of shell, e.g. bash, so check out this: http://stackoverflow.com/questions/11053558/bash-rename-small-part-of-multiple-files-in-middle-of-name – x29a Aug 28 '13 at 15:54
  • Can you please verify which system the files reside on? Linux based? I meant to add the tags "bash, shell, remote-access" to your question but the reviewers did not think "a combination of multiple commands" might justify for a shell question. So do you get an interactive shell (and if yes which one) when you SSH into your mp3machine? – x29a Aug 28 '13 at 16:33
  • So I'm on a Linux server with Bash.... – user1661286 Aug 28 '13 at 19:03

1 Answers1

2

Assuming you get an interactive shell when you ssh into your linux server, this might be a possible way:

ssh user@machine-name-or-ip

then you will get some sort of terminal like

user@machine-name:~$

where you enter the commands to execute on that remote machine.

As mentioned in the comments, the answer here might just fit very well:

Bash: Rename small part of multiple files in middle of name

user@machine-name:~$ for i in *.mp3; do mv "$i" "$(echo "$i" | sed 's/_oldDomainName/_NEWDomainName/g')"; done

This assumes, your current directory is the one with all the MP3 files in it.

If you dont want interactivly operate on your files, e.g. because they change very often and you want a script to perform this action, SSH can also execute a command and/or shell script remotely.

To pass the command directly with the SSH call: SSH error when executing a remote command: "stdin: is not a tty"

To pipe a local shell script into the SSH connection: How to use SSH to run a shell script on a remote machine?

Run a remote shell script via SSH: how to run a script file remotely using ssh

Edit: Assume you are connected via SSH to your remote machine and have somewhat similar versions of bash and sed, it should work like this:

$ ls
bar_chosefil.mp3 boo_chosefil.mp3 foo_chosefil.mp3
$ for i in *.mp3; do mv $i $(echo $i | sed 's/chosefil/tamasha/g'); done
$ ls
bar_tamasha.mp3 boo_tamasha.mp3 foo_tamasha.mp3

Versions involved:

  • bash: 4.2.25
  • sed: 4.2.1
  • mv: 8.13

Edit 2: Updated the command to work with blanks in filenames

$ ls
asd chosefil.mp3 bar_chosefil.mp3 boo_chosefil.mp3 foo_chosefil.mp3
$ for i in *.mp3; do mv "$i" "$(echo "$i" | sed 's/chosefil/tamasha/g')"; done
$ ls
asd tamasha.mp3 bar_tamasha.mp3 boo_tamasha.mp3 foo_tamasha.mp3
Community
  • 1
  • 1
x29a
  • 1,761
  • 1
  • 24
  • 43
  • This suggestion seems to work on directories with one file only ! Not sure if that makes sense, but I tried it within directories that contain 1 file only and it renames the one file correctly. But when I run it within a directory with multiple files, I get erros saying: "FILE NAME" is not a directory!! I used this: for i in *.mp3; do mv $i $(echo $i | sed 's/chosefil/tamasha/g'); done – user1661286 Aug 29 '13 at 18:40
  • i assume your filenames contain blanks? i updated the answer to work with those as well – x29a Aug 30 '13 at 12:16
  • 1
    Better to use the shell's own substitution mechanism, like `mv "$i" "${i/_oldDomainName/_newDomainName}"` – tripleee Aug 30 '13 at 12:49