1

I have tar.gz files accessible from

./parent/subfolder/tar_file_folder/*tar.gz

And I want to find them and move them into the ./parent/ directory, therefore one level up. However, there are several subfolders and tar_file_folders. Therefore, I want to call my command from the parent folders.

I have tried this command line:

cd ./parent

find -name '*.tar.gz' -exec mv {} /path/to/single/target/directory \;

However, I am not quite sure how to specify the path to one level up. Any help?

Two examples of path where in this case the tar files has to be either move to ./AU-ASM folder or ./AR-Vir/ folder:

./AU-ASM/besnard.sim@gmail.com-10142016-131638-715/LT51030752011211-SC20161014133947.tar.gz

./AR-Vir/besnard.sim@gmail.com-10142016-131706-095/LT51030751995263-SC20161014133510.tar.gz’

And this is an example on where the tar.gz are stored:

./Landsat_Data/AR-Vir/besnard.sim@gmail.com-10142016-131706-095/*tar.gz

The command line needs to be run from the ./Landsat_Data/ directory

  • And d should be replaced by tar? – SimonB Oct 21 '16 at 14:01
  • @don_crissti Might as well post that as a separate answer so this comment chain can go – Michael Mrozek Oct 21 '16 at 14:48
  • @nrc don didn't "repost" your answer. He posted a comment with the same basic idea as your answer while trying to understand what the OP needed. He can get no reputation or credit from that comment, so he didn't slight you in any way. His criticism is quite valid, there's no need to take it personally. Note that the comment he had posted (which is now deleted) was quoting the variables used so it didn't have the issue your answer does. I know it looks like a small change but $tar fails while "$tar" works. He also used globbing where you used find. – terdon Oct 21 '16 at 15:34
  • @terdon I promptly agreed on the IFS issue. I really do appreciate constructive critics and I agree with you 100% on what important change quoting variables is. I overreacted and "dishonest" was the wrong term. Let's say I found unfair to propose something you just criticized it and then when asked about what the improvements over the answer, edit the comment to add, only later, these quotes... – nrc Oct 21 '16 at 16:34
  • @nrc my main point is that don wasn't proposing anything. That's why he didn't post an answer but only a comment. He was trying to understand what the OP wanted. – terdon Oct 21 '16 at 16:54
3

You could iterate on tar files and move them to parent directory '..', like that:

for tar in $(find path -name '*.tar.gz'); do mv $tar $(dirname $tar)/..; done
  • this command will work whatever directory is executed from – nrc Oct 21 '16 at 13:23
  • 1
    This fails if the filenames contain any IFS chars. Avoid the for f in $(find... construct, it's very fragile... – don_crissti Oct 21 '16 at 13:30
  • I agree in general, but in this particular case the tree and filenames in the example is email/date based – nrc Oct 21 '16 at 13:33

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.