Tar command generation with Find

Hello,

I'm trying to generate a TAR command and fill it with the result of a Find command. What I did is not elegant and I'm sure that it can be in a different way to be faster.

I need to Find all files with an extension to ".sf" . If a file is found, I need to replace the extension ".sf" to ".sfd" and see if this file exist.. if yes, this is the file (.sfd) that I need to backup, by using the Tar command. The goal is to backup all existing ".sfd" related to a ".sf" file in one big Backup file (TAR).

Below the code that I did.

export tar_cmd='"tar -cvf - '

# Extract .sfd related to .sf files

find *.sf -type f -mtime -$nb_days | sed -e "s/.sf/.sfd/g" | while read FileName
do
[ -f $FileName ] && export tar_cmd=$tar_cmd" "$FileName
done

export tar_cmd=$tar_cmd' | gzip > $MAITUT/BCK_DATA_sfd.tar.gz"'

# The command below will Backup all .SFD file related to .idx
$tar_cmd  

tar -T - reads filenames from stdin(= from find). ... if your version of tar supports -T

find /your/path -mtime -10 | tar -cf backup.tar -T -
1 Like

Can you tell us what you think this does? - I can see what it will do but I do not see a reason for it. Note: Without the sed command your script becomes a simple one liner.

sed -e "s/.sf/.sfd/g"

Thanks.

FWIW - In general, correctness and readability are more important on production systems than elegance. Why? Because somebody else will have to work on your code. If they find something questionable it makes their job much harder than it needs to be.

Stomp did remove the sed command from the loop -> one line.

Hello Stomp, the -T is not supported. Also the file that I need to backup is not the one that I found.. First I need to find files with extension .sf and replace the .sf with .sfd and if this one exist, this is the file that I want to backup. .sfd

thanks
Alain

---------- Post updated at 09:33 AM ---------- Previous update was at 09:27 AM ----------

Hello Jim,

I'm using the sed to replace .sf by .sfd.

Because at the end I want to backup file.sfd not file.sf

the .sf is used to find specific files and after that the sed is used to replace .sf to .sfd and I will loop in the result of the find (while) to verify if the file.sfd exist. If yes, this is the file that I want to backup (.sfd)

thanks

Hm. If .sf are files, there's no point using find here, since a simple loop will work just as good.

FILES=""
for FILE in *.sf
do
        FILE2="${FILE/.sf}".sfd
        [ -e "$FILE2" ] && FILE="$FILE2"
        # Add .sfd to list if exists, .sf if not
        FILES="$FILES $FILE"
done

echo tar -cf - $FILES
2 Likes

Thanks Corona688,

I need to select only files modified since last x days, this is why in the first post you can see -mtime -nbdays on my find command.

And Yes .sf are files.

I don't understand the section :

# Add .sfd to list if exists, .sf if not

I need to generate 1 tar file that will contain all files selected (.sfd).

1 - find all .sf file modified since last 2 years
2- with that list of files, see if a file (same filename) but with the extension .sfd exist. if the .sfd exist keep only the .sfd file.. IF not exist skip the file and process the next .sf file.

thank you

---------- Post updated at 12:47 PM ---------- Previous update was at 12:08 PM ----------

Corona688,

the fact that I'm just a beginner in Unix, I just understand what you saying..

FILES="$FILES $FILE"

At the end, $FILES with contain all .sfd file picked ||

Just need to be able to select only files (.sf) where the last mods date is max 2 years.

thanks

---------- Post updated at 01:09 PM ---------- Previous update was at 12:47 PM ----------

I tried the commands (from Corona688) and I got the error :

The specified substitution is not valid for this command.

related to the line :

FILE2="${FILE/.sf}".sfd

thanks

---------- Post updated at 02:01 PM ---------- Previous update was at 01:09 PM ----------

Hello,

I find a way to replace the .sf by .sfd without error by usinf sed .

FILES=""
for FILE in *.sf
do
FILE2=`echo $FILE | sed 's/\.sf/.sfd/g'` 
[ -e "$FILE2" ] && FILE="$FILE2"
FILES="$FILES $FILE"
done
echo tar -cf - $FILES

Now, what is missing is the way to select only files (.sf) where the last mod date is 2 years.

Something similar that I did in a find command, but need to be done in the FOR command that Corona688 gave us.

Something similar to :
find *.sf -type f -mtime -$nb_days | ...

thanks a lot

Please use code tags, the button, not icode tags.

What shell are you using? That works in both BASH and KSH.

You are correct, though, in that it doesn't measure times.

1 Like

I'm working on an HP unix !!
I don't know which shell is it .. maybe bourne..

Do you have an idea how I can combine the FOR and mtime
or mtime it can only use with the command find ?

thanks

What output do you get from running the following two commands:

uname -a
ls -ld /bin /usr/bin /bin/bash /bin/ksh /bin/sh /usr/bin/bash /usr/bin/ksh /usr/bin/sh

Thanks to all..

I used the code below and works fine & fast.

set +x
 FILE_TMP=""
FILES=""
 find *.sf -type f -mtime -$nb_days | sed -e "s/.sf/.sfd/g" | while read FileName
do
       FILE_TMP=""
       [ -f $FileName ] && FILE_TMP="$FileName"
       FILES="$FILES $FILE_TMP"
done
 set -x

Little shorter and little safer

set +x
FILES=""
find *.sf -type f -mtime -$nb_days | sed -e "s/[.]sf/.sfd/" |
while read FileName
do
       [ -f "$FileName" ] && FILES="$FILES $FileName"
done
set -x

This only works with ksh or a ksh-derived sh. Other shells will run the while loop in a sub shell that does not return $FILES to the main shell.

Thanks for the Info,

I will use yours.