3

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
  • 29,831
  • don't think you should update without upgrade so just install is probably better. –  May 19 '20 at 12:26
  • 1
    Hi, @bac0n, sudo apt update just updates the list of the repositories and their content, thus you have fresh information which packages (and their versions, resp. dependencies) are currently available in the remote repositories. This step is mandatory when you installing some package. Some people miss it and then comes here to ask why some packages are not available... On the other hand sudo apt upgrade does the actual update of the packages if some is available. Most Ubuntu manuals where a new package is installed begins with apt update... – pa4080 May 19 '20 at 14:10
  • Something more if you didn't do sudo apt update for a long time and trying to install something it is highly possible to end with broken packages. Fortunately sudo apt install -f fixes this issue in the most cases. – pa4080 May 19 '20 at 14:13
  • if you update the package list it may move out of sync with the system, new depends will not be installed with apt install and the package/rename may break. believe its best to leave the repository as is or upgrade before installing a package. –  May 19 '20 at 14:20
  • 1
    @bac0n, it is not matter of belief or opinion, please read: https://help.ubuntu.com/community/AptGet/Howto and https://askubuntu.com/q/222348/566421 – pa4080 May 19 '20 at 14:28
  • @bac0n while that's certainly true on distros like Arch Linux, it's not on apt-based distros. In fact, by default, Ubuntu runs apt update periodically without automatically installing them. If you open Software & Updates and switch to the "Updates" tab, you'll see how often that happens next to "Automatically check for updates". If it was a problem to run apt update without upgrade, then your system would break by just leaving it connected to the internet. – lights0123 May 20 '20 at 00:47
  • you have three indexes; srv_repo, pkt_idx, and the inst_pkt. Srv_repo will, of course, move forward with new packages added, this will put the srv <-> pkt_idx out of sync, so you update, now pkt_idx and the inst_pkt will come out of sync, so you upgrade. if you just install then apt will only be responsible for its package and its dependencies and if an installed package still is dependent on an old dependency (due to out of sync), the requirement will not be met and the installation will be rejected. its nothing dramatic and as pa4080 says it's just a decision where you like it to break :)= –  May 20 '20 at 08:26
  • ...but I still believe it's unnecessary to require something that will affect the whole system. –  May 20 '20 at 08:26
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
  • 136,215
  • 21
  • 243
  • 336
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.