Remove old files in a directory except files present in an exception file
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.
I know the following command works for one exception file but i need to check a list of exception files
find . ! -name 'file.txt' -type f -exec rm -f +
shell-script find ksh
add a comment |
up vote
3
down vote
favorite
I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.
I know the following command works for one exception file but i need to check a list of exception files
find . ! -name 'file.txt' -type f -exec rm -f +
shell-script find ksh
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.
I know the following command works for one exception file but i need to check a list of exception files
find . ! -name 'file.txt' -type f -exec rm -f +
shell-script find ksh
I am writing a shell script to delete old files (older than 60 days) in a directory except few files and these file names are maintained in an exception file present in another directory.
I know the following command works for one exception file but i need to check a list of exception files
find . ! -name 'file.txt' -type f -exec rm -f +
shell-script find ksh
shell-script find ksh
edited Nov 20 at 22:52
Rui F Ribeiro
38.2k1475126
38.2k1475126
asked Jun 28 '16 at 16:32
Balaji
183
183
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17
add a comment |
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:
find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f
Replace rm -f
with ls -1
for a dry run first. Put paths exactly as they are printed by find
in exceptions.txt
.
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line.fgrep
filters out the files inexceptions.txt
.xargs ... rm -f
removes the files.
– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
add a comment |
up vote
1
down vote
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +
file.txt
will have list of files to exclude in find
command.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:
find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f
Replace rm -f
with ls -1
for a dry run first. Put paths exactly as they are printed by find
in exceptions.txt
.
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line.fgrep
filters out the files inexceptions.txt
.xargs ... rm -f
removes the files.
– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
add a comment |
up vote
2
down vote
accepted
Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:
find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f
Replace rm -f
with ls -1
for a dry run first. Put paths exactly as they are printed by find
in exceptions.txt
.
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line.fgrep
filters out the files inexceptions.txt
.xargs ... rm -f
removes the files.
– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:
find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f
Replace rm -f
with ls -1
for a dry run first. Put paths exactly as they are printed by find
in exceptions.txt
.
Assuming your files have sane names (i.e. they don't have embedded newlines), something like this should work:
find . -mtime +60 | fgrep -v -x -f exceptions.txt | xargs -d 'n' rm -f
Replace rm -f
with ls -1
for a dry run first. Put paths exactly as they are printed by find
in exceptions.txt
.
answered Jun 28 '16 at 16:39
Satō Katsura
10.9k11534
10.9k11534
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line.fgrep
filters out the files inexceptions.txt
.xargs ... rm -f
removes the files.
– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
add a comment |
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line.fgrep
filters out the files inexceptions.txt
.xargs ... rm -f
removes the files.
– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
Thanks for your input. Is fgrep taking output from find command and eliminating the files present in exceptions.txt and sending the result to rm command ?
– Balaji
Jun 28 '16 at 17:17
find
prints a list of files, one per line. fgrep
filters out the files in exceptions.txt
. xargs ... rm -f
removes the files.– Satō Katsura
Jun 28 '16 at 17:20
find
prints a list of files, one per line. fgrep
filters out the files in exceptions.txt
. xargs ... rm -f
removes the files.– Satō Katsura
Jun 28 '16 at 17:20
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
Works perfectly !! Thanks for your help !!
– Balaji
Jun 28 '16 at 17:58
add a comment |
up vote
1
down vote
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +
file.txt
will have list of files to exclude in find
command.
add a comment |
up vote
1
down vote
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +
file.txt
will have list of files to exclude in find
command.
add a comment |
up vote
1
down vote
up vote
1
down vote
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +
file.txt
will have list of files to exclude in find
command.
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
find . -name "*.txt" $(printf "! -name %s " $(cat file.txt)) -mtime +60 -exec rm -f +
file.txt
will have list of files to exclude in find
command.
edited Jun 28 '16 at 16:50
answered Jun 28 '16 at 16:41
Rahul
8,82412842
8,82412842
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f292643%2fremove-old-files-in-a-directory-except-files-present-in-an-exception-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
related: Delete all files in a directory whose name do not match a line in a file list
– don_crissti
Jun 28 '16 at 17:17