Akshit Gupta
Akshit Gupta

Reputation: 17

Create a empty tar file and then store its name in a variable

I am writing a shell script in which a tar file with today's date and time will be created.

tar -zvcf "log_grabber$(date '+%y-%m-%d_%H%M').tar.gz" --files-from /dev/null

Now, to add more files to this tar files after running the find command. How can I get the name of the tar file and use it in the output of the find command?

find . -type f -name 'local*' -newermt "$user_date" -exec tar -rvf <variable tar file> {} \;

Any help will be very much useful.

Upvotes: 0

Views: 662

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

Instead of

tar -zvcf "log_grabber$(date '+%y-%m-%d_%H%M').tar.gz" --files-from /dev/null

Create a variable with the name first and use that:

name="log_grabber$(date '+%y-%m-%d_%H%M').tar.gz"    
tar -zvcf "$name" --files-from /dev/null

And then:

find . -type f -name 'local*' -newermt "$user_date" -exec tar -rvf "$name" {} +

Note that I changed \; to + so that tar gets multiple files in one invocation, rather than one tar invocation per file.

Upvotes: 1

Related Questions