1

I would like to rename my files using Bash by removing 4 characters in the middle.

Original file names:

dwe_hiul123456.pdf dwe_lkyd345876.pdf dwe_ythn157843.pdf 

I want to remove the 4 letters in the middle so the new file names would be:

dwe_123456.pdf dwe_345876.pdf dwe_157843.pdf 

The 4 letters will always follow the _ and there will always be one _ in the file name. All of the file names are the same length the the four characters I want to remove are in the same place.

I'm really new to this and don't know what version of anything I am using other than I am using Windows 10. I was trying to do this through the Git Bash command Window.

4
  • 2
    Welcome to the site. Are the 4 characters alway the ones following the _ in the filename? Is there exactly one _ in the filenames? Please update the question when answering. Commented Nov 3, 2021 at 16:08
  • Please edit your question and tell us what operating system you are using and, if Linux, which one. The tools you have available depend on this information. Commented Nov 3, 2021 at 16:17
  • @roaima - That doesn't work for me because the code they wrote is replacing the same text in each file. My text is different in every file. All of the file names are the same length the the four characters I want to remove are in the same place. Commented Nov 3, 2021 at 19:50
  • "I am using Windows 10" strongly implies that you're not asking about Unix & Linux! Commented Nov 11, 2021 at 16:34

2 Answers 2

2

If you have perl-rename (available as rename on Debian-based systems, perl-rename on others), you can do:

$ rename -n 's/_..../_/' *pdf dwe_hiul123456.pdf -> dwe_123456.pdf dwe_lkyd345876.pdf -> dwe_345876.pdf dwe_ythn157843.pdf -> dwe_157843.pdf 

The -n causes rename to only print what it would do without making any changes. Once you ahve confirmed it works as you want, remove the -n and run the command again to actually rename the files:

rename 's/_..../_/' *pdf 

If you can't use rename for some reason, you can also do it with a simple shell loop:

for file in *pdf; do echo mv -- "$file" "${file/_????/_}" done 

Once again, if you are confident this does what you want, remove the echo to actually rename the commands:

for file in *pdf; do mv -- "$file" "${file/_????/_}" done 
4
  • Without the dot *pdf would mv files like dwe_ythn157843pdf, or there's some reason you use it that way? Commented Nov 3, 2021 at 16:27
  • @schrodigerscatcuriosity just habit. Extensions aren't really a thing in the *nix world, with very few exceptions, and *pdf would match both foo.pdf and foopdf, so it will work fine for the OP. Commented Nov 3, 2021 at 16:54
  • Thank you! The second version (without the rename) worked perfectly. Commented Nov 5, 2021 at 12:56
  • You're welcome, @hosta. If one of the answers here solved your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites. Commented Nov 5, 2021 at 13:17
1

Using parameter expansion:

$ for i in *.pdf; do echo mv -- "$i" "${i/_[a-z][a-z][a-z][a-z]/_}" # actual `mv` command # mv -- "$i" "${i/_[a-z][a-z][a-z][a-z]/_}" done mv -- dwe_hiul123456.pdf dwe_123456.pdf mv -- dwe_lkyd345876.pdf dwe_345876.pdf mv -- dwe_ythn157843.pdf dwe_157843.pdf 

If you are happy with the echo result, remove it to actually perform the command.

1
  • Thank you! This also worked. Commented Nov 5, 2021 at 13:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.