I have some files or directory with space in the end of the name. It's possible with the shell to delete it ?
Thanks
On
Here is a native POSIX shell solution with no external calls.
#!/bin/sh
for file in *[[:space:]]; do # loop over files/dirs ending in space(s)
[ -e "$file" ] || continue # skip empty results (no-op)
new_file="${file%[[:space:]]}" # save w/out first trailing space char
while [ "$file" != "$new_file" ]; do # while the last truncation does nothing
file="${file%[[:space:]]}" # truncate one more trailing space char
done
# rename, state the action (-v), prompt before overwriting files (-i)
mv -vi -- "$file" "$new_file"
done
See:
${varname:start:length}taking a slice of a given length starting at positionstart).forloop, used to iterate over glob results.The printf
%qspecifier is a bash extension which formats a string in such a way as toevalback to that original string's contents -- thus, it may print a name ending in a space asname\or'name ', but will in some way or another make sure that the whitespace is visible to the reader.