Home > Back-end >  Remove a file has the same name with folder after using tar
Remove a file has the same name with folder after using tar

Time:11-21

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.

CodePudding user response:

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.

  • Related