2

Someone in our Global Linux 3.0.31-0.9 server has put a file with several spaces, parenthesis, question mark and a colon in its name.

And I am at my wit's end to rename or remove it!

Sample of the file name and commands I tried to remove/rename the file to "gibberish" is attached here.

My ls output:

-rw-r--r--  1 root   root       11306 May  2 03:25 who suggesting to assign this issue to the group which is responsible for the application StreamServe. (Probably it is operated by Linq????:q
-rw-------  1 root   root       15797 Jun 12 12:59 .viminfo
-rw-------  1 root   root        1200 Jun 12 13:00 .lesshst
drwxrwxrwx 32 root   root        4096 Jun 13 00:00 .snapshot
-rw-------  1 root   root       32976 Jun 13 14:05 .bash_history
  • sorry for the broken link... it is again link – RustyNails Jun 13 '15 at 12:53
  • 1
    Just quote the filename. rm 'some file (with spaces)' – jordanm Jun 13 '15 at 12:56
  • @jordanm did try that, did not work. But I have now 2 solution & am a bit wiser. Thanks though. – RustyNails Jun 15 '15 at 16:29
2

Enclosing the filename in question in quotes is probably the easiest way to remove or rename it. First create a test file:

rm "   -myfile(1):ok?"

If a file simply begins with a hyphen, use "--" to tell the command that no more options exist and that the leading hyphen is a command argument:

rm -- -myfile

In cases where your file name has unprintable characters, you can use remove it with find having determined its inode number:

find . -iname "*myfile*" -exec ls -i {} +

9225180 ./ -myfile(1):ok?

find . -inum 9225180 -exec rm -i {} +

remove ./ -myfile(1):ok??

Adding the -i (interactive) response to commands like rm allows you to confirm that you are operating on the entity you want first.

  • Thanks !! @JRFerguson find and -inum worked like a charm... results Alas ! no reputations to upvote yet ! – RustyNails Jun 13 '15 at 13:06
  • 1
    @RustyNails even without reputation you can still accept JRFerguson's answer; that will give you reputation too. – Stephen Kitt Jun 13 '15 at 13:10
3

The easy way to act on a file with a difficult-to-type name is to use completion. In this case, since the first few characters of the name are easy to type, type them, and press Tab.

$ mv whoTab found.txt

If you were unable to determine the first character, you could press Tab after ./, to cycle through all the file names in the current directory.

The shell's completion command will generate a quoted string, using ' or \ or however it's programmed.

Alternatively, you can make this quoted string yourself. Inside single quotes, all characters are interpreted normally, except a single quote itself. So if the file name doesn't contain any single quote, paste it between single quotes — but you need to have the actual file name (as printed by ls --show-control-chars), not the file name with some nonprintable characters substituted. If there is a single quote in the file name, replace it by '\'' (single quote, backslash, single quote, single quote). For example, for a file called it's, you can use 'it'\''s'.

Alternatively, you can craft a wildcard pattern that only matches this file. For example, in this case:

$ ls -d w*
who suggesting to assign this issue to the group which is responsible for the application StreamServe. (Probably it is operated by Linq????:q
wibble

In my example, there are two matching files, so the wildcard isn't good enough. Adding a character cuts it.

$ ls -d wh*
who suggesting to assign this issue to the group which is responsible for the application StreamServe. (Probably it is operated by Linq????:q
mv who* found.txt

If the first character isn't printable, you can pick another pattern matching a different part of the name. You can use a pattern like [!#-z]* or [!0-9A-Z_a-z]* or [![:alnum:]]* which exclude characters commonly used at the beginning of a file name.

If the file name begins with - (ASCII dash or hyphen), it may be interpreted as an option. Use ./-wibble to make avoid this.

  • Yes that could also have worked. – RustyNails Jun 15 '15 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.