3

I want to tar some Python files and send it across a server. I do:

find /some/path -iname "*.py"  -exec tar czfv python_files.tar {} +

Followed by:

pv python_files.tar| ssh some_user@some_server 'cat | tar xz -C /some/path/on/server'

Unfortunately when I SSH on the server I see the full directory structure has been sent across the server and I find my python files like that:

/some/path/on/server/complicated/sub/directory/some_file.py
/some/path/on/server/another/complicated/sub/directory/some_file2.py

What I want is my files to be at the root:

 /some/path/on/server/some_file.py
 /some/path/on/server/some_file2.py

What is the missing piece?

1
  • 3
    What do you want to do when you have files with the same name in different subfolders? Commented Mar 20, 2019 at 11:02

2 Answers 2

3

as per man tar (a long reading)

--transform=EXPRESSION, --xform=EXPRESSION
Use sed replace EXPRESSION to transform file names.

I used

find . -name \*.py -print | xargs tar cf tmp7/test-py.tar --transform=s:./.*/:: -

all files were on same level.

sed expression

  • s:./.*/:: : (greedy) replace ./.*/ by nothing.

bonus:

find . -name \*.py -print | sed -e s:./.*/:: | awk 'a[$1]++ { print ; }'

will print dupplicate filename.

0
1

Like the solution from user Archemar, only applied when extracting the tar. Substituting .*/ with the empty string.

pv python_files.tar| ssh some_user@some_server 'cat | tar xz --transform=s,.*/,, -C /some/path/on/server'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .