Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tar errors???

13 views
Skip to first unread message

cerr

unread,
May 23, 2013, 5:08:01 PM5/23/13
to
Hi,

I need to tar 20 files from within a certain directory and for that, I exercise following command:

/bin/tar -C bin/ -cvjf bin/136934285324.tar.bz2 $(ls bin/*.bin| head -20)

but I get these errors:


tar: bin/04ee75.1369342806.bin: No such file or directory
tar: error exit delayed from previous errors


How can I get rid of these errors? The command seems to be executed properly and I get my archive created...

Thanks!

Dave Gibson

unread,
May 23, 2013, 7:21:44 PM5/23/13
to
cerr <ron.e...@gmail.com> wrote:
> Hi,
>
> I need to tar 20 files from within a certain directory and for that, I
> exercise following command:
>
> /bin/tar -C bin/ -cvjf bin/136934285324.tar.bz2 $(ls bin/*.bin| head -20)
>
> but I get these errors:

cd bin
list=( *.bin )
cd -
/bin/tar -C bin -cvjf bin/136934285324.tar.bz2 "${list[@]:0:20}"

>
>
> tar: bin/04ee75.1369342806.bin: No such file or directory
> tar: error exit delayed from previous errors
>
>
> How can I get rid of these errors? The command seems to be executed
> properly and I get my archive created...

The list generated by ls has leading directories. Tar changes its
working directory (into bin/) before starting to add files to the archive
so won't find the files if they have a leading directory.

$( cd bin && ls *.bin | head -n 20 )

cerr

unread,
May 24, 2013, 7:56:47 PM5/24/13
to
fine, I thought that everything after the '$' gets executed separately from the actual tar command.
I now tried both and got this:

# touch ram/bin/ABCDEF.1234567890.bin
# /bin/tar -C ram/bin/ -cvjf ram/bin/136943948211.tar.bz2 $(ls *.bin| head -20)
tar: macs.bin: No such file or directory
tar: error exit delayed from previous errors
# /bin/tar -C ram/bin/ -cvjf ram/bin/136943948211.tar.bz2 $(ls ram/bin/*.bin| head -20)
tar: ram/bin/ABCDEF.1234567890.bin: No such file or directory
tar: error exit delayed from previous errors
# ls ram/bin/ABCDEF.1234567890.bin
ram/bin/ABCDEF.1234567890.bin

I get errors both ways round! I can't explain that, anyone???

Thanks!!!

David W. Hodgins

unread,
May 24, 2013, 9:41:35 PM5/24/13
to
Please get an account at a real usenet server, such as
www.eternal-september.org, and use a real nntp client. Google groups
is mangling all of your replies, adding blank lines (Which I've removed
above, between every line quoted from prior messages.

The problem is being cause by a combination of when the * is expanded,
and command expansion. In addition, using ls is not recommended, if
any of the filenames might contain whitespace (spaces, newlines, etc).

Please read "info bash", in the section "Shell Features", particularly
"Shell Expansions". The order of expansions is important to understand.

Skimming through http://tldp.org/LDP/abs/html/index.html would be
useful too.

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)

Dave Gibson

unread,
May 25, 2013, 10:09:35 AM5/25/13
to
cerr <ron.e...@gmail.com> wrote:
> On Thursday, May 23, 2013 4:21:44 PM UTC-7, Dave Gibson wrote:
>> cerr <ron.e...@gmail.com> wrote:
>>
>> > Hi,
>> >
>> > I need to tar 20 files from within a certain directory and for that, I
>> > exercise following command:
>> >
>> > /bin/tar -C bin/ -cvjf bin/136934285324.tar.bz2 $(ls bin/*.bin| head -20)
>> >
>> > but I get these errors:
>>
>> cd bin
>> list=( *.bin )
>> cd -
>> /bin/tar -C bin -cvjf bin/136934285324.tar.bz2 "${list[@]:0:20}"

That method will avoid problems with filenames containing spaces.

>>
>> >
>> >
>> > tar: bin/04ee75.1369342806.bin: No such file or directory
>> > tar: error exit delayed from previous errors
>> >
>> >
>> > How can I get rid of these errors? The command seems to be executed
>> > properly and I get my archive created...
>>
>> The list generated by ls has leading directories. Tar changes its
>> working directory (into bin/) before starting to add files to the archive
>> so won't find the files if they have a leading directory.
>>
>> $( cd bin && ls *.bin | head -n 20 )
^^^^^^
That part is essential.

>
> fine, I thought that everything after the '$' gets executed separately
> from the actual tar command.

It does.

> I now tried both and got this:
>
> # touch ram/bin/ABCDEF.1234567890.bin
> # /bin/tar -C ram/bin/ -cvjf ram/bin/136943948211.tar.bz2 $(ls *.bin| head -20)

ls would run in the current working directory so the list of files generated
would not be available after tar changes its working directory to ram/bin.

cd into ram/bin, within the $(...) expansion, before running ls.

tar ... $( cd ram/bin && ls *.bin | head -n 20 )
^^^^^^^^^^
Which still won't handle spaces in filenames. Use the array method.

> tar: macs.bin: No such file or directory
> tar: error exit delayed from previous errors
> # /bin/tar -C ram/bin/ -cvjf ram/bin/136943948211.tar.bz2 $(ls ram/bin/*.bin| head -20)

ls' output would include the leading path (e.g. ram/bin/foo.bin).

> tar: ram/bin/ABCDEF.1234567890.bin: No such file or directory
> tar: error exit delayed from previous errors
> # ls ram/bin/ABCDEF.1234567890.bin
> ram/bin/ABCDEF.1234567890.bin

Tar was given a list of filenames containing leading paths (ram/bin/foo.bin).
After tar changed its working directory to ram/bin there was no ram/bin/*.

cerr

unread,
May 27, 2013, 12:24:03 PM5/27/13
to
Hi Dave,

Thank you for taking the time to assist me with this.
However, one more question:

I now have /bin/tar -C ram/bin/ -cvjf ram/bin/136967148910.tar.bz2 $(cd ram/bin/ ls +*.bin| head -20)

which seems to work but as you say:

> Which still won't handle spaces in filenames. Use the array method.

I'm unsure how to use the array method, I've tried stuff with |@|0:20 but my shell doesn't seem to like this. I'm not running on bash, my application is running on /bin/sh, may that be the reason?
As for the spaces, I don't expect that this would ever become an issue as my applicatoin is in charge of the file creation, always! Howevcer, if spaces were supported, it definitely would be a cleaner approaach...
Any hints?

Thanks!
Ron

Dave Gibson

unread,
May 27, 2013, 3:12:21 PM5/27/13
to
cerr <ron.e...@gmail.com> wrote:
> Hi Dave,
>
> Thank you for taking the time to assist me with this.
> However, one more question:
>
> I now have /bin/tar -C ram/bin/ -cvjf ram/bin/136967148910.tar.bz2 \
> $(cd ram/bin/ ls +*.bin| head -20)
>
> which seems to work but as you say:
>
>> Which still won't handle spaces in filenames. Use the array method.
>
> I'm unsure how to use the array method, I've tried stuff with |@|0:20
> but my shell doesn't seem to like this. I'm not running on bash, my
> application is running on /bin/sh, may that be the reason?

Yes. Is ksh (the real one -- ksh93) available?

> As for the spaces, I don't expect that this would ever become an issue
> as my applicatoin is in charge of the file creation, always! Howevcer,
> if spaces were supported, it definitely would be a cleaner approaach...
> Any hints?

Use globbing and the positional parameters ("$@").



cd ram/bin
set -- *.bin
cd -
# How many filenames in the list?
want=20
# If there are more than $want elements in the argument list, rearrange
# the list so that the first $want elements are at the end then shift
# everything else out.
if [ $# -gt $want ]; then
i=0
while [ $i -lt $want ]; do
set -- "$@" "$1"
shift
i=$(( $i + 1 ))
done
shift $(( $# - $want ))
fi
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 "$@"




To have the list in an order other than that provided by globbing it
will be necessary to use command substitution.

The following approach breaks on filenames containing newlines. Then
again, if you have filenames containing newlines this thing breaking
is unlikely to be your main problem.



list=$( cd ram/bin && ls *.bin | head -n 20 )
# Restrict word splitting to newlines
oifs=$IFS
IFS='
'
# Prevent the shell expanding any wildcards in filenames while expanding
# the unquoted $list (tar's arguments).
set -f
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 $list
set +f
IFS=$oifs



# Similar, using the positional parameters.
oifs=$IFS
IFS='
'
set -f
set -- $( set +f ; cd ram/bin && ls *.bin | head -n 20 )
set +f
IFS=$oifs
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 "$@"


0 new messages