Hi,
There's a few ways to approach this, but using the find
command as you're starting to do will certainly do the trick. However, as things stand, your find
command isn't actually going to do anything other than print out the message Found file <FILENAME>
for all files with the extension txt
underneath the directory /var/tmp/NewFile/Test
.
Taking things in turn:
-name '*.txt'
This will match all files whose name ends with the extension .txt
-mtime 0
Not one I've seen often phrased in this particular way, but this will have the effect of matching all files modified between now (the time when the command is run) and one day ago. Personally I usually phrase this as -mtime -1
(meaning all files modified less than one day ago), but this is fine, if that's what you're wanting to find.
Now, you then go on to pipe the output into a while
loop for some reason, which I'm not too clear on - you don't really have to do this, as the find
command is capable of operating directly on what it finds via the -exec
flag. This will cause any given command to be executed on whatever the find
command ha found.
So, for example: let's say you wanted to find all files whose extension was .txt
and which were modified less than one day ago, and then you wanted to move them to another directory (let's call it /tmp/recent-files
for the sake of our example). This could be achieved with a single find
command like this:
find /var/tmp/NewFile/Test -mtime 0 -name '*.txt' -exec mv -fv \{\} /tmp/recent-files/ \;
So let's look at this last bit in more detail, as this is probably new to you.
Firstly, -exec
is the flag that tells find
that we want to run a command on all the things we've found that match our patterns up to this point.
Next, we have mv -fv \{\} /tmp/recent-files/ \;
- this is the actual command we want to run, used with some find
-specific syntax. Breaking it down further: mv -fv
is the first part of our move command, and is just as normal (the flags you use here can be whatever you want, but I went with -fv
to ensure files get moved no matter what, and that it will print the name of each file it moves).
Now, here comes the interesting bit. The meaning of \{\}
is "the filename of whatever matched file we are currently considering". So the two curly brackets are a stand-in for the filename of whatever thing find
has found which matched your conditions.
Next is the second part of the mv
command, which is the destination you want to move things to - in our case, that's /tmp/recent-files/
.
Lastly, we end with a single semi-colon symbol, \;
. This marks the end of our -exec
to the find
command.
So the end result of this particular find
command is to match all files underneath /var/tmp/NewFile/test
which were modified between now and 1 days ago and which end with the extension .txt
, and to move all such matching files one at a time into the directory /tmp/recent-files/
.
Hope this helps ! You should be able to play about with the syntax of the above to achieve whatever result you're wanting, but if you have any other questions let me know and I'll see if I can lend a further hand.