Shell script: How to delete all files in a directory except ones listed in a file?

manshou

I have a directory (~/temp/) that contains many files and sub directories, and in some directories, they may contain other files and sub directories. Also, in the directory (~/temp/), it contains a special txt file with name kept.txt, it list some direct files and sub directories that contained in ~/temp/, now i want to delete all other files and directories under ~/temp/ that are not listed in the kept.txt file, how to do this with a shell command, the simpler the better.

e.g.

The directory likes as below:

$ tree temp/ -F
temp/
 ├── a/
 ├── b/
 ├── c/
 │   ├── f2.txt
 │   └── z/
 ├── f1.txt
 └── kept.txt

The content of kept.txt is:

$ more kept.txt
b
kept.txt

For this case:

  1. i want to delete a/, c/ and f1.txt. For c/, the directory itself and all sub content (files and directories) will be deleted.
  2. In kept.txt, the format is one item (file or directory) per line.
anubhava

Using extglob you can do this:

cd temp
shopt -s extglob

rm -rf !($(printf "%s|" $(<kept.txt)))

printf "%s|" $(<kept.txt) will provide output as b|kept.txt| and !(...) is an extended glob pattern to negate the match.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash Script delete files in directory except those listed in different files

From Dev

How to delete all files except some specific file (for many folders in a directory)? via bash script

From Dev

How to delete all files in a directory except some?

From Dev

How can I delete all the files in the current directory except one file?

From Dev

In Linux terminal, how to delete all files in a directory except one or two

From Dev

How to delete all files in a directory except some globs using Puppet?

From Dev

How to delete all directories inside a directory except for files, (safely)?

From Dev

How to delete all files inside a directory except some extensions?

From Dev

How to remove all files in a directory except one file in Solaris

From Dev

How to delete files listed in a text file

From Dev

How to delete files listed in a text file

From Dev

Unix delete with find: Delete all files that are listed in a file

From Dev

BAT file: Delete all files in directory older than 5 days except one file

From Dev

How to delete all files and directory except one named directory from a specific folder in centos

From Dev

bash shell script to delete directory only if there are no files

From Dev

Delete all files except files with the extension pdf in a directory

From Dev

Delete all files except files with the extension pdf in a directory

From Dev

How can I delete a particular if statement in shell script file and do the same for all files of all folders inside a single folder

From Dev

How to delete all files except directories?

From Dev

How can I backup directory and files including all sub directory and files using shell script?

From Dev

(Batch) How to recursively delete all files/folders in a directory except those with a leading .?

From Dev

Git ignore all files except particular ones

From Dev

How to recursivly delete all JPG files, but keep the ones containing "sample"

From Dev

How to delete all directories in a directory older than 2 weeks except the latest one that match a file pattern?

From Dev

Delete all files except the newest 3 in bash script

From Dev

Delete all file except some in bash, without cd to the directory?

From Dev

Batch file to delete all folders in a directory except the newest folder

From Dev

How to delete all files that match a pattern (or older than..) except the newest file that matches the pattern?

From Dev

How to find all files except a specified file

Related Related

  1. 1

    Bash Script delete files in directory except those listed in different files

  2. 2

    How to delete all files except some specific file (for many folders in a directory)? via bash script

  3. 3

    How to delete all files in a directory except some?

  4. 4

    How can I delete all the files in the current directory except one file?

  5. 5

    In Linux terminal, how to delete all files in a directory except one or two

  6. 6

    How to delete all files in a directory except some globs using Puppet?

  7. 7

    How to delete all directories inside a directory except for files, (safely)?

  8. 8

    How to delete all files inside a directory except some extensions?

  9. 9

    How to remove all files in a directory except one file in Solaris

  10. 10

    How to delete files listed in a text file

  11. 11

    How to delete files listed in a text file

  12. 12

    Unix delete with find: Delete all files that are listed in a file

  13. 13

    BAT file: Delete all files in directory older than 5 days except one file

  14. 14

    How to delete all files and directory except one named directory from a specific folder in centos

  15. 15

    bash shell script to delete directory only if there are no files

  16. 16

    Delete all files except files with the extension pdf in a directory

  17. 17

    Delete all files except files with the extension pdf in a directory

  18. 18

    How can I delete a particular if statement in shell script file and do the same for all files of all folders inside a single folder

  19. 19

    How to delete all files except directories?

  20. 20

    How can I backup directory and files including all sub directory and files using shell script?

  21. 21

    (Batch) How to recursively delete all files/folders in a directory except those with a leading .?

  22. 22

    Git ignore all files except particular ones

  23. 23

    How to recursivly delete all JPG files, but keep the ones containing "sample"

  24. 24

    How to delete all directories in a directory older than 2 weeks except the latest one that match a file pattern?

  25. 25

    Delete all files except the newest 3 in bash script

  26. 26

    Delete all file except some in bash, without cd to the directory?

  27. 27

    Batch file to delete all folders in a directory except the newest folder

  28. 28

    How to delete all files that match a pattern (or older than..) except the newest file that matches the pattern?

  29. 29

    How to find all files except a specified file

HotTag

Archive