• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Script to remove older files from various directories

Resolved: Script to remove older files from various directories

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I want to remove files(whose name contains “extract”) older than 7 days from various directories in a particular path. If I run the script without any argument the script should be able to delete the files from all the directories inside that particular path. If I run the script using any arguments , then the script should not delete files from those specific directories.
Example :-
Path – /languages/analytics
Let’s assume directory “analytics” contains 10 dirctories like c,java,javascript,python,shell,golang,scala,ruby,react,angular.This directory also contains few files along with these 10 directories.
Scenario1 – I want to remove the files older than 7 days from all the 10 directories.
Scenario2 – I want to remove the files older than 7 days from all directories except angular.
Scenario3 – I want to remove the files older than 7 days from all directories except ruby,react,shell.
So basically whatever arguments I pass , the script should not delete files from those particular directories.The number of arguments may vary as per requirement.And if I run without arguments, then the script should delete older files from all the directories in that path.The number of directories may vary as well.
Here is what I have written so far …
The above script works fine when I run it without arguments. It runs fine when I pass 1 argument.
sh scriptname -> successfully deletes files from all directories in /languages/analytics path.
sh scriptname ruby -> successfully delete files from all directories except ruby.
Can you please advise how to enhance this script to take as many arguments as possible and the script should not delete files from those particular directories which are passed in the argument ?

Answer:

Try this Shellcheck-clean code:
  • for arg; do loops over the positional arguments ($1, $2, …)
  • continue 2 skips to the next iteration of the outer (for dir ...) loop.

If you have better answer, please add a comment about this, thank you!

bash
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How applied a multiple textures images or one image into a cube in qt 6 with qml?

22/03/2023

Resolved: More than one condition in CASE clause after renaming of column in SQL

22/03/2023

Resolved: Filter objects with random number keys in Es6

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.