I have many folders that I have ended up with too many empties in the folder names and I need to remove all the empty and leave the folders with $1.empty

Of course my logic is flawed here as I am not sure how to get the new file name. I can only get what is matching from regex.


for I in `cat filelist`;
     # for renaming, use mv
    NEWNAME = `$I.empty`
    echo $NEWNAME 

The goal is to replace folders that have $ empty.empty then be $.empty. The folders that do not contain empty.empty should be left alone.

enter image description here


Use rename:

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

You might need to install rename:

sudo apt install rename

Alternative with pure bash:

for d in *empty; do
    mv -i "$d" "${d% empty*}.empty"
  • 2
    bring back your other suggestions please :) Not everyone wants to install rename :) – Smurfz87 Oct 30 '19 at 14:59
  • rename is installed by default in ubuntu. Also, the other options did not care about OPs wish that the final name should be $.empty. They made it be $ empty. – pLumo Oct 30 '19 at 15:00
  • Ye, now I have a few folders named $ empty and a few named $.empty lol opps – Jamie Hutber Oct 30 '19 at 15:01
  • 1
    Sorry for that, you may use rename 's/ empty$/.empty/' *' empty' to fix that. – pLumo Oct 30 '19 at 15:01
  • 1
    @Smurfz87 Now I have a good working solution without rename. – pLumo Oct 30 '19 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.