Hello
What I need is a script to delete all files from the month and leave only one ( the final one ) It will be marked as _final. The idea behind it:
In a Sharepoint site - in a document library - we are having to many files named
BTL_STOCK28732846
BTL_STOCK2873284fd
BTL_STOCK287328464
BTL_STOCK28732867u
and so one... exported from a software tool that we are using. So we do not need all those files but one. And this will be per month. So the script need to connect to office 365 , search in the rights Document library and sort the files. Then when find file starting with BTL_STOCK but ended with ..._final to excluded (leave it in the folder). The rest starting with BTS_STOCK -be deleted. I will add it in task schedule then and will run once a month and delete all files by name but leave for every month _final one.
Let us consider Documents directory contains the below files
$cd Documents
$ ls -lrt
BTL_STOCK28732846
BTL_STOCK2873284fd
BTL_STOCK287328464
BTL_STOCK28732867u
BTL_STOCK_final
Now we need to sort the files
$find /Documents -type f -exec sort -o {} {} > file.txt ----
, this command first searches for documents directory and
performs/executes sort
on all files present in that directory and redirects the output to
file.txt
Now we need to delete all files except the file which contains "final" in it's name
This can be done with :
sed '/final/!d' file.txt ---don't delete the file containing final in file.txt
Now file.txt contains only the file with "BTL_STOCK_final"
Get Answers For Free
Most questions answered within 1 hours.