Quantcast
Channel: Remove all files except some from a directory - Stack Overflow
Viewing all articles
Browse latest Browse all 21

Answer by Leonardo Hermoso for Remove all files except some from a directory

$
0
0

You can do this with two command sequences.First define an array with the name of the files you do not want to exclude:

files=( backup.tar.gz script.php database.sql info.txt )

After that, loop through all files in the directory you want to exclude, checking if the filename is in the array you don't want to exclude; if its not then delete the file.

for file in *; do  if [[ ! " ${files[@]} " ~= "$file"  ]];then    rm "$file"  fidone

Viewing all articles
Browse latest Browse all 21

Trending Articles