Wednesday, November 14, 2007

Linux folders and directories recursive removal

I have been noticing with all my old linux blog entries that most other simple commands are being covered and taken from a single blog entry. These linux commands have not been pinpointed by definition and usage samples, why such commands has been issued and bypassed from detailed explanations from this linux blog site.

How to delete multiple directories and folders recursively?
How to create multiple folders simultaneously?
How to delete multiple folders interactively and recursively?

Here are few basic samples of simple commands that has not been a blog entry topic here.

Read the DISCLAIMER, be careful on using these rm commands.

Let us start working into the linux temporary folder /tmp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cd /tmp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create multiple directories or folders simultaneously
The long way:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cd /tmp
# mkdir tmp1
# mkdir tmp1/tmp2
# mkdir tmp1/tmp2/tmp2B
# mkdir tmp1/tmp3
# mkdir tmp1/tmp3/tmp3B
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The short way:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# mkdir tmp1 tmp1/tmp2 tmp1/tmp3 tmp1/tmp2/tmp2B tmp1/tmp3/tmp3B
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Interactive rm deletions do screen prompts before proceeding to any rm command executions like shown below.

In Fedora and RedHat, by default, rm command prompts before any proceeding to rm executions.

Delete multiple folders recursively and interactively.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cd /tmp
# mkdir tmp1 tmp1/tmp2 tmp1/tmp3 tmp1/tmp2/tmp2B tmp1/tmp3/tmp3B
# rm -ri tmp1
rm: descend into directory `tmp1'? y
rm: descend into directory `tmp1/tmp3'? y
rm: remove directory `tmp1/tmp3/tmp3B'? y
rm: remove directory `tmp1/tmp3'? y
rm: descend into directory `tmp1/tmp2'? y
rm: remove directory `tmp1/tmp2/tmp2B'? y
rm: remove directory `tmp1/tmp2'? y
rm: remove directory `tmp1'? y
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see, rm descends to folders recursively and prompts everytime a folder deletion is about to occur.

Force delete multiple folders recursively without any folder deletion prompts and confirmations.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# mkdir tmp1 tmp1/tmp2 tmp1/tmp3 tmp1/tmp2/tmp2B tmp1/tmp3/tmp3B
# rm -rf tmp1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alternatively, here's another long approach of deleting folders and subdirectories recursively
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cd /tmp
# mkdir tmp1 tmp1/tmp2 tmp1/tmp3 tmp1/tmp2/tmp2B tmp1/tmp3/tmp3B
# find /tmp/tmp1/ -type d -exec rm -rf {} \;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another basic way to delete multiple but separate folders
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# rm -rf folder folder2 oldfolder*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


You might be interested with the CONSIDERATION details from the bottom page too.

HTH.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy