I have few directories whose names looks like the following:
I want to remove the newline from the end recursively.
I checked Recursively rename directories
I also checked Remove newlines in file names
The solution it suggests is:
find -name $'*\n*' -exec rename $'s|\n| |g' '{}' \;
But in my case find -name $'*\n*'
returns nothing. If I remove the $
it can find the directories
% find . -name '*\n*'
./second?
% find . -name '*\r*'
./third?
./first?
However, when I run find . -name '*\n*' -exec rename $'s|\n| |g' '{}' \;
it does not rename the directory. I also tried find . -name $'*\n*' -exec rename $'\n' ' ' {} \;
from Recursively remove newline in file names. It is also not renaming the directories.
What can I do?
find -name $'*\n*'
works for me and finds filenames with hard newlines.-name '*\n*'
is the same as-name '*n*'
and finds names with ann
... What shell are you actually running? You tagged this with both [tag:bash] and [tag:zsh], and they should both support$'...'
. Are you sure it's the actual newline character you have there, and not something different? What do you get if you runls --quoting-style=shell-escape
(with a recent-ish GNU ls) orprintf "%q\n" *
(in Bash or Zsh) in the directory with those dirs? – ilkkachu Apr 11 '21 at 21:45$
from both strings, i.e.find . -depth -name '*\n*' -exec rename 's|\n|_|g' {} \;
– Chris Davies Apr 11 '21 at 21:46rename
, you need to know if you have the Perl one, or the util-linux one... What doesrename --version
say? (see also https://unix.stackexchange.com/a/510583/170373 and links therein) – ilkkachu Apr 11 '21 at 21:47printf "%q\n" *
showsfirst$'\342\200\251' second$'\342\200\251' third$'\342\200\251'
– Ahmad Ismail Apr 11 '21 at 22:56rename --version
says/usr/bin/rename using File::Rename version 1.10
– Ahmad Ismail Apr 11 '21 at 22:58$_
is changed by the rename script, so you can just usefind . -type d -exec rename 's/[\n\r]/ /g' {} +
. directory names with newlines and/or carriage-returns in them will be renamed, directories without will be ignored. – cas Apr 12 '21 at 00:38find . -type d -exec rename 's/[\n\r]|\342\200\251/ /g' {} +
– cas Apr 12 '21 at 00:42