1

A program messed up my directory putting a dot "." on the end of some file and directory names. What is the easiest way to remove them?

I have thought of removing the last character but not all the files/dirs have a dot on the end. Also removing all the dots is a problem, this will make the extension useless.

What I need is a rename to change name.of.the.file.ext. to name.of.the.file.ext and name.of.the.dir. to name.of.the.dir

Thanks!

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • just write a script that scans the file names and checks for trailing dots, then discards them if necessary. you chould use the ´mv´ command to rename the files/directories – xQuare May 09 '13 at 11:25

2 Answers2

2

Go over the files with the dot at the end, rename each if possible (i.e. the target file does not exist).

for file in *. ; do
    [[ -e ${file%.} ]] || mv "$file" "${file%.}"
done
echo Not renamed: *.
choroba
  • 231,213
  • 25
  • 204
  • 289
  • If the user has to descend into subdirectories: `shopt -s globstar; for file in **/*.; ...` . Also, why the `-e` test? Will `*.` return non-existing files? – glenn jackman May 09 '13 at 13:02
  • @glennjackman: No, it tests the target in order not to overwrite it. – choroba May 09 '13 at 13:09
1

There might be a rename utility on your machine that will let you do

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

Check man rename

glenn jackman
  • 238,783
  • 38
  • 220
  • 352