tirmUK
July 18, 2018, 4:03am
1
Hi all,
I have about 1.8 million files in a directory structre, that contain a ? on the end, for example:
/testdocs/1/mar/08/08/images/user/{1234-1234-1234-1234}0?
Is there a way to go through the testdocs folder, recursively, and remove the ? from all docs that have one on the end?
Thanks!
Tirm
RudiC
July 18, 2018, 4:24am
2
Try
ls *\?
ls */*\?
find . -name "*\?"
tirmUK
July 18, 2018, 4:38am
3
Thanks RudiC, that will definitely locate the files, but what about the rename? The remove of the ? on the end?
Thanks!
Tirm
RudiC
July 18, 2018, 4:49am
4
for FN in *\?; do mv "$FN" "${FN%\?}"; done
etc ...
tirmUK
July 18, 2018, 7:34am
5
I can't seem to get the right results with the for loop.
I got really close using:
find -name $'*\r*' -exec rename $'s|[\r]| |g' '{}' \;
This works for every character it seems EXCEPT for \r! \n works, any character works, but not \r. I'm not sure why.
Its driving me a bit nuts, this shouldnt be this difficult!
Just to add the ? is in fact a \r where the cp script was built using a windows c# program.
RudiC
July 18, 2018, 8:44am
6
What exactly is going wrong? Without precise error description, nobody will be able to help you.
tirmUK
July 18, 2018, 8:49am
7
Here is the find working:
find -name $'*\r*'
./FDBCFEAE-021F-47BC-896B-A225C198C1A1{52730124-EF33-405F-9A8A-C840F5CEFB95}0?
Here is the command with the replace and the error message
find -name $'*\r*' -exec rename $'s|\r| |g' '{}' \;
rename: not enough arguments
What am I doing wrong in my exec command that might cause this?
Thanks!
RudiC
July 18, 2018, 9:08am
8
Can't speak for rename
. Try to execute with the -x
(xtrace) option set.
How about
find -name $'*\r*' | while read FN; do echo mv "$FN" "${FN%?}"; done
tirmUK
July 18, 2018, 4:09pm
9
That didnt work RudiC:
FileName:
FDBCFEAE-021F-47BC-896B-A225C198C1A1{52730124-EF33-405F-9A8A-C840F5CEFB95}0?
Command and result:
find -name $'*\r*' | while read FN; do echo mv "$FN" "${FN%?}"; done
./FDBCFEAE-021F-47BC-896B-A225C198C1A1{52730124-EF33-405F-9A8A-C840F5CEFB95}0}0
No change to the file.
RudiC
July 18, 2018, 4:43pm
10
That echo
is for check / debug purposes; it displays what the commands had looked like when executed. I should have mentioned that. As you can see in the output, the <CR> (\r) character makes the target file name overwrite the mv
command.
pipe above through less
for checking.
Remove the echo
and rerun.
1 Like
Please try below command. It renames all files in directory/subdirectories.
find . -type f -name "*?" | while read file; do mv "$file" "${file%?}"; done
tirmUK
July 19, 2018, 6:06am
12
Hi Chandan,
Unfortunately, it removes the last character from the file, no matter what it is, not just the carraige return.
---------- Post updated at 11:06 AM ---------- Previous update was at 10:58 AM ----------
Hi RubiC, that worked a treat, thank you for your help, and your patience as I tried to explain the issue!