0

Currently I have a recursive directory of files that looks like the following

foo_1/bar_1.txt
foo_1/foo_2/bar_2.txt
foo_1/foo_2/bar_2a.txt
foo_1/foo_2/foo_3/bar_3.txt
...

I need to use a bash script to convert them into single file names safe across all operations like the follows:

foo_1_bar_1.txt
foo_1_foo_2_bar_2.txt
foo_1_foo_2_bar_2a.txt
foo_1_foo_2_foo_3_bar_3.txt
...

However using the underscore as the replacement of "/" would undermine the backward process and one cannot convert them back to the original "Path" names.

I am currently considering using "=" as the replacement character and thing will look like

foo_1=bar_1.txt
foo_1=foo_2=bar_2.txt
foo_1=foo_2=bar_2a.txt
foo_1=foo_2=foo_3=bar_3.txt

I went through this question (Good style/practices for separators in file (or directory) names) where the answer suggests using multi-character options but I since my file names are already pretty long and need to be kept below a certain threshold after the "replacement process", if a single character option, that is human readable, is available things will work in my best interest.

I have tested touching a file with equal sign and it is working fine on at least MacOS, however I am afraid there are some other systems that will result in problems. Are there any caveats in doing this?

EDIT: The purpose of my process, is to convert a tree-like directory structure into a single directory, upload the files to a server (via ssh) that only accepts single directory structure, and then have another computer download (via HTTP) the files and convert them back to the directory structure.

cr001
  • 105
  • 1
    The obvious caveat is that your original names can't contain equal signs, or whatever character you decide to use. It's not clear what the issue is that you are solving by replacing the original path separators though. Using ordinary pathnames should be portable. – Kusalananda Jan 13 '22 at 09:02
  • I have edited the question to include the original issue – cr001 Jan 13 '22 at 09:05
  • Using the original "/" separator would not work. touch a/b.txt on my system would give no such file or directory error – cr001 Jan 13 '22 at 09:12
  • Well, make sure that the directory called a exists first then. mkdir -p a && touch a/b.txt. – Kusalananda Jan 13 '22 at 10:01
  • 1
    why not just use tar or something to put all the files into one archive file (optionally compressed with gzip or xz or whatever), upload it, and the other system can download and extract it? That's one of the things that archive formats are for. – cas Jan 13 '22 at 10:12
  • @cas +1 ... with the additional benefit that metadata (time stamps etc) are preserved throughout the transfer. – Paul_Pedant Jan 13 '22 at 11:12

0 Answers0