I made a symbolic link with the following commmand:
How to remove directory with subdirectories and files? I'm trying to remove several directories which contains sun-dirs and files inside. I used the command rm -r. More UNIX and Linux Forum Topics You Might Find Helpful: Append string to all the files inside a directory excluding subdirectories and.zip files.
I want to remove it now but my rm fails:
How can I remove my symbolic link?(Ubuntu 8.10, bash)
Peter Smit
Peter SmitPeter Smit
5,10110 gold badges35 silver badges42 bronze badges
6 Answers
Remove the trailing slash:
With prompt:
$ rm test5
Without prompt:
$ rm -f test5
1,3231 gold badge11 silver badges8 bronze badges
Try
(without the training slash).
rm test5
(without the training slash).
The slash indicates that 'test5' is a direactory whereas it's actually a file linking to a directory.
pelmspelms
6,6549 gold badges48 silver badges72 bronze badges
You can run removing the trailing slash:
This will remove the file (i.e. the symlink).
Alternatively you may use unlink:
Again you must omit the trailing slash since you are attempting to unlink the symlink not the directory.
bwDraco
37.4k37 gold badges142 silver badges179 bronze badges
CallumCallum
1,0562 gold badges9 silver badges15 bronze badges
Sometimes if you use autocomplete to name the link that you want to delete you may not see a trailing slash but it's 'half there' and that invisible slash still gives the delete error when trying to remove that link.
So in that case type out character by character the link to be deleted as 'test5' as eg.
bertieb
rm test5
.
5,78211 gold badges29 silver badges44 bronze badges
user872812user872812
The issue in the OP is the trailing
/
, so test5/
throws an error but test5
works.
I prefer to use
unlink
rather than rm
as my intention is clearer and there is no chance of mistakenly removing the real directory instead of the link. Make sure that there is no trailing /
after the directory name, e.g.:
isapirisapir
I feel silly asking, but have you tried
rm -r
?Since it's a symbolic link it shouldn't delete the target.
Edit: Just tried it, it's correct
Edit 2: rmdir says in its first line of the man page it deletes empty directories. I would think because it's a link it had the directory bit checked on its file properties, but because rmdir doesn't suspect that being the case it spits errors. Just use rm -r
bobbybobby
Not the answer you're looking for? Browse other questions tagged linuxbashsymbolic-link or ask your own question.
I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:
then I'd like to be left with just
I assume that I need to use
rm -r -i
Am I correct?
lesmana
13.8k6 gold badges35 silver badges42 bronze badges
atomh33lsatomh33ls
3595 gold badges10 silver badges25 bronze badges
5 Answers
No that would give you 'missing operand' since you didn't specify anything. Putting a '*' would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The 'mindepth 1' will exclude '.' from the results, the 'maxdepth 1' will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few 'innocent' warnings.
fede.evolfede.evol
1,6581 gold badge7 silver badges5 bronze badges
I found this one somewhere:
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
MartinMartin
Use
That avoids interactive mode an deletes only directories in your local directory.
WeSeeWeSee
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
Matthew WilliamsMatthew Williams
4,0598 gold badges21 silver badges36 bronze badges
linux.cnflinux.cnf