Find command outputs files without a directory structure

directoryfindtar

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?

Best Answer

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.

Related Question