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

Farman Ali

I want to delete all files and directories from a specific directory except for one directory. For the same operation I had run this command

# rm -R -df !(dir_name_not_to_delete)

It works in Ubuntu but it gives an error in CentOS; i.e.,

-bash: !: event not found  " what is the reason for this error"

What do I have to use in CentOS to get this to work? Is there any alternative of '!' this sign in Centos?

John1024

First, !(dir_name_not_to_delete) is an extended glob. For it to work, you need to have the extended globs option turned on. Run:

shopt -s extglob

Secondly, the specific error message that you see, -bash: !: event not found, means that you also have history expansion turned on. If you don't want history expansion, it it safest to turn it off:

set +H

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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?

From Dev

Linux move files from specific directory one directory up and delete empty folder

From Dev

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

From Dev

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

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 extract a specifically named folder from a recursive directory, delete the others?

From Dev

How to extract a specifically named folder from a recursive directory, delete the others?

From Dev

how delete All files except one in folder with php?

From Dev

Use find to find certain directory and delete all files in it except one directory

From Dev

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

From Dev

How do I remove all files from wtihin a certain directory except for a child directory of that directory?

From Dev

Delete all files from within a directory that contain a specific word

From Dev

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

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

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

From Dev

How to delete directory named ".."

From Dev

Delete specific files in directory

From Dev

Copy Files from one directory to another while creating a new folder for each file, named after the file

From Dev

Delete all files in a directory

From Dev

Delete all folders inside a folder except one with specific name

From Dev

Delete a string from all files in a directory

From Dev

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

From Dev

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

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

How to delete a specific set of ??.png files in a directory?

Related Related

  1. 1

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

  2. 2

    How to delete all files in a directory except some?

  3. 3

    Linux move files from specific directory one directory up and delete empty folder

  4. 4

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

  5. 5

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

  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 extract a specifically named folder from a recursive directory, delete the others?

  10. 10

    How to extract a specifically named folder from a recursive directory, delete the others?

  11. 11

    how delete All files except one in folder with php?

  12. 12

    Use find to find certain directory and delete all files in it except one directory

  13. 13

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

  14. 14

    How do I remove all files from wtihin a certain directory except for a child directory of that directory?

  15. 15

    Delete all files from within a directory that contain a specific word

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    How to delete directory named ".."

  21. 21

    Delete specific files in directory

  22. 22

    Copy Files from one directory to another while creating a new folder for each file, named after the file

  23. 23

    Delete all files in a directory

  24. 24

    Delete all folders inside a folder except one with specific name

  25. 25

    Delete a string from all files in a directory

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

    How to delete a specific set of ??.png files in a directory?

HotTag

Archive