Home > Net >  Rename a file when I don't know its name - shell script
Rename a file when I don't know its name - shell script

Time:11-30

I am trying to rename a file which is the only file in a directory.

It is a podcast download which changes name each day so I don't know what it is called but it always ends in .MP3

I want to rename it to news.mp3

I have tried the following based on another solution on this site but it appends the news to the file

 #!/bin/sh
for file in *.MP3; do
    mv "$file" "${file/.MP3/news.mp3}"
done

CodePudding user response:

If it's the only file in the directory you can just write the following command:

mv directory_name/* directory_name/news.mp3

CodePudding user response:

In case there are few files or if dir is empty:

shopt -s nullglob
src="/path/to/dir/with/files"
dst="/destanation/folder"
i=1
cd "$src"
for f in *; do
   mv "$f" "$dst/new_name_$((i  ))"
done
  • Related