To find with variable value when using find in Linux shell script.

Asked 2 years ago, Updated 2 years ago, 100 views

Writing shell scripts on Linux.

First, the variable name contains the date.

date=`/bin/date +%Y%m%d -d '1 day ago'`

The date variable contains yesterday's date. Same as 20161026.

Then find directories with this date as the directory name and tagvfz {}.You want to compress it to tar.gz.

However, there are directories with numbers such as _1 and _2 among the directories that the problematic part should be looking for.

20161026, 20161026_1, 20161027, ... In these cases, I tried to use the following command.

find/path -name '${date}*' -exec tar cvfzP/path/{}.tar.gz{} \;

Each of the directories retrieved using the above command is tar.You want to compress it into gz.

However, now that I put a variable in the file name I'm looking for, I don't think I can search properly.

How to search for both 20161026 and 20161026_1 directories and directory names respectively.Can I compress it to tar.gz?

linux shell-script

2022-09-22 14:25

1 Answers

I'll ask myself...

find/path -name "${date}*" -exec tar cvfzP/path/{}.tar.gz{} \;

As shown above, the file name is tied to a double quart instead of a single quart, and it comes out.

However, the result of the find is not just the file name, but also the path name There is an error in the tar command at the back.


2022-09-22 14:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.