0

I created a script to find files, move them to a folder, compress this folder using tar. Then delete original folder. But after running the script, the folder was removed but a file having same name with the folder was created. I tried to run one by one command, it's OK. There is not this file. I added rm command in the script to remove it but not working. I don't know why this file was create.

My script is below:

#!/bin/bash

cd /home/tuan/testrm/9/
mkdir compressdir
sudo find . -type f -name "*.txt" -print | xargs -I {} mv {} compressdir > /dev/null 2>&1 &
tar -cvzf compresslog.tar.gz --remove-files compressdir
mv compresslog.tar.gz compresslog-`date +"%d%m%Y"`.tar.gz
rm -rf compressdir

image show

I want to know why this file was create and how to prevent this happen.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37
tuanle
  • 13
  • 3

1 Answers1

0

You should remove & at the end of sudo find line, and it will works.

Because of The & makes the command run in the background.

Root cause: the sudo find command line and tar -> mv -> rm run at synchronized.

If you had checked the file compresslog.tar.gz which the script generated, you will found that it was null or error, and the compress file contain the same content with someone file which you find.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37
  • 1
    Thank you for your help. It works. I used $ it because I want it run in the background. I'll try another way. – tuanle Nov 21 '22 at 08:21
  • Welcome, you could try this: `{ command 1; command 2; } &` run the multiple commands at backgroud. – Victor Lee Nov 21 '22 at 08:31