22

I was running a Python script that malfunctioned and used sudo to create a file named >.

How can I get rid of this file?

Of course, when I try sudo rm >, I get the error bash: syntax error near unexpected token 'newline', because it thinks I'm trying to redirect the output of rm.

Its permissions are -rw-r--r--.

4
  • 10
    The answers are right, of course, but in general you might consider this: how was the file created in the first place? (Of course, sudo touch > at a shell would give the same error.) You said it was with a Python script, presumably created by passing ">" to some os function. So sudo python <<< 'import os; os.remove(">")' should work just fine.
    – wchargin
    Commented May 30, 2015 at 21:27
  • 4
    This reminded me of the one bug in windows where you literally couldn't delete a file with a certain name (but there was no problem making it.) Commented May 30, 2015 at 21:35
  • PyRulez ooh, which? Commented Jun 1, 2015 at 1:00
  • 8
    Ironically, the answer is in the title you wrote yourself.
    – Raphael
    Commented Jun 1, 2015 at 10:20

5 Answers 5

54

Any of these should work:

sudo rm \>
sudo rm '>'
sudo rm ">"
sudo find . -name '>' -delete
sudo find . -name '>' -exec rm {} +

Note that the last two commands, those using find, will find all files or directories named > in the current folder and all its subfolders. To avoid that, use GNU find:

sudo find . -maxdepth 1 -name '>' -delete
sudo find . -maxdepth 1 -name '>' -exec rm {} +
10
  • 2
    It's probably worth pointing out that find is not like rm because it is recursive and will delete every file named rm under your current directory tree no matter how deep.
    – jw013
    Commented May 30, 2015 at 15:31
  • 6
    @rahul The first three (the rm variants) are all the same. It's just three different ways of protecting that file name from the shell. There isn't really a reason to prefer one over another.
    – derobert
    Commented May 30, 2015 at 17:24
  • 7
    Why do you suggest find(1)? The only problem here is that a shell metacharacter needs to be quoted. There are a million-and-one different programs that can remove a file, but using anything other than rm(1) is just obfuscating the answer.
    – camh
    Commented May 31, 2015 at 6:15
  • 2
    You don't need GNU find to avoid recursing. sudo find . ! -name . -prune -name '>' -exec rm {} +.
    – hvd
    Commented May 31, 2015 at 20:25
  • 2
    I agree with @camh, find adds nothing for answering the question asked. Commented Jun 1, 2015 at 5:38
18

You can also use Python to remove it:

python -c 'import os;os.remove(">")'

With POSIX find:

find . ! -name . -prune -type f -name '>' -exec rm -f {} +
2
  • 3
    Why bother? The shell provides plenty of ways to avoid interpreting > as output redirection.
    – alexis
    Commented May 31, 2015 at 14:36
  • 3
    @alexis imo it's best to know as many ways of accomplishing these simple tasks as possible. if op has another problem like this that's more difficult to solve purely with the shell, they'll have another tool in their toolbox to give it a try with Commented May 31, 2015 at 22:07
1

What I ended up doing initially also works:

sudo sh -c "rm \>"

This is, of course, a variant on the simpler sudo rm \>.

0

I tried this as a comment but it came out all on one line

[Harry@localhost]~% touch ">"
[Harry@localhost]~% cat > ">"
line 1
line 2
[Harry@localhost]~% cat ">"
line 1
line 2
[Harry@localhost]~% ls -l ">"
-rw-r--r-- 1 Harry Harry 14 Jun  5 12:04 >
[Harry@localhost]~% rm ">"
[Harry@localhost]~% ls -l ">"
ls: cannot access >: No such file or directory
[Harry@localhost]~% 
2
  • This is already in terdon's answer, though... Commented Jun 5, 2015 at 11:18
  • Yes, it is partly given in tendon's answer, I agree, I just thought that this sequence is simple and made it explicit. As you'll see I tried to include it as a comment rather than an answer, but I don't know how to avoid it all coming out on one line. It also shows you don't need Python to create the file, another point also suggested in other comments. Commented Jun 5, 2015 at 14:12
0

Quote the character so that it isn't interpreted by the shell as a redirection:

sudo rm '>'

However, if you have other files with weird characters, the safest method is to open a GUI file explorer such as nautilus and delete it there.

You must log in to answer this question.

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