-1

I have folder and subfolder. I need to loop through each folder and subfolder and remove or move the file names which start with abc.txt and 14 days old to temporary folder. My folder tree structure is:

folder and subfolder look

The file may be inside the folder or subfolder 'abc.txt'

I have used this below code but not working.

  1. I took the folder paths into a list.txt file using below command

    find $_filepath -type d >> folderpathlist.txt
    
  2. I pass the path list to below code to search and remove or move files to temporary folder

    find folderpathlist.txt  -name "abc*" -mtime \+14 >>temp/test/
    

How do I achieve this scenario ?

jww
  • 97,681
  • 90
  • 411
  • 885
  • Youu should probably use `-exec cp ...` or `-exec mv ...`. Possible duplicate of [Find and copy files](https://stackoverflow.com/q/5241625/608639), [find and copy file using Bash](https://stackoverflow.com/q/1562102/608639), [How to move or copy files listed by 'find' command in unix?](https://stackoverflow.com/q/17368872/608639), etc. – jww Jun 12 '19 at 12:49

1 Answers1

1

You want to find files: -type f
that start with abc.txt: -name "abc.txt*"
that are 14 days old: -mtime +14
and move them to a dir.: -exec mv {} /tmp \;
and to see what moved: -print

So the final command is:

find . -type f -name "abc.txt*" -mtime +14 -exec mv {} /tmp \; -print

Adjust the directory as required.

Note that mtime is the modification time. So it is 14 days old since the last modification was done to it.

Note 2: the {} in the -exec is replaced by each filename found.

Note 3: \; indicates the termination of the command inside the -exec

Note 4: find will recurse into sub-directories anyway. No need to list the directories and loop on them again.

Nic3500
  • 8,144
  • 10
  • 29
  • 40