Given a path to some directory (i.e. path/to/directory
), I want to obtain the output in the following format:
100|path/to/directory/a/file1.txt
200|path/to/directory/b/file2.txt
321|path/to/directory/c/d/file3.txt
...
Here 100, 200, 321 etc. are the sizes (in bytes) of the corresponding files (the symbol |
is used as a separator.)
I tried to use the following command:
find path/to/directory -maxdepth 3 -type f -printf "%s|%p\n" > path/to/output.txt
But then I had a lot of error messages of the following form:
find: `path/to/directory/my directory 1': No such file or directory
I noticed that this happened when the name of some directory contained at least one space. So my question is: what command can I use so that it will produce the desired output, no matter what chars occur in file names and directory names?
The question is not a duplicate of this question because there is an additional issue of problematic names. This answer mentions the issue of subdirectory or file names containing white space, then says that it can be solved by -print0 | xargs -0
, then says that it's easier to avoid using the xargs
construction altogether. I have found one relevant answer here, but was unable to modify it so that it would fit my needs.
EDIT
I have found the issue that causes the problem. The thing is, I run these commands from Cygwin or MSYS2, which leads to this problem because the long filenames contain characters encoded by more than one byte. The problem is also described here and here. Therefore, Cygwin and MSYS2 are unable to deal with some files whose paths do not exceed the maximum length (255 characters) allowed by the OS (Windows). For example, I have created a file whose full path (in Windows) is the following:
E:/test1/αβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγ.txt
Then I run the following command:
find E:/test1 -maxdepth 3 -type f -printf "%s|%p\n" > E:/test2/output.txt
But I got the following error:
find: ‘E:/test1/αβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγαβγα\316’: No such file or directory
As far as I understand, it cannot be fixed.