How to Delete_Remove a Directory in Linux.pdf

21 views 11 slides Aug 01, 2023
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

We’ll demonstrate how to remove a directory in Linux command line to delete files and directories.


Slide Content

HowtoDelete/RemoveaDirectoryinLinux?-
Hostbillo
Introduction
UnderstandingthecoreLinuxcommandsisessentialforservermanagement
becauseLinuxisawidelyusedoperatingsystemforhostingservices.Acrossall
Linuxindividualsandsystemadmins,understandinghowtooperatetheLinux
commandlineisanurgentrequirement.Itallowsyoutoperformbasic
maintenanceoperationslikemakingnewfiles,accessingcertainfolders,and
relocatingexistingones.
Inthispost,we'lldemonstratehowtoremoveadirectoryinLinuxcommand
linetodeletefilesanddirectories.
SystemRequirements

Beforebeginning,youmustbeawareofthefollowingpredetermined
requirements:
●ALinux-basedsystem.
●Asudo-privilegeduseraccount.
●thepossibilitytousethecommandlineandterminalwindow.
HowtoRemoveDirectoryinLinux?
Youcandeleteadirectoryfromthecommandlineaswellastheterminal
windowusingoneofthefollowingremoveadirectoryinLinuxcommands:
●Whenusingthermcommand,allfilesandsubdirectoriesinadirectory
getdeletedcompletely.
●Byusingthermdircommand,emptydirectoriesgetdeleted.
ThermandrmdircommandsdonotrelocatedirectoriestotheTrashdirectory;
instead,theypermanentlydestroythem.Thiswouldbevitaltoremember.It
followsthatexecutingthesecommandswillnotallowyoutorestoreadirectory
thatgetsdeleted.
DeleteaDirectoryinLinuxusingrmdirCommand
Linuxhasacommandcalledrmthatattemptstounderminedirectoriesandfiles.
Itadoptsthefollowingsyntax:
rm[options][Nameofafileordirectory]
Therearevariousrmcommandparameters,including:

●Removesalldirectoriesorfileswiththe-foption.
●Beforeuninstalling,the-ioptionasksforauthorization.
●Whendeletingoverthreefilesorperformingarecursivedeletion,the-I
optionpromptstheuseronce.
●The-roptionrecursivelydeletesfoldersandtheircontents.
●-d:Deletesemptyfolders.
●Averboseoutputgetsprovidedviathe-voption.
●—help:Makesthehelptextvisible.
●—version:Indicatesthecommandversion.
WhenattemptingtodeleteadirectoryinLinuxwiththermcommandwithno
parameters,thefollowingmessageappears:
Byaddingthe-dparametertothermcommand,youcandeleteanempty
directory:
Example:rm–d
Toremoveadirectorythatincludesfilesandsubdirectories,utilize-rflag.Ithas
manytextfilesintheDir1&Dir2subdirectories:
Incontrasttothe-vflag,whichproducesalistofeverystageoftheprocedure,
the-rflagdeletesthecompletedirectory,withsubdirectoriesandfiles:
Example:rm-r–v

Yougetpromptedtoconfirmtheremovalofthedirectorywhenusingthe-i
option.EnteraftertypingYtoverify.
Example:rm-d–i
Ifadirectoryiswrite-protected,itcannotgetdeletedautomatically.Withinthis
typeofdirectory,include:
Example:sudomkdir
Utilizethefollowingcommandstoknowhowtodeletethedirectory:
Example:rm-d

Toconfirmremoval,typeY.Usethe-fswitchorgivethecommandsudo-level
accesstoomittheconfirmation:
Example:rm-d-f
Example:sudorm-d
Byusingthecommandpromptwhetherotherfilesanddirectoriesarepresent
insidethewrite-protecteddirectory:
rm-rf<directoryname>
RemoveaDirectoryinLinuxusingthermCommand
AllemptydirectoriescangetdeletedwiththeLinuxrmdircommand.This
commandseemstohavethefollowingformat:
rmdir[options][directoryname]

