Tar command generation with Find


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Tar command generation with Find
# 1  
Old 04-21-2017
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.

Code:
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

# 2  
Old 04-21-2017
tar -T - reads filenames from stdin(= from find). ... if your version of tar supports -T

Code:
find /your/path -mtime -10 | tar -cf backup.tar -T -

This User Gave Thanks to stomp For This Post:
# 3  
Old 04-21-2017
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.
Code:
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.
# 4  
Old 04-21-2017
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

Last edited by rbatte1; 04-21-2017 at 11:12 AM.. Reason: Corrected tag
# 5  
Old 04-21-2017
Quote:
Originally Posted by royinfo.alain
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.

Code:
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


Last edited by Corona688; 04-21-2017 at 12:57 PM.. Reason: fix typo, file2/file
These 2 Users Gave Thanks to Corona688 For This Post:
# 6  
Old 04-21-2017
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.

Code:
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

Last edited by Corona688; 04-21-2017 at 03:16 PM..
# 7  
Old 04-21-2017
Please use code tags, the Image button, not icode tags.

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

The specified substitution is not valid for this command.
What shell are you using? That works in both BASH and KSH.

You are correct, though, in that it doesn't measure times.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single command - unzip files from a tar command

I have a tar file that contains multiple .Z files. Hence I need to issue a tar command followed by a gzip command to fully extract the files. How do I do it in a single command? What I'm doing now is tar xvf a.tar (this will output 1.Z and 2.Z) gzip -d *.Z (to extract 1.Z and 2.Z) (9 Replies)
Discussion started by: ericlim
9 Replies

2. Shell Programming and Scripting

find + tar + gzip + uunecode/email --> in one command?

How to search for all files with matching strings --> find + tar + gzip + uunecode/email them in one command? I am sure there is a right way to pass list of files to tar, then compress tar file. Then send that as attachment using uuencode in one command.. Can we do that!? (3 Replies)
Discussion started by: kchinnam
3 Replies

3. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

4. Shell Programming and Scripting

Dynamic command line generation with awk

Hi, I'm not an expert in awk but i need a simple script to do this: I'd like to AutoCrop PDF files. I found 2 simple script that combined together could help me to automatize :) The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf. The pdfinfo output... (8 Replies)
Discussion started by: gbagagli
8 Replies

5. Shell Programming and Scripting

tar command dont tar to original directory

HI, if I have a tarfile called pmapdata.tar that contains tar -tvf pmapdata.tar -rw-r--r-- 0/0 21 Oct 15 11:00 2009 /var/tmp/pmapdata/pmap4628.txt -rw-r--r-- 0/0 21 Oct 14 20:00 2009 /var/tmp/pmapdata/pmap23752.txt -rw-r--r-- 0/0 1625 Oct 13 20:00 2009... (1 Reply)
Discussion started by: borderblaster
1 Replies

6. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

7. AIX

command usage on find with xargs and tar

my task : tar up large bunch of files(about 10,000 files) in the current directories that created more than 30 days ago but it come with following error find ./ -ctime +30 | xargs tar rvf test1.tar tar: test1.tar: A file or directory in the path name does not exist. (3 Replies)
Discussion started by: darkrainbow
3 Replies

8. UNIX for Advanced & Expert Users

How to restrict Core file generation after scp (of SSH) command executed in UNIX

Hi, I am getting core file in local machine after trasfer files to other machine by using scp (secure copy) of SSH in UNIX. Could any one please tell me how to restrict core file generatation by using scp command. (4 Replies)
Discussion started by: nrsekhar
4 Replies

9. UNIX for Advanced & Expert Users

How to use the command Tar to find a file

Hello, I am just using the command Tar to find if a file was backuped on my tape. Can you help me with that command. I am using the command : tar -tvf /dev/rmt/4m /kcc_dms/AC7/c15a* > toto.txt to find all files c15a* in these subdirectories on my tape. But it doesn't work correctly. Can... (2 Replies)
Discussion started by: steiner
2 Replies

10. Solaris

Bar code generation in to the text file and printing the same using lp command.

Hi, I need the information regaring bar code printing. I am looking for the program which runs on solaris that take number string (like mobile number, bill number etc) as input and generate bar code font in to the text file. Remember it is not the bit map. The program just writes the fonts in to... (0 Replies)
Discussion started by: Manjunath Naik
0 Replies
Login or Register to Ask a Question