3

I have a file blahblah\r\n.txt that I want to remove but rm blahblah\r\n.txt does not work as I receive No such file or directory message.

How to remove??

2
  • Did you try rm blabla*.txt? You also might benefit from tab completion using rm blahbla+tab. Third option, if the \r\n is listed literally you can use single quotes: `rm 'blabla\r\n'.txt'
    – Lambert
    Oct 28, 2015 at 22:15
  • Yes, same result.
    – zoltar
    Oct 28, 2015 at 22:19

5 Answers 5

3

You can enclose a string in $'' to enable the interpretation of escape sequences in it (in this case to enable the interpretation of \r):

rm $'file\rwith_carriage_return'
% touch $'file\rwith_carriage_return'
% ls
file?with_carriage_return
% rm $'file\rwith_carriage_return'
% ls
% 
2
  • I believe this is the best solution, I will accept the answer when I run into the problem again because at the moment I cannot reproduce.
    – zoltar
    Oct 31, 2015 at 21:32
  • @zee You can create a test case running touch $'file\rwith_carriage_return', and check that it actually contains it running ls $'file\rwith_carriage_return' | hexdump -c
    – kos
    Nov 1, 2015 at 21:27
1

Of the several various methods available, one is to find the inode number and then nuke that.

$ mkdir -p ~/tmp/asdf
$ cd !$
cd ~/tmp/asdf
$ touch `head -c 32 /dev/random` # newlines are boring
$ find . -type f -ls
5636303    0 -rw-r--r--   ...
$ find . -maxdepth 1 -inum 5636303 -exec rm '{}' \;
1

It should be possible to delete the file if you escape the filename: rm 'bla\n.txt'

But if that doesn't work try deleting by inode number:

ls -i bla*
1234 bla\n.txt
find . -inum 1234
#make sure the right file and only the right file is returned then
find . -inum 1234 -delete
1

Usually when I run into such situations, I do the following..

EX: Suppose I have the file 'xxx\nyyy'

I know that it starts with 'xxx'.....

I use the command:

 rm -i xxx*

And delete the RIGHT file when prompted if you want to delete a file or not.

0

I removed with rm *.* however I'd still like to know why rm blahblah\r\n.txt and rm blahblah*.txt do not work..

2
  • 1
    It doesn't work because the shell interprets the backslash to escape the next character. And it seems a literal backslash is needed here.
    – Bram
    Oct 28, 2015 at 22:30
  • Try to echo your command to see how the shell interprets it. echo rm blahblah\r\n.txt prints "rm blahblahrn.txt", which can't work as your file is not named blahblahrn.txt
    – Geoffroy
    Oct 28, 2015 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .