How to archive txt files greater than 30 days old

Hi There,
I am new to unix and i am trying to get a feel for the archiving utility.
I am using AIX ver 7.
I want to archive files greater than 30 days old in a directory, that have a file extension '.cpy'

Ive tried:

tar -cvf - "/*.cpy~" | gzip -c > archivedCopies.tar.gz

but get the exception:

tar: /*.cpy~: A file or directory in the path name does not exist.

I haven't tried the fileing older than 30 days command as I want to get the archive command in place first.

thanks In Advance

Welcome to the community, @dancingbush!

I think the error is quite telling. The directory doesn't exist.
Firstly, you said `

Couple of question:

  1. who has the extension .cpy? Directory OR files within the directory? (assuming files)
  2. .cpy is NOT the same as .cpy~
  3. You probably need to find the files and then tar them up. I'd concentrate on find first.
1 Like

Maybe you want the shell to expand wildcards to the matching filenames?
Then omit the quotes!
Maybe you want something like

find . -name "*.cpy" -type f -mtime +30 >/tmp/find.out

Why the quotes here? Because find itself knows how to expand wildcards, and it will do this for each sub directory it visits (while the shell would only do it for the current directory).
The dot is the current directory.
Does the filename list look good?

cat /tmp/find.out

Then use it as input for tar.
According to the online man page tar takes a -L listfile command, so it would be

tar -cvfL - /tmp/find.out | gzip -c > archivedCopies.tar.gz

The - is for the f and the /tmp/find.out is for the L

Tip: when you want to extract a tar file then always list it first:
gunzip -c | tar -tvf archivedCopies.tar.gz
before you extract it:
gunzip -c | tar -xvf archivedCopies.tar.gz

1 Like

Thanks all for going to the effort of explaining the commands , great for a novice.

The last point raises an interesting question:
Because find itself knows how to expand wildcards, and it will do this for each sub directory it visits (while the shell would only do it for the current directory).

I was wondering about this , where the Find command navigates all sub directories by default, is there a flag or any other means to prevent this behavior and confine the search to current directory?

In GNU/Linux find there is
find . -maxdepth 1 ...
And it won't visit sub directories.
find . -maxdepth 1 -mindepth 1 ...
would also skip the start directory itself (here: the current directory), but being a directory this is skipped anyway if there is -type f

In a Unix find there is only the trick:
find . \! -name . -prune ...
Prune(skip) all directories but the start directory.
More general:
start="."; find "$start" \! -name "$start" -prune ...

The less preferrable alternative is: let the shell expand the wildcards to the entry points for find:
find *.cpy -type f -mtime +30 >/tmp/find.out
Here the entry points should be files, not directories.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.