Sorry if this has an answer elsewhere, I've no idea how to search for my problem.
I was running some simulations on a redhat linux HPC server, and my code for handling the folder structure to save the output had an unfortunate bug. My matlab code to create the folder was:
folder = [sp.saveLocation, 'run_', sp.run_number, '/'];
where sp.run_number
was an integer. I forgot to convert it to a string, but for some reason running mkdir(folder);
(in matlab) still succeeded. In fact, the simulations ran without a hitch, and the data got saved to the matching directory.
Now, when the folder structure is queried/printed I get the following situations:
- When I try to tab autocomplete:
run_ run_^A/ run_^B/ run_^C/ run_^D/ run_^E/ run_^F/ run_^G/ run_^H/ run_^I/
- When I use
ls
:run_ run_? run_? run_? run_? run_? run_? run_? run_? run_? run_?
. - When I transfer to my mac using rsync the
--progress
option shows:run_\#003/
etc. with (I assume) the number matching the integer insp.run_number
padded to three digits, so the 10th run isrun_\#010/
- When I view the folders in finder I see
run_ run_ run_ run_ run_ run_ run_ run_ run_ run_?
- Looking at this question and using the command
ls | LC_ALL=C sed -n l
I get:
run_$
run_\001$
run_\002$
run_\003$
run_\004$
run_\005$
run_\006$
run_\a$
run_\b$
run_\t$
run_$
I can't manage to cd
into the folders using any of these representations.
I have thousands of these folders, so I'll need to fix this with a script. Which of these options is the correct representation of the folder? How can I programmatically refer to these folders so I rename them with a properly formatted name using a bash script? And I guess for the sake of curiosity, how in the hell did this happen in the first place?
^A
is not literally^
followed byA
, but Ctrl-A (you can type it using Ctrl-V Ctrl-A since Ctrl-A is generally a shortcut for the shell). – muru Aug 26 '19 at 02:46run_
and I have to type something – Bamboo Aug 26 '19 at 02:49run_1
,run_2
, etc? If you have thousands of them, how are the ones beyond the first byte's worth represented (| tail
is probably good enough)? – Michael Homer Aug 26 '19 at 03:41rename
utility. e.g.rename -n 's/([[:cntrl:]])/ord("$1")/eg' run_*/
. You may have to use perl'sunpack()
function instead oford()
if sp.run_number can exceed one 8-bit value (i.e. >255). the-n
option in my example is for a dry-run, remove it to actually rename. – cas Aug 26 '19 at 03:59run_1
,run_2
etc, yes. Each simulation only goes up to 10 runs, but I have hundreds of simulations. @cas can you expand on your comment, maybe make it an answer? – Bamboo Aug 26 '19 at 04:05/
. Any other character is valid, including control characters. I don't know what matlab would have done if sp.run_number was 0 (probably either abort with an error or producerun_
, as the NUL byte would terminate the directory name string). Of course, this would be also problematic for 16-bit (or higher) values that had a NUL byte in them, and would also vary according to the endian-ness of the system running matlab. – cas Aug 26 '19 at 05:19for f in *; do mv "$f" run$i.txt; ((i++)); done
. That takes the files (in no particular order, I assume) and renames them asrun.txt run1.txt run2.txt
etc. Note thatecho
swallows the non-printable chars, soecho *
does not show you the true content of the*
expansion. – Peter - Reinstate Monica Aug 27 '19 at 15:21