I have a directory where some filenames have been wrongly named something .pdf rather than something.pdf. Is there a quick one liner I can use to delete the whitespace in the filenames.I have tried find -name * .pdf -type f | rename 's/  //g'but this did not work.
Delete whitespace in filenames in directory
linux;files;rename
In zsh, this is easily done with the zmv function. Put autoload -U zmv in your ~/.zshrc, or run this once in your shell, then:zmv '* .pdf' '${f// /}'This removes all spaces from the file name. If you only want to remove the one before the .pdf extension:zmv '* .pdf' '${f# .pdf}.pdf'or using parentheses to delimit groups that can be used as backreferences:zmv '(*) .pdf' '$1.pdf'If you want to act in subdirectories as well:zmv '**/* .pdf' '${f# .pdf}.pdf'or (note that you have to use (**/), not (**)/):zmv '(**/)(*) .pdf' '$1$2.pdf'Under Linux, with or without zsh, if there is only this one spurious space in the file names:rename \ .pdf .pdf *\ .pdfor, if there's only this one space in the original names:rename ' ' '' *\ .pdfIf you want to act in subdirectories as well, in ksh93 o+ or bash 4, you can use ** to match files in subdirectories. In bash, you need to run shopt -s globstar first (put this line in your ~/.bashrc) and set -o globstar in ksh93.rename \ .pdf .pdf **/*\ .pdfBeware that bash follows symlinks when recursing directories.Without the benefit of **, you can use find to recurse.find . -name '* .pdf' -exec rename ' .pdf' .pdf {} +Under Debian and derivatives (including Ubuntu) and most non-Linux systems that ship with perl, the rename command is a different one, which takes a Perl expression as an argument instead of a string to replace and a replacement string. Either use rename.ul in the commands above (under Debian and derivatives), or with the Perl rename:rename 's/ \.pdf$/.pdf/' -- *\ .pdf    # strip the space before the extensionrename 's/ //g' -- *\ .pdf             # strip all spaces