12
Mitiko
2y

This is a story of how I did a hard thing in bash:

I need to extract all files with extension .nco from a disk. I don't want to use the GUI (which only works on windows). And I don't want to install any new programs. NCO files are basically like zip files.

Problem 1: The file headers (or something) is broken and 7zip (7z) can only extract it if has .zip extension

Problem 2: find command gives me relative to the disk path and starts with . (a dot)

Solution: Use sed to delete dot. Use sed to convert to full path. Save to file. Load lines from file and for each one, cp to ~/Desktop/file.zip then && 7z e ~/Desktop/file.zip -oOutputDir (Extract file to OutputDir).

Problem 3: Most filenames contain a whitespace. cp doesn't work when given the path wrapped in quotes.

Patch: Use bash parameter substitution to change whitespace to \whitespace.

(Note: I found it easier to apply sed one after another than to put it all in one command)

Why the fuck would anyone compress 345 images into their own archive used by an uncommon windows-only paid back-up tool?
Little me (12 years old) knowing nothing about compression or backup or common software decided to use the already installed shitty program.

This is a big deal for me because it's really the first time I string so many cool commands to achieve desired results in bash (been using Ubuntu for half a year now). Funny thing is the images uncompressed are 4.7GB and the raw files are about 1.4GB so I would have been better off not doing anything at all.

Full command:
find -type f -name "*.nco" |
sed 's/\(^./\)/\1/' |
sed 's/.*/\/media\/mitiko\/2011-2014_1&/' > unescaped-paths.txt

cat unescaped-paths.txt | while read line; do echo "${line// /\\ }" >> escaped-paths.txt; done

rm unescaped-paths.txt

cat escaped-paths.txt | while read line; do (echo "$line" | grep -Eq .*[^db].nco) && echo "$line" >> paths.txt; done

rm escaped-paths.txt

cat paths.txt | while read line; do cp $line ~/Desktop/file.zip && 7z e ~/Desktop/file.zip -oImages >/dev/null; done

Comments
Add Comment