LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2021, 01:24 PM   #1
Donny Bahama
Member
 
Registered: Aug 2009
Location: Margaritaville (a state of mind west of Las Vegas), NV
Distribution: Linux Mint
Posts: 61

Rep: Reputation: 1
Shell script to zip files from input file?


I'm trying to write a single line shell script to backup my browser profile using zip. I don't want to backup the whole thing (way too many cache files, and other non-essential stuff) - I just want the bare essentials. So I created a text file "profile_files" and populated it with all the file and directory names I want to backup. But several of these files and directories have spaces in the name. I tried wrapping each line in double quotes. That didn't work. I tried escaping the space with a preceding "" and when that didn't work I tried a back-tick instead of a back slash. No matter what I do, it skips over any file or subdirectory that has a space in the name.
 
Old 03-22-2021, 01:33 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,356
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
It'll help to see the script, but pass the paths and file names using null as a terminator rather than the default of whitespace.

If you are using find then you can use -print0. If you are using xargs thenn see the --null option. Again, it would help to see the script. Please remember to use [code] [/code] tags around it.
 
Old 03-22-2021, 01:37 PM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
AFAIK, zip -@ archive <profile_files will take file names with spaces just fine.
Code:
$ find -name f\*|zip -@ archive
  adding: a b/examples/f2 (stored 0%)
  adding: a b/examples/f1 (stored 0%)
$ unzip -l archive
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       14  04-24-2013 15:55   a b/examples/f2
       10  04-24-2013 15:55   a b/examples/f1
---------                     -------
       24                     2 files

Last edited by shruggy; 03-22-2021 at 01:45 PM.
 
1 members found this post helpful.
Old 03-22-2021, 06:50 PM   #4
Donny Bahama
Member
 
Registered: Aug 2009
Location: Margaritaville (a state of mind west of Las Vegas), NV
Distribution: Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Turbocapitalist View Post
Again, it would help to see the script.
There is no script. As I said, it'll basically be a one-line script. Something like:
Code:
#!/bin/bash
[zip command I need help with]
Quote:
Originally Posted by shruggy View Post
AFAIK, zip -@ archive <profile_files will take file names with spaces just fine.
Interesting. When I run the command, everything without a space in the name gets zipped up into the archive; everything with a space does not.
 
Old 03-22-2021, 07:08 PM   #5
Donny Bahama
Member
 
Registered: Aug 2009
Location: Margaritaville (a state of mind west of Las Vegas), NV
Distribution: Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 1
Problem solved.
This:
zip -r ChromeProfile.zip $(cat profile_files)
doesn't work

But this:
zip -r ChromeProfile -@ < profile_files
does.

Not sure why but I have a follow up question... when I create that zip file, it's creating the path within the zip file. I.e. when I open the zip file, all I see is a directory - "home". When I double click that, it takes me to [user name]. When I double click that, it takes me to ".config", etc. I'd much prefer, when I open the zip file, to just see the files and (recursed) subdirectories in /home/username/.config/google-chrome/Default
 
Old 03-22-2021, 08:15 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
If a directory contains files like
Code:
file 1.txt
file 2.txt
And profile_files contains
Code:
/home/username/path_to/file 1.txt
/home/username/path_to/file 2.txt
Code:
zip test.zip -@ < /path/to/profiles_files
adding: home/username/path_to/file 1.txt 
adding: home/username/path_to/file 2.txt 

unzip -t test.zip
Archive:  test.zip
    testing: home/username/path_to/file 1.txt   OK
    testing: home/username/path_to/file 2.txt   OK
I see you have found the problem...
 
Old 03-23-2021, 05:13 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: please don't double-click at all, instead check the output of `unzip -l myfile.zip`: what would you like to get, and what do you get instead.
 
Old 03-23-2021, 09:23 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
After the $ substitution the shell splits the result into words according to IFS (defaults to whitespace), and then (by default) attempts filename generation.
You can improve it with
Code:
IFS=; set -f; zip -r ChromeProfile.zip $(cat profile_file)
But it still can overflow with "two many arguments".
Reading the filenames from stdin or an input file is much better.
 
Old 03-23-2021, 09:43 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by Donny Bahama View Post
when I create that zip file, it's creating the path within the zip file
From the zip manual page
Quote:
-D
--no-dir-entries
Do not create entries in the zip archive for directories. Directory entries are created by default so that their attributes can be saved in the zip archive.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backed up HD to tar., ZIP, g-zip file stuck on shell prompt thefinickyhobo Linux - Newbie 16 11-20-2019 06:22 AM
[SOLVED] How can I have zip -d file.zip "__MACOSX*" work on all zip files in directory? thomwblair Linux - Newbie 10 10-08-2018 02:30 PM
[SOLVED] Write a shell script that receives a word, an input file and an output file. The scripts copies all the lines in the input file that contain mandy2112 Linux - Newbie 3 08-18-2016 10:11 AM
How to find out a file in a WAR which in a zip without unzip the zip-file? thomas2004ch Linux - Software 3 09-30-2011 03:06 PM
Shell Script for Identifying the file and zip all files, move the files to Target Dir gvenkat Linux - Newbie 3 05-07-2011 10:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration