Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

linux remove all files except specific ones

# Basic syntax:
find /directory/to/search -type f -name "pattern_to_match" ! -name "pattern_to_ignore" -delete

# Example usage:
touch file1 file2 file3

# This will delete all files except those containing "3"
find . -type f -name "*file*" ! -name "*3*" -delete

# Note, if I'm only deleting a moderate number of files, I prefer to use the
#	following which asks for confirmation before deleting:
find . -type f -name "*file*" ! -name "*3*" -ok rm -- {} ;
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #linux #remove #files #specific
ADD COMMENT
Topic
Name
4+8 =