Results 1 to 4 of 4
Good afternoon everyone,
I know that if you want to remove a single folder with a space in its name, you can do
Code:
rm -rf "Test Folder"
I want ...
- 02-17-2009 #1Just Joined!
- Join Date
- Jan 2009
- Location
- Halifax, NS
- Posts
- 19
Removing folders with a space in name
Good afternoon everyone,
I know that if you want to remove a single folder with a space in its name, you can do
I want to remove all folders in a directory except specific ones. The problem is, it doesn't remove the folders with a space in their names.Code:rm -rf "Test Folder"
So if I have:
Folder1
Folder2
Test Folder
And I want to keep Folder1, but delete the others.
That will delete Folder2, but not Test Folder because the commandCode:ls | grep -v Folder1 | xargs rm -rf
doesn't work.Code:rm -rf Test Folder
Any help is appreciated and thanks for your time.
- 02-17-2009 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
As you mentioned in your first example the name has to be enclosed in quotes in order to access file or folder names with spaces in them. So you have to use quotes in the xargs command. You do this using the -I option as follows:
In this example the xxx will be replaced with the file/folder name. Since the name is now enclosed in quotes names with spaces in them will work.Code:ls | grep -v Folder1 | xargs -I xxx rm -rfv "xxx"
- 02-17-2009 #3Just Joined!
- Join Date
- Jan 2009
- Location
- Halifax, NS
- Posts
- 19
Thanks for the reply vsemaska,
I already found a solution though.
I decided to use sed to replace the space with a backslash space.
This thread can be locked/closed now.Code:ls | grep -v Folder1 | sed 's/ /\\ /' | xargs rm -rf
- 02-18-2009 #4Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Using just the command find, better solution maybe?
Code:find . -mindepth 1 -type d -path ./Folder1 -o -print0 | xargs -0 rm -rf


Reply With Quote