Sunday 20 January 2019

command line - Copy a list of files preserving directory structure, without scripts


Is there a way to copy a list of files, that share a common base directory, preserving their relative directory structure? The files with relative paths are listed in a text file. edit I have the filenames listed in a plain text file.


I'd like to do this without using a script (no bash/batch/python/ruby/lua/etc.), similar to how wget -i works for urls. (My reason is that it'd give overhead/non-standard solution for a reasonably common task.)


I've tried cat list.txt | xargs -I % cp % new_folder from Copy list of files but it didn't preserve the relative directory path.


I'd do this to export my selection of files (that I already have in a text file) from a directory structure that has big number of files (happens from time to time).



Answer



tar cvf - ./filesAndDirecotries | (cd targetDirectory ; tar xvf - )


TAR (Tape Archive is old command to create an archive file) can write to stdin ( " - " parameter for the file ) and pipe and read from stdin in the second directory.


Above command first creates a tar file and writes to stdin and piped to a shell and change directory and untar the file by reading it from stdin.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...