10

I want to rename file with weird name to something reasonable, however I'm not able to :/

$ mv *_000c.jpg 000c.jpg
mv: cannot move '?j?Z?R?C1_000c.jpg' to '000c.jpg': No such file or directory

I've tried using inode number as was recommend in few places on the internet:

$ ls -il *000c.jpg
ls: '?j?Z?R?C1_000c.jpg': No such file or directory
213915 -rw-r--r-- 1 wolf wolf 794655 Jul 21  2012 '?j?Z?R?C1_000c.jpg'
$ find . -inum 213915 -print0 | xargs -0 -I '{}' mv '{}' 000c.jpg
mv: cannot move './?j?Z?R?C1_000c.jpg' to '000c.jpg': No such file or directory

What should I do?

graywolf
  • 931
  • Try to escape the * char. So like that: mv \*_000c.jpg 000c.jpg – ddnomad Apr 12 '17 at 13:47
  • @ddnomad My first thought too, but the expansion of * seems to be wanted by OP. ?j?Z?R?C1_000c.jpg is the actual name of the file to move. – lgeorget Apr 12 '17 at 13:49
  • 1
    What is the output of a simple ls in that directory? Is the file shown as a regular file? I am asking to make sure that the No such file or directory is a globbing error and not indicative of a damaged file system. – terdon Apr 12 '17 at 13:50
  • 1
    I wonder if the filesystem needs to be run through fsck? I may be corrupted. How did the file acquire that name? – Kusalananda Apr 12 '17 at 13:50
  • Would mv -v \?j\?Z\?R\?C1000c.jpg 000c.jpg work? – Mio Rin Apr 12 '17 at 13:53
  • @Kusalananda it was extrated from .zip file already like this. When I open the zip file in midnight commander, the filenames are all like this – graywolf Apr 12 '17 at 13:53
  • @Mioriin mv: cannot stat '?j?Z?R?C1000c.jpg': No such file or directory – graywolf Apr 12 '17 at 13:53
  • 1
    @Mioriin The question marks in the filename are (or may well be) placeholders for characters that can't be displayed. (one reason to never parse the output of ls) – Kusalananda Apr 12 '17 at 13:54
  • 1
    This might be a terminal font issue. It's showing ? for letters that it can't display. Try switching to a fully unicode font. – Mio Rin Apr 12 '17 at 13:55
  • 1
    @Mioriin I don't think that's it, my terminal shows for example japanese just fine, imho it's more likely that the ? are not valid unicode characters? – graywolf Apr 12 '17 at 13:57
  • 5
    @Mioriin Whether or not the filename can be displayed is a bit of a red herring in this case I think. What's odd is that the shell is able to expand *_000c.jpg but that mv then can't process the file. This is, IMHO, indicative of a corrupt filesystem. ... or possibly of a filesystem that's mounted on a network share that's gone away (with the directory entries still cached), or something. – Kusalananda Apr 12 '17 at 13:59
  • There's an answer out there that suggests using dd to 'edit' the zip file to rename the files in the zip. If you can do that, then you can just delete the directory that contains this file. http://stackoverflow.com/questions/32829839/rename-files-in-zip-archive – JohnQ Apr 12 '17 at 14:02
  • Yes, there's nothing special about that file name. You can test this yourself by running touch '?j?Z?R?C1_000c.jpg' in an empty directory. You will then be able to manipulate the file normally as you have tried in your question. The fact that you can't suggests there's something wrong and that doesn't depend on the file name. Can you run for f in *; do file "$f"; done? What does that return? – terdon Apr 12 '17 at 14:02
  • 1
    @Kusalananda man you rock! that was it. Since I have btrfs on both ends, I kinda forgot that there is samba in between. I tried ssh-ing into the server and doing it there and it worked fine. So samba apparently had problem with the name while btrfs didn't. Could you write it as an answer so I can accept it (if you want the reputation that is) :) – graywolf Apr 12 '17 at 14:05

3 Answers3

16

Summary of relevant comments:

Unix file systems allows any character in a file name apart from \0 (nul) and / (forward slash). The fact that ls shows question marks is only because it can't display some of the characters of the filename in the current locale (which is one of the reasons why you should avoid parsing the output of ls).

However, with a Samba share, you apparently have more strict requirements on filenames than on a standard Unix filesystem.

Since the file had a name that was "illegal" on your intermediate Samba share, the file was inaccessible by its correct name on the machine mounting the share.

The mv didn't work since the name returned from the Samba share for the expansion of the globbing pattern was not the actual name of the file on the hosting filesystem, only Samba's own mangled version of the name.

Your solution was to log into the server that actually hosted the file (on a filesystem with less restrictive naming rules than Samba) and change the name of the file there. This was the correct course of action.

See also the Unix&Linux chat about this question.

Kusalananda
  • 333,661
-2

Before I learned about quoting in unix shells, I would use emacs' dired mode to rename or delete hard-to-match files. You simply open the directory in emacs:

emacs -q .

... then you can use the arrow keys to navigate to the file you want to modify and rename the file with R. ? will give you the list of available commands (you can delete files that are hard to match too).

Then you just C-x C-c your way out of emacs and you're done.

Jim K.
  • 101
-3

Often with unusual filenames you can add "./" before the filename and Linux will properly recognize it. For example: $ ls ./?j?Z?R?C1_000c.jpg