9

I worked out a neat way ro get rid of duplicate files. You know, the ones that contain "(1).", "(2).", "(3).", etc. in their names. In a terminal window, at the command line, you type in "rm "[backslash]").", but without the quotes. That will do it. The [backslash] "\" means the next character is accepted as just a character, not part of a pair of parentheses. This works when nothing else will. Incidently, I tried to type the "\" into the "rm" command, but it failed to display properly, so I put the term [backslash] there instead.

The appearance of "\ " in a folder or a filename shows the presence of a space there as well. The use of spaces in names is not all that common, unless you work with Windows. Windows just has you bracket the "whole path\file name" in double quotes. You can do that in Ubuntu as well, or just stick a backslash "\" in front of the space. But what if you want to replace the space with a different character instead? Like a hyphen or underscore? How would you do that for all folders and files at once?

Or what if you decide to just remove the spaces, just pack the rest of the characters together? How would you do that?

And here is a toughy: Just get rid or any leading or trailing spaces. even if there is more than one present.

And to wrap it up, how to detect and delete and files that are completely empty. Or folders that are empty.

3 Answers 3

20
  • To remove any number of leading spaces from file names you can use rename (prename) :

    rename -n 's/^ *//' *
    
  • To remove any number of trailing spaces from file names you can use rename (prename) :

    rename -n 's/ *$//' *
    

    Remove -n (dry-run) if you are satisfied with the file names.

  • To remove files or folders that are empty (recursively) :

    find . -empty
    

    Satisfied ? Let the action take place :

    find . -empty -delete
    

    Only in the current directory :

    find . -maxdepth 1 -empty -delete
    

    Also use -type f for only files and -type d for only directories if you want.

Read man rename and man find to get more idea.

2
  • 1
    I never knew about -empty. That's useful when rsync creates junk empty files. May 19, 2017 at 2:15
  • Thanks for -n dry-run! Dec 17, 2020 at 9:35
6

I often forget about rename. Here's how to do it with plain bash:

$ touch "     leading spaces" "trailing spaces     "

$ printf ">%s<\n" *spaces*
>     leading spaces<
>trailing spaces     <

$ shopt -s extglob  
$ for f in *spaces*; do 
    new=${f##*([[:blank:]])}        # remove leading whitespace
    new=${new%%*([[:blank:]])}      # remove trailing whitespace
    mv "$f" "$new"
done

$ printf ">%s<\n" *spaces*
>leading spaces<
>trailing spaces<
0

For removing spaces, several approaches here: https://stackoverflow.com/questions/2709458/bash-script-to-replace-spaces-in-file-names https://stackoverflow.com/questions/15347843/remove-whitespaces-from-filenames-in-linux

This easy one: "First enter directory with cd: cd /my/directory and then run: for f in *; do mv "$f" echo $f | tr ' ' '_'; done"

Even simpler: "rename "s/ //g" *"

For getting rid of spaces, underscores, and hyphens: "rename -i "s/[-_ ]//g" *"

For getting rid of files that have zero bytes: https://stackoverflow.com/questions/5475905/linux-delete-file-with-size-0

For handling either empty files or folders, I found this one: "Delete empty files (remove 'echo' from the command): Code: find . -empty -type f -print0 | xargs -0 echo rm Delete empty directories (remove 'echo' from the command): Code: find . -empty -type d -print0 | xargs -0 echo rmdir" http://www.linuxquestions.org/questions/programming-9/bash-delete-empty-files-910203/

Seems odd that I answered my own question in the space of a half hour, but that is the power of a search engine if you pick the right words to search on. However, there are terms and conditions asked that produce no ready answers, such as with another question I had posted here the other day. I researched it hard first, found no results, so posted it on AskUbuntu. I've checked, and still no answer. May have to go back to doing searches, if I can think of what else to ask.

1
  • You asked about removing leading / trailing spaces but those tr and rename commands 1. Don't do the same thing, one outputs the processed filenames to stdout and the other actually rename the files 2. Delete all spaces. Thanks for reporting back, but if that's what you were asking for please edit your question to make this clear. Otherwise accept heemayl's or glenn jackman's answer, which both answer the question as-is-written perfectly.
    – kos
    Sep 1, 2015 at 14:44

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.