2

what is the "command line" "to remove certain character from the end of a file name . For example, subash.jpg.jpg. I want to remove ".jpg" from last. i have been having problem "renaming the files".

3 Answers3

5

rename is the perfect command for file renaming :) It has wide range of options than mv and can works with regular expressions too. In your case the following syntax must do the job:

rename 's/\.jpg\.jpg$/.jpg/' *.jpg.jpg

Explanation:

  • the expression s/searched/replace/ means substiture the searched expression with the replace string.
  • \.jpg\.jpg$ will match to any line that ends $ with .jpg.jpg, where the back slashes will escape the special meaning of the dot within the regex.
  • *.jpg.jpg will be expanded by the shell as list of all files that ends with .jpg.jpg within the current directory.

If the command doesn't exist at your system, you need to install it:

sudo apt update && sudo apt install rename
pa4080
  • 30,621
2

If the file name is contained in a shell variable, you can remove the shortest matching substring from the end using ${var%sub} ex.

$ f=subash.jpg.jpg

$ echo "${f%.jpg}"
subash.jpg

So for example

mv "$f" "${f%.jpg}"

You can also use a wildcard like ${f%.*} to remove an arbitrary dot suffix.

steeldriver
  • 142,475
1

Use mv command to rename a file. If the file is in your current working directory, the command would be:

mv ./subash.jpg.jpg ./subash.jpg

Try man mv for the documentation on the command.