2

Firstly come background I am trying to write an executable shell script I can easily map as the default for .tgz files to auto-extract.

so far so simple

tar -xzvf $1

which works to drag/drop but not if I double click. Since I can use it if files are in the same location I assume it is because of the current working directory of the script.

However equally shouldn't $1 pass the full file path to the tar command so it doesn't matter?

Anyway I then thought the answer would be the pwd command then the dirname command i.e.

cd 'dirname $1'

but that didn't work A google around suggested

cd -P -- "$(dirname -- "$1")" && pwd -P

But I still can't get this script to work. It may not be the right error.

1
  • 1
    You should add information on why/how the cd command fails. – schily Feb 2 at 15:23
0

No, $1 does NOT expand to the full file path, it actually is the exact first argument - also if the file is non-existent.

A simple echo $1 does reveal this.

Instead of a clumsy directory change, just make use of the -C flag.

man tar

-C, --directory=DIR

Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow.

I.e. your script would be:

#!/bin/bash
tar -C "$(dirname "$(readlink -f "$1")")" -xzvf "$1"
11
  • thanks mate. It must be another issue. Still only works on drag/drop but if set as the default handler. It outputs Cannot open file no such file or directory. – MANICX100 Feb 3 at 11:07
  • Is your script in the PATH? How did you specify the handler? – FelixJN Feb 3 at 11:08
  • Is not in path. Was specified through open via custom command using the standard pcmanfm file properties panel – MANICX100 Feb 4 at 12:57
  • Did you use quotes? e.g. "$1"? How about using the full path -xzvf "$(readlink -f "$1")"? Anything noticeable when you run it from the shell via bash -x script.sh test.tar.gz? – FelixJN Feb 4 at 13:59
  • No change on using the quoted command. running using bash -x it works as it did before and uses the current working directory not switching. – MANICX100 Feb 6 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.