ThesechoicesareavailablewiththermdircommandinLinux:
●Whentryingtodeleteanon-emptydirectory,theoption—ignorefailure
onnon-emptydoesn'tdisplayanerrormessage.
●-p:Erasesthehierarchy'sparentdirectorytogetherwiththeworking
directory.
●-v:Producesoutputthatisverbose.
●—help:Providesahelpmessage.
●—version:Showstheversionsofthecommand.
Anon-emptydirectorycausesanerrorwhenthermdircommandgetsusedon
it.Thereforeinthisinstance,theTestsubdirectorygetsfoundunderthe
Exampledirectory:
Addingthemintheoppositeorderofthehierarchywillallowyoutodelete
themusingthermdircommand.The-voption'soutputgivesanoutlineofeach
stageintheprocess:
rmdir-vExample/TestExample
The-pparameteralongwiththenameofthesubfoldermakesthisprocess
easier.Withthis,thesubdirectory'shierarchicalparentisalsoremoved:

rmdir-p-vExample/Test
Utilizingwildcards,youcanexecutethermdircommandtoeliminateseveral
directorieswithsimilartitles.Todelete,forreference,thedirectoriesExample1,
Example2,orExample3:
rmdir-vExample*

CautionaryMeasures(Discusstheimportanceoftakingcaution
whenremovingdirectoriesandExplainhowtoprevent
accidentalremovalofdirectories)
Duetothelackofarecyclebinaswellasatrashfolder,filesandfoldersinthe
Linuxsystemarepermanentlyerased.Linuxonlyallowsyoutorestore
accidentallydeletedfilesanddirectoriesfrombackups.
Asitonlyfunctionsforemptydirectories,usingrmdircommandinLinux
minimizestheseunpleasanteffects.Iftherearefilesinthefolder,itwillgive
youthefollowingerrorcode:
rmdir:failedtodelete‘Directory’:Foldernotempty
Thermdirfunctionhasthefollowingsyntax:
rmdir[option]DirectoryName
Toavoidusingacommandlineparameter,deleteitfromthesyntax.Don't
forgetthatthecommandlineconsidersthecase.Toavoidconfusion,enterthe
foldernameinuppercaseifthat'sthecase.
Forinstance,thefollowingcommandremovestheSimple-Directoryfolder:
rmdirSimple-Directory

Moreover,thermdircommandsupportvariousemptydirectories.OnLinux,
youcanremoveallfilesinadirectorybyincludingthermdirinthecommand
viaextraarguments.
rmdirDirectory1Directory2Directory3
Solet'slookatseveralparametersforthermdircommand.Asubdirectoryorits
parentdirectorycangetdeletedwiththefirstoption,-p.Usethermdir
commandinLinuxbelowifyourdirectorypath,forinstance,is
/Directory/SubDirectory:
rmdir-p/Directory/SubDirectory

OntheDirectorypath,theabovecommandwillstartbydeletingthe
SubDirectoryfolder.FollowingtheremovaloftheSubDirectory,theDirectory
folderwillgeterasedifitisempty.
The-vcommandpromptparameterspecifiestheverboseoption,andit
representsthenextchoice.Toconfirmtheremovalofthechosendirectory,this
parameterwillprintatext.Anexampleofthiscommandisbelow:
rmdir-vSimple-Directory
Itwillappearintheoutputmessage:
RMDIR:'Simple-Directory'removalofadirectory
Conclusion

UsingthermorrmdircommandsinLinuxcorrectlyisnecessarytodelete
filesanddirectorystructuresinLinux.Thedifferencebetweenthetwo
commandsisthatthermcommandremovesfilesornon-emptydirectories,but
thermdircommandremovesjustemptyfolders.
KeepinmindthatLinuxdoesnothavearecyclebin/trashfolder.Byutilizing
thecommandline,youcanpermanentlydeletedirectoriesandfiles.Becauseof
this,usecautionwhileusingthesecommandsormakeabackupbefore
removinganycontentonyourhostingserver.Aftergoingthroughthistutorial,
youwillbeabletousetheterminalwindowandperhapsthecommandlineto
deletedirectoriesinLinux.
Tags