[SOLVED] copy or move file and create target directory at the same time?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
copy or move file and create target directory at the same time?
I am posting this to the Newbie forum as it seems to be rather fundamental and an answer might be helpful here. In a nutshell, I have a bunch of files representing my monthly data backup in a stucture as shown below. My objective is to copy or move the files to two new directory trees - one containing the .tar.gz files and the other containing the .tar.gz.gpg files. Here is a sample of the source directory tree and files:
I can cp the entire /data/stage/ tree to /data/proton/ However, that copies both the .gz and .gpg files. I have not found any option to cp which will allow me to filter on only one file extension. No luck with mv either.
If I create the entire target tree manually to start with I can use a "find ... -exec" approach to copy of move the files one by one. And I could hard code the commands to create the target tree in my script. I am looking for a more elegant and flexible approach. Any advice greatly appreciated.
I do not see how tar would help. I am already using tar to combine and compress the actual source data files on a directory by directory basis. For example LibreOffice.tar.gz contains all of my Libre Office documents, spreadsheets etc. gpg with the --batch option encrypts the tar.gz file and places the results in the corresponding tar.gz.pgp file. gpg does not seem to allow me to specify a target directory for the output file. It places the encrypted file in the same directory as the source file. That is the root of the problem.
rsync with the --remove-source-files could do the trick I guess. It would of course create second copies of each file during the process as opposed to using mv. With the relatively small amount of data I am dealing with (and it is located on a high speed PCIe SSD) the downside would only be academic.
Of course I could cp the whole tree to one target directory, delete all of the .gpg files from that tree, delete all the .gz files from the original tree and mv the original to the second target. That is sort of my typical cave man with a large stone hammer approach
I do not have a problem creating the tar files where I want them in the tree. My script runs a loop for each major source data directory (/data/data/, /data/static/, /data/reference/) and places the tar files for all data subdirectories in the appropriate destination.
/data/data/LibreOffice is tarred -> /data/stage/data/LibreOffice.tar.gz
/data/data/thindebird is tarred -> /data/stage/data/thunderbird.tar.gz
I could the upload the stage/ tree to ProtonDrive. The problem is I wish to encrypt each of these files before uploading to Mega. gpg --batch places the encrypted file in the same directory as the unencrypted file.
As to rsync, I do not think that will work. If I run
Code:
rsync -r -v /data/stage/*.gpg /data/mega
rsync cannot find the files as they are in subdirectories under /data/stage/ If I run
Code:
rsync -r -v /data/stage/*/*.gpg /data/mega
rsync finds the files in question but places them all in the /data/mega/ director. The recursive option is ignored. Am I missing an option with rsync?
I will give that a try. I use rsync for some purposes and always to like to learn more about it. As to the original issue raised in this thread... I got a reply to an earlier thread
and now I can plunk my .gpg files exactly where I want them. No need to separate them as I was trying to do in this thread. Still it is good to have these options available.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.