0

I want to remove all files and dirs except for certain files whose names I want to specify. Is there any PHP script that can help me?

Here is an example:

Dir name:

/home/root/database/

files in "database" dir are:

  [include]
   [img]
  [file]
  index.php
  index.html
 .htaccess
 index.jpg
 index.png
  readme.txt
  video.mp4
  music.mp3

I want to remove all files except .htaccess and index.php and the [file] dir.

Is there any command that can save these files and remove the other files or any php code?

closed as off-topic by muru, Thomas, Eric Carvalho, user535733, Amith KK Jul 13 '18 at 14:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – muru, Thomas, Eric Carvalho, user535733, Amith KK
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Is PHP even capable of deleting files? What’s the reason you’re mentioning a PHP script? – dessert Jul 12 '18 at 22:20
  • 1
    Do you have command line access to this account? – Skaperen Jul 12 '18 at 22:50
3

Command line

First make a copy of all:

tar -cf /to/a/save/place/PHP_backup.tar *

where /to/a/save/place is any directory not in that path.

Now you can use the following command:

rm -rv !(.htaccess|index.php|file)

delete files from php

If you have to use php, you can take this funtion and modify it to leave the wanted files and dir.

  • 1
    Why not just tar -cf /ta/a/safe/place/backup.tar * in the first place? Extended pattern matching operators like !(…) need the extglob shell option enabled, which it may not be by default (test for that with bash -O|grep extglob) – run shopt -s extglob to enable it. – dessert Jul 12 '18 at 22:00
  • yes of course! ;) forgot – abu_bua Jul 12 '18 at 22:07
  • @dessert checked bash -O , is off , and it works too? – abu_bua Jul 12 '18 at 22:10
  • You misunderstood me, bash -O shows what is the default on your system, without reading .bashrc and the like, to show your current setting run shopt extglob (without -s). – dessert Jul 12 '18 at 22:11
  • 2
    @dessert you are right! every day bash surprise me with another feature. – abu_bua Jul 12 '18 at 22:13
2

the trash command from trash-cli is very handy for this type of thing. It's available in the universe repository and you can install it if you don't have it with the command sudo apt install trash-cli

Once installed you can simply issue the command

trash !(.htaccess|index.php|file) which tells it to trash everything not matching what is in the parenthesis. In this case .htaccess , index.php and the directory file.

Of course this requires extended pattern matching to be turned on with shopt -s extglob as mentioned by dessert in this comment

There really is no need to backup everything in a tar file with this approach as everything you trash will be in the trash bin and if you make a mistake you can recover with restore-trash and selecting the corresponding file or directory that you wish to restore.

If you must have PHP see unlink.

This answer was inspired in part by this one

Not the answer you're looking for? Browse other questions tagged or ask your own question.