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 85 times
-2
-
1Is `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](http://unix.stackexchange.com/posts/223978/edit) your question to address this, rather than responding in comments.) – roaima Aug 18 '15 at 14:07
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
- 47,463
- 11
- 118
- 144
-
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. The `rmdir` 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
- 1
- 3
-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,398
- 2
- 13
- 21
-
3Be very careful with using `ls` in a script! Have a read of this discussion: [link](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – FelixJN Aug 18 '15 at 14:00
-
1