spaces in filename

Hello I can�t find an answer to my problem.
I am trying to tar some files with spaces

#!/bin/sh
files="/var/installer/server Config
/var/installer/client user
/var/installer/Svenskt Language
/var/installer/GUI user Plugin
/var/installer/Firefox Plugin"

tar -czvf /tmp/files.tar.gz $files

I have tried escaping the spaces with \ but still errors

How about ...

tar -czvf /tmp/files.tar.gz "$files"
tar -czvf /tmp/files.tar.gz "$files"

It tries the whole variable as one file name

tar: /var/installer/server Config
/var/installer/client user
/var/installer/Svenskt Language
/var/installer/GUI user Plugin
/var/installer/Firefox Plugin: No such file or directory

if i use one variable to each file it works

tar -czvf /tmp/files.sat.tar.gz $file1 $file2 $file3 

but after 20 files it is difficult to maintain.

Plan B:

[house@leonov] ll
-rw-r--r-- 1 house house    0 20. Jul 12:12 1st file
-rw-r--r-- 1 house house    0 20. Jul 12:12 2nd file
-rw-r--r-- 1 house house    0 20. Jul 12:12 3rd file
[house@leonov] cat list
./1st file
./2nd file
./3rd file
[house@leonov] tar -cf test.tar --files-from list
[house@leonov] tar -tvf test.tar
-rw-r--r-- house/house       0 2010-07-20 12:12 ./1st file
-rw-r--r-- house/house       0 2010-07-20 12:12 ./2nd file
-rw-r--r-- house/house       0 2010-07-20 12:12 ./3rd file

thanks for that but it is not working

it is an embedded system

Usage: tar -[czxtvO] [-f TARFILE] [-C DIR] [FILE(s)]...

Create, extract, or list files from a tar file

Options:
        c       Create
        x       Extract
        t       List

Archive format selection:
        z       Filter the archive through gzip

File selection:
        f       Name of TARFILE or "-" for stdin
        O       Extract to stdout
        C       Change to directory DIR before operation
        v       Verbose

It's not working, mostly because you're asking it to write to another file extension...namely a gzipped format. Have you looked for the a similar file named .tar?

Otherwise, try the following:

tar czvf - $(find /var/installer -name "* *" -type f ) |gzip -9 - >/tmp/files.tar.gz

it is not the receiving file where the problem is, it is the interpretation from the variable to tar that is failing

this do not work

#!/bin/sh
files="/var/installer/server\ Config
  /var/installer/client\ user
  /var/installer/Svenskt\ Language
  /var/installer/GUI\ user\ Plugin
 /var/installer/Firefox\ Plugin"

tar -czvf /tmp/files.tar.gz $files

it don�t find
tar: Config: No such file or directory
tar: /var/installer/server\: No such file or directory

It look likes it do not under stand '\ ' in the file name

but this work

#!/bin/sh
tar -czvf /tmp/files.tar.gz  /var/installer/server\ Config /var/installer/client\ user /var/installer/Svenskt\ Language /var/installer/GUI\ user\ Plugin /var/installer/Firefox\ Plugin

You're using options available in GNU tar so I'm assuming you have the bash shell installed. If so, you could use arrays, instead of a scalar variable to store the filenames:

files=(
  '/var/installer/server Config'
  '/var/installer/client user'
  '/var/installer/Svenskt Language'
  '/var/installer/GUI user Plugin'
  '/var/installer/Firefox Plugin'
  )

tar zcfv /tmp/files.tar.gz "${files[@]}"

./filer.tar.gz.sh: 2: Syntax error: "(" unexpected
# sh
BusyBox v1.01 (2009.06.07-19:25+0000) Built-in shell (ash)

Thanks any way. it looks like arrays don�t work in this shell.

Yep,
this details are important :slight_smile:

With ash you could try something like this (assuming no newline character(s) in the filenames):

files="var/installer/server Config
/var/installer/client user
/var/installer/Svenskt Language
/var/installer/GUI user Plugin
/var/installer/Firefox Plugin"

(
  IFS='
'
tar czfv files.tar.gz $files
  )
1 Like

For those who are following this thread. Cross reference to Busybox command syntax.

BusyBox - The Swiss Army Knife of Embedded Linux

Try:

#!/bin/sh

files="'/var/installer/server Config' \
'/var/installer/client user' \
'/var/installer/Svenskt Language' \
'/var/installer/GUI user Plugin' \
'/var/installer/Firefox Plugin'"

eval tar -czvf /tmp/files.tar.gz "$files"

Regards,
Alister

... and hoping someone does not add something like this to the list of filenames:

;rm -rf *

I don't mean to be rude, but that's a foolish comment without merit which only serves to engender unwarranted distrust in newbies.

That file list is static, not dynamic and does not blindly eval user provided text. An eval which evals static text is not in any way a problem or a security risk.

However, if unauthorized personnel are editing your scripts, you got problems. Why would anyone bother adding "; rm -fr *" to an eval statement when with the access required to accomplish that they could simply delete the entire script and replace it with a single rm command?

Regards,
Alister

This is working

Thank you all for your help