开发者

How to replace backslashes from windows formatted files into *nix format and reestablish structure

开发者 https://www.devze.com 2023-02-03 09:22 出处:网络
I have a fair few files that have been extracted from a zip created in a strange way. the files have come out of the tar.gz in windows file structure format

I have a fair few files that have been extracted from a zip created in a strange way.

the files have come out of the tar.gz in windows file structure format

Example:

jpg_250\MI\00\00\00\19\MI0000001900.jpg

versus

jpg_250/MI/00/00/00/19/MI0000001900.jpg

The former is seen as a single file by linux.

I've been playing around with awk and sed to delimit the filename by backslash, and create the dir开发者_运维知识库ectories in question in the correct structure, and finally rename the file to the MI**.jpg and move it into the correct newly created end directory.

Is awk and sed the way to go here? I have awk exploding the filename into the 'directories' I need but I'm having trouble getting the directories actually created. I assume I would need sed at the end to rename the file into the MI**.jpg format.

Many thanks for any help.


Something like this?

$ ls 
a\b\c
a\b\d

$ for i in *; do
    F=$(echo $i | sed 's,\\,/,g')
    D=$(dirname $F)
    echo mkdir -p ${D}
    echo cp "${i}" "${F}"
done

mkdir -p a/b
cp a\b\c a/b/c
mkdir -p a/b
cp a\b\d a/b/d


You could do this with Perl like this.

# assume that the script was called like this:
# myscript file1 file2 etc...
# then all the files are in @ARGV

foreach $orig (@ARGV) {
    $orig_fixed = $orig;

    # convert the \ to /
    $orig_fixed =~ s!\\!/!g;

    #split the filename into directory and filename 
    ($dir, $filename) = ($orig_fixed =~ m!^(.*)/([^/]*)$!);

    # create the directory if it doesn't exist
    if (!-e $dir) {
        `mkdir -p $dir`; # -p means create the full path
    }

    # now move the file
    `mv '$orig' $dir/$filename`;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消