Removing a ? from multiple files

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

Try

ls *\?
ls */*\?
find . -name "*\?"

Thanks RudiC, that will definitely locate the files, but what about the rename? The remove of the ? on the end?

Thanks!

Tirm

for FN in *\?; do mv "$FN" "${FN%\?}"; done

etc ...

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! :frowning:

Just to add the ? is in fact a \r where the cp script was built using a windows c# program.

What exactly is going wrong? Without precise error description, nobody will be able to help you.

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!

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

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.

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

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!