4

I have this situation in a certain directory:

$ ls -al
...
drwxr-xr-x   2 me users         8192 Juni 28 15:27 tmp
-rw-r--r--   1 me users          439 Jan.  3  2013
...

How can I access the second entry, which appears to have no filename?

Daniel Andersson
  • 23,895
  • 5
  • 57
  • 61
alk
  • 167
  • 10
  • what about `mv "" "newname"`? or maybe the file name is made of spaces ? try `ls | wc` or something similar – mveroone Aug 21 '13 at 09:13
  • 1
    @Kwaio You cannot have a file with an empty name, or at least `mv ""` would not recognize the empty argument. alk, can you do an inode listing with `ls -i`? Do you want to move the file or rename it? – slhck Aug 21 '13 at 09:17
  • How did you get `Juni` or `Jan.`. Doesn't look like right. – user568109 Aug 21 '13 at 10:11
  • @user568109: `Juni` and `Jan.` are german. – alk Aug 21 '13 at 10:40
  • How was that file created? https://unix.stackexchange.com/questions/66965/files-with-empty-names – Ciro Santilli OurBigBook.com Jun 05 '17 at 09:39
  • @CiroSantilli709大抓捕六四事件法轮功: Applying the solution mentioned here: https://superuser.com/questions/634632/accessing-an-unnamed-file?noredirect=1#comment792292_634668 it turned out that the file's name contain unprintable characters. So it hadn't had an "empty" string (`""`) as name. – alk Jun 05 '17 at 17:01

2 Answers2

7

A sample run to create a file named "non breaking space" and renaming it:

$ ls -1 # Each file on a separate row
testfile
$ touch   # I'm actually writing the character u00a0 here
$ ls -1
testfile
 
$ ls -i # Print inodes
2031842 testfile
2023653  
$ find . -maxdepth 1 -inum 2023653 -exec mv {} hithere \;
$ ls -i1
2023653 hithere
2031842 testfile

The exec in its form only makes sense if there is just a single match, but the inode number should be unique on that partition unless there exists hard links to the same file. You can test this by running find without arguments first to see if you get multiple matches. If that is the case, you can e.g. refine the find pattern to only match the file you want.

Since the renaming does not depend on the exact characters of the filename, there should be no problem with entering strange code points.

Daniel Andersson
  • 23,895
  • 5
  • 57
  • 61
  • Thanks a lot! `find . -inum -exec mv {} gatya \;` did it. Please see my comment on *jaychris* answer for some background. – alk Aug 21 '13 at 10:39
2

I would try "ls -1a > /tmp/list" and hexdump to see if something comes up. It is possible the file name is sequence of spaces and tabs. If the hexdump shows the filename as spaces (say 1 space), then do a "mv \ somename"

ls -1

ls -1 > /tmp/list
cat /tmp/list

hexdump /tmp/list
          00 01 02 03 04 05 06 07 - 08 09 0A 0B 0C 0D 0E 0F  0123456789ABCDEF

00000000  20 0A     {------                                          .
mv \  new        {------
ls -1
new
jaychris
  • 368
  • 1
  • 6
  • It turned out the file name was `^?`. However to grab the file I had to rename it, but wasn't able to enter `^?` via the keyboard so I succesfully used the "inode"-approach propose by *Daniel Andersson*. Thanks anyway. – alk Aug 21 '13 at 10:37
  • 1
    If its some special character like `^?` you can also redirect output of `ls` to a file, edit it so it contains only the file name with weird chars and use it in your command: `mv $(cat file) new_name` (bash syntax, you can use also backticks, but here I can't put backticks inside backticks :) – Marki555 May 15 '15 at 20:37
  • @Marki555 why not post this as another answer. It's a smart and simple approach. :-) – alk Jun 09 '17 at 17:26