-1

I first tried everything at How to remove trailing whitespace of all files recursively? and https://superuser.com/questions/402647/how-to-remove-trailing-whitespace-from-file-extensions-and-folders-snow , which did not work.

The file name for example is "image.jpg " and i want to convert it to "image.jpg".

Please help, It also should be recursive. example

Community
  • 1
  • 1
  • 1
    Your example link seems to show a linefeed instead of a space. Could you clarify how you are using the code from the previous articles? Maybe paste your code here.. – vmachan Dec 30 '15 at 21:11
  • 2
    For a single file whose path is in the variable `filepath`: `shopt -s extglob; local newpath; newpath=${filepath%%+([[:space:]])}; [[ $newpath != $filepath ]] && mv "$filepath" "$newpath"`. You should be able to generalize from there. – 4ae1e1 Dec 30 '15 at 21:16

2 Answers2

1

give this a shot. (backup your data first)

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

replace /tmp/ with your folder.

for Apple, how about this:

for oldname in *
do
  newname=`echo $oldname | sed -e 's/ //g'`
  mv "$oldname" "$newname"
done
arcee123
  • 101
  • 9
  • 41
  • 118
  • OS X doesn't have `rename`. – 4ae1e1 Dec 30 '15 at 21:13
  • OS X is a UNIX, and actually more UNIX (so to speak) than any Linux since it's certified. `rename` is just less than nonstandard. By the way, your changed answer is still wrong. – 4ae1e1 Dec 30 '15 at 21:15
  • I agree, however, rename is widely used in UNIX. He tagged his question as UNIX, when he needed OSX. Might be the same, but as in this example, the answer isn't. – arcee123 Dec 30 '15 at 21:16
  • look at that. it's a long week. – arcee123 Dec 30 '15 at 21:18
  • By the way, I don't know where you got the impression that `rename` is widely available. I just checked. No `rename` on OpenBSD; nor on Solaris. I'm sure the list can be much longer. – 4ae1e1 Dec 30 '15 at 21:24
  • I tried your answer, but it didn't work it just renamed my directory names and when I put it in the same directory as the pictures it didn't do anything. – Séan M. Warren Dec 31 '15 at 09:17
  • ok. I'm going to go look around. In the meantime, howabout this? http://mrrsoftware.com/namechanger/ – arcee123 Dec 31 '15 at 17:06
0

find . -depth ... is the best answer, but unfortunately it's unavailable to you (unless you install homebrew)

One difficulty of the for solution is that it does not descend into the directory hierarchy depth-first. So you're in danger of renaming a directory first, and then failing to rename any of the files under it.

To ensure that you're renaming files before any parent directory is to find the files and then reverse sort:

shopt -s globstar nullglob extglob
printf "%s\n" **/*[[:space:]] | sort -r | while IFS= read -r filename; do
    newname=${filename/%+([[:space:]])}
    mv "$filename" "$newname"
done
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Why do you think `find` is unavailable on OS X? `find` is part of POSIX (the `-depth` primary is also POSIX — you don't need findutils for `-depth`), so `/usr/bin/find` should certainly be available on vanilla (without CLT, Xcode, or Homebrew). And your answer would break if there are linefeeds in filenames, which is precisely the case here. – 4ae1e1 Dec 31 '15 at 00:19
  • Thanks Glenn Jackman, your code worked almost perfectly for the job it renamed all of the filenames with a linefeed but I still received an error "someCode.sh: line 1: shopt: globstar: invalid shell option name"; but i read that is because I need to upgrade bash. – Séan M. Warren Dec 31 '15 at 09:21