Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible that if I have this:

/folder1/folder2/folder3
/folder1/folder2/file1.txt

Can I delete folder2 but keep its content?

So the result would look like:

/folder1/folder3
/folder1/file1.txt
share|improve this question
add comment

2 Answers 2

I think that you don't need to actually delete something... just move it away.

mv  /folder1/folder2/*  /folder1/
rmdir /folder1/folder2/

First command moves the contents to parent directory and the second removed the directory if it's empty. This won't move hidden/dot files. If you want also to move hidden files you would need:

mv  /folder1/folder2/{*,.*}  /folder1/
rmdir /folder1/folder2/

or use dotglob option in bash. If you don't have dot files it would lead to no matches found: dir1/dir2/.* in bash and the command would fail. Also you can ignore the:

mv: cannot move ‘folder1/folder2/..’ to ‘folder1/..’: Device or resource busy

errors since bash passes . and .. to mv.

share|improve this answer
add comment
cd /folder1/folder2/
mv * ../
cd ../

Now check the contents of the folder:

ls

Then use this command to delete the directory. It is completely safe since it will only delete empty directories:

rmdir folder2/
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.