3

I would like to delete the spaces at the end of my folder names.

I have a folder called "Project1 " in which I have another folder called "Exercise1 ".

I want to use a script to delete the spaces at the end and only at the end.
I don't know if I have expressed myself correctly.

Thank you in advance, have a nice evening.

I typed the command /bin/ls -l | od -c

root@debian:~$ /bin/ls -l | od -c
0000000   t   o   t   a   l       3   2  \n   d   r   w   x   r   -   x
0000020   r   -   x       2       k   a   b   i   a       k   a   b   i
0000040   a       4   0   9   6       j   u   i   l   .       2   0    
0000060   1   0   :   2   6       B   u   r   e   a   u  \n   d   r   w
0000100   x   r   -   x   r   -   x       7       k   a   b   i   a    
0000120   k   a   b   i   a       4   0   9   6       j   u   i   l   .
0000140       2   2       1   7   :   4   6       D   o   c   u   m   e
0000160   n   t   s  \n   d   r   w   x   r   -   x   r   -   x       2
0000200       k   a   b   i   a       k   a   b   i   a       4   0   9
0000220   6       j   u   i   l   .       2   2       1   9   :   2   6
0000240       I   m   a   g   e   s  \n   d   r   w   x   r   -   x   r
0000260   -   x       2       k   a   b   i   a       k   a   b   i   a
0000300       4   0   9   6       j   u   i   l   .       2   0       1
0000320   0   :   2   6       M   o   d 303 250   l   e   s  \n   d   r
0000340   w   x   r   -   x   r   -   x       2       k   a   b   i   a
0000360       k   a   b   i   a       4   0   9   6       j   u   i   l
0000400   .       2   0       1   0   :   2   6       M   u   s   i   q
0000420   u   e  \n   d   r   w   x   r   -   x   r   -   x       2    
0000440   k   a   b   i   a       k   a   b   i   a       4   0   9   6
0000460       j   u   i   l   .       2   0       1   0   :   2   6    
0000500   P   u   b   l   i   c  \n   d   r   w   x   r   -   x   r   -
0000520   x       2       k   a   b   i   a       k   a   b   i   a    
0000540   4   0   9   6       j   u   i   l   .       2   0       1   0
0000560   :   2   6       T 303 251   l 303 251   c   h   a   r   g   e
0000600   m   e   n   t   s  \n   d   r   w   x   r   -   x   r   -   x
0000620       2       k   a   b   i   a       k   a   b   i   a       4
0000640   0   9   6       j   u   i   l   .       2   0       1   0   :
0000660   2   6       V   i   d 303 251   o   s  \n
0000673

5 Answers 5

3

With zsh:

autoload zmv # best in ~/.zshrc
zmv -n '**/*[[:space:]]' '${f%%[[:space:]]#}'

(remove -n for dry-run when happy)

Would remove trailing whitespace characters from the end of every non hidden file or directory. Add (#q/) to the pattern argument if you want to restrict to directories only. Or (#qD) to also process hidden file.

POSIXly, you can do something approaching with:

LC_ALL=C find . -depth -name '*[[:space:]]' -exec sh -x -c '
  for f do
    : mv -i "$f" "${f%"${f##*[![:space:]]}"}"
  done' sh {} +

(remove -x and : when happy).

That one is limited to ASCII whitespace characters.

1
  • Hello, I have taken into account your answer and thank you. However, it still doesn't work. When I run my script, my folders with spaces at the end are not renamed. Also, I am trying to run this code in bash. I am testing things on my side but I am still at the same point. But thank you very much for your answers and your reactivity. Commented Jul 22, 2021 at 12:55
3

You can do this using a Bash script.

Just create a script and copy this code in it, put it in the top directory in which the "bad named directories" are located, then execute:

#!/bin/bash
    
# find all directory names that end with space:
while find "$(pwd)" -type d |grep  -q ' $'
do
    # select one and store its name in DIR variable
    DIR=$(find "$(pwd)" -type d | grep ' $'|head -1)
    
    # remove the last character (which is space) from that 
    # name and save it to NEW_DIR variable
    NEW_DIR=${DIR%?}
    
    # rename the directory
    mv "$DIR" "$NEW_DIR"
done

As you can see in this screenshot, there were directories which had similar conditions in "test2" directory, and the script named "space.sh" is also located in "test2". After executing the script, directories have been renamed as we desired:

enter image description here

1

The pathlib module in python3 has all the wherewithal to walk the hierarchy , detect the said files, and rename them.

python3 -c 'import pathlib
for p in iter(pathlib.Path(".").rglob("*[ \t]")):
  p.rename("/".join([str(p.parent),p.name.rstrip()]))'

Based on the kinds of filenames present in your file hierarchy, we use the following piece of python to recursively traverse the tree depth first is the default action . It looks for any non-ASCII names and renames by replacing he nonASCII char. with an "X". No checks are made before renaming whether the new name already exists.

python3 - <<\eof
import pathlib, re
for p in iter(pathlib.Path('.').rglob('*[! -~]*')):
  foo = p.name
  for m in re.finditer(r"[^ -~]",p.name):
    foo = foo[:m.start()] + "X" + foo[m.end():]
  p.rename("/".join([str(p.parent),foo]))
eof
5
  • Hello, I have taken into account your two answers and thank you. However, it still doesn't work. When I run my script, my folders with spaces at the end are not renamed. Also, I am trying to run this code in bash. I am testing things on my side but I am still at the same point. But thank you very much for your answers and your reactivity. Commented Jul 22, 2021 at 12:55
  • 1
    Well that means there are not real spaces at the end of file names then. Can you share the output of the command /bin/ls -l | od -c Commented Jul 22, 2021 at 14:57
  • The output is very big, I think I can't share it Commented Jul 22, 2021 at 17:30
  • Can you share a clipped output? Obtained via piping the od -c output to head? Because I want to know what characters comprise your filenames exactly. Commented Jul 22, 2021 at 18:11
  • Yes I can, I will put a little part. Commented Jul 23, 2021 at 12:21
1

Using Perl's rename.

I didn't write this but I use it a lot. Transferring video files from a Windows box to my Linux box puts extra white spaces at the end of directory names and this command removes them whether it's a single white space or multiple white spaces at the end of folder and file names.

Remove empty spaces at the end of directory / file names in the current directory

does a dry run

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

actual run

rename 's/ *$//' ./*
-1

In python I used os.walk() to process whole file structures cleaning up file and folder names.

I needed something for migrating Apple servers or replacing Netatalk as a file service by Samba on linux.

As there was nothing around solving all naming issues, I ended up writing a python script to do my bidding. Although I tested this only on linux, it should work on any posix system providing python 3.

You can find the code on my git: macSanitize

2
  • It is not enough to point to a tool, you also need to show how to use the tool to solve the specific issue in the question. Commented May 31, 2024 at 9:32
  • You should not point to a tool as an answer. Commented Jun 6, 2024 at 18:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.