Skip to content Skip to sidebar Skip to footer

Delete All But One Directory Linux

Delete All But One Directory Linux. (data.txt) here is the command to delete all files in your folder, except files data.txt and project.txt. It didn't succeed, but erased the old one, so i said 'no sweat, i have a copy' only to find that a midnight cron task had erased it with every file more than a week old in /tmp!

How to delete locked files or folder in Linux
How to delete locked files or folder in Linux from www.qasimtricks.com

In the example below, you delete every directory starting with dir. Delete the folder, then recreate it. Rmdir directory for deleting multiple empty directories, you can use the following format of the command.

Delete The Folder, Then Recreate It.


To delete everything in a directory run: In linux, one of the easiest commands for deleting the empty directory, you can use “rmdir” command. Let us look at one additional example, the command below will wipe out all files excluding.gz,.odt, and.jpg files in the current directory:

When Using Regular Expansions, First List The Files With The Ls Command So That You Can See What Files Will Be Deleted Before Running The Rm Command.


Follow this answer to receive notifications. To remove a folder whose name has space, make sure to always use quotes like: Rmdir directory for deleting multiple empty directories, you can use the following format of the command.

Will Remove All Regular Files (Recursively, Including Hidden Ones) Except File.txt.


The procedure to remove all files from a directory: Using bash globignore variable to remove all files except specific ones. Take a look at the following link:

If The Directory Is Not In Your Current Path You Have To Provide It Along With The Directory Name As We Did In Case Of Removing Files.


Type y and press enter to confirm. As you see in the above example, the test folder contains same type of files i.e.txt files. Replace it with the filename of your choice.

(File.txt), You Must Enable Extglob:


Providing more than one directory name deletes all of the specified empty directories. Remove=$ (echo $dirname | sed ‘s/—/ /g’) echo “removing file or directory $remove”. Now you can see where i’m going with all of this.

Post a Comment for "Delete All But One Directory Linux"