How can I delete a file named “>”?

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--.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 {} +

Method 2

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 {} +

Method 3

What I ended up doing initially also works:

sudo sh -c "rm >"

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

Method 4

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

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2aa8390909ba28e8d81838e8a8d9196">[email protected]</a>]~% touch ">"
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c78fa6b5b5be87aba8a4a6abafa8b4b3">[email protected]</a>]~% cat > ">"
line 1
line 2
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c38ba2b1b1ba83afaca0a2afabacb0b7">[email protected]</a>]~% cat ">"
line 1
line 2
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="662e0714141f260a0905070a0e091512">[email protected]</a>]~% ls -l ">"
-rw-r--r-- 1 Harry Harry 14 Jun  5 12:04 >
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fdb59c8f8f84bd91929e9c9195928e89">[email protected]</a>]~% rm ">"
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="450d2437373c05292a2624292d2a3631">[email protected]</a>]~% ls -l ">"
ls: cannot access >: No such file or directory
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ade5ccdfdfd4edc1c2ceccc1c5c2ded9">[email protected]</a>]~%

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x