Lets say I have the directory /Users/admin/Documents/Folder1/file1.txt
and would like to delete Folder1
without also deleting file1.txt
. In the end it should look like /Users/admin/Documents/file1.txt
.
Asked
Active
Viewed 86 times
-2
3 Answers
1
You have to do that in two steps:
mv /Users/admin/Documents/Folder1/file1.txt /Users/admin/Documents/file1.txt
rm -R /Users/admin/Documents/Folder1
With bash
you can do the following shorter version:
mv /Users/admin/Documents/{Folder1/,}file1.txt
rm -R /Users/admin/Documents/Folder1

chaos
- 48,171
-
1For a more automated way (and save directory removal), I'd suggest:
mv /dir/subdir/{.[^.]*,*,} ; rmdir /dir/subdir
- will print a negligible error if there is no.
-file, but otherwise get them all. Thermdir
is saver as the dir has to be empty. – FelixJN Aug 18 '15 at 14:35
1
What you can do is try to copy the .txt file to the documents directory. Then you can go ahead and delete the sub-directory. That would be 100X easier.

MesamH
- 11
-2
Originally I had suggested this:
cd /Users/admin/Documents/Folder1
mv $(ls -A) .. # the -A will find hidden items, but not return "." or ".."
cd ..
rmdir Folder1
But from the comments, I see that is not safe.

rexroni
- 1,508
-
3Be very careful with using
ls
in a script! Have a read of this discussion: link – FelixJN Aug 18 '15 at 14:00 -
1
file1
really a directory, as stated in your question? If it's a file, then does it represent a collection of files, or just itself? (Please edit or update your question to address this, rather than responding in comments.) – Chris Davies Aug 18 '15 at 14:07