Skip to content

Instantly share code, notes, and snippets.

@ginsterbusch
Created January 7, 2015 18:54
Show Gist options
  • Save ginsterbusch/0863d3f0e3d48718031e to your computer and use it in GitHub Desktop.
Save ginsterbusch/0863d3f0e3d48718031e to your computer and use it in GitHub Desktop.
Find and delete all hidden files in $dir. PHP CLI file. Change the shebang accordingly.
#!/usr/bin/php
<?php
/**
* Find and delete all hidden files in $dir
* Usage: remove-hidden-files.php OR remove-hidden-files.php /path/to/parse/
*
* Without path parameter, the current path is being used.
*
* (C)ommitted 2015 by @author Fabian Wolf (@link http://usability-idealist.de/)
*
* @license GNU GPL v3
*/
// find all hidden files in $dir (eg. the current one)
$dir = './';
if( !empty($argv[1]) ) { // first parameter
if( !file_exists( $argv[1] ) ) {
echo "\n" . $argv[1] . " does not exist.\n";
exit(1);
} elseif( file_exists( $argv[1] ) && !is_dir( $argv[1] ) ) {
echo "\n" . $argv[1] . " is not a directory.\n";
exit(2);
}
$dir = '' . $argv[1];
}
$result = shell_exec( 'find ' . $dir . ' -type f -iname ".*" -print' );
if( !empty( $result ) ) {
echo "\nFound the following files in [$dir]:\n" . $result;
$hidden_files = explode("\n", trim($result) );
} else {
echo "\nFound no hidden files in [$dir].\n";
}
if( !empty( $hidden_files ) ) {
// now, lets remove em
foreach( $hidden_files as $strFileName ) {
echo "\nRemoving $strFileName ...";
if( unlink( $strFileName ) != false ) {
echo " done.\n";
} else {
echo " failed!\n";
}
}
}
@amine-y
Copy link

amine-y commented Dec 21, 2020

Thank you for the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment