The mkdir and rmdir commands

Discussion in 'Beginner Tutorials' started by Rob, Jul 9, 2013.

  1. Rob

    Rob Administrator Staff Member

    Oct 27, 2011
    Likes Received:
    'mkdir' is the command for making directories. 'mkdir' may be familiar to MS-DOS users out there. As you have noticed, the people who wrote these programs tried to give them names that described what they do more or less, not as long as 'makemeadirectoryplease' and not too cryptic like 'xr77b'.

    Using the 'mkdir' command

    To create the directory 'my_friends' that we talked about in the last lesson, you would type:

    mkdir my_friends
    There are no whistles or buzzers. If you'd like some sort of acknowledgment, you could type

    mkdir --verbose my_friends
    and it will tell you that you created the directory.

    If you type ls -l You'll see it there along with information about it.

    Now you know how to use 'mkdir'. You can even use it to create a directory called 'my_enemies' if you're into that sort of thing.

    The 'rmdir' command

    'rmdir' is the opposite of 'mkdir'- it gets rid of directories. It should be pointed out that in order to use it, the directory has to be empty. If you copied or moved anything to 'my_friends' and you typed

    rmdir my_friends/
    Linux would politely tell you that you can't do that.

    So, you have to use your 'rm' command on the files first to remove them or use 'mv' to get them into another directory. Then you're free to use 'rmdir'
  2. plotozoidz

    plotozoidz New Member

    Mar 21, 2014
    Likes Received:
    Personally, i don't use 'rmdir' as i find it rather tedious to operate due to it only being able to delete empty directories. If you want to remove the directory and its contents, one has the long task of 'cd' into directory and then 'rm' or 'mv' all files, then 'cd' out, before one can use 'rmdir'.

    I prefer to use 'rm -ir' on directories. actually i use rm -ir on files too, cause it works on individual files too.

    Lets say you have a directory called directory1 with file1.txt and file2.txt inside, and you want to delete everything:

    rm -ir directory1
    You will get prompted a number of times for a y (yes) or n (no) key press something like what follows:

    rm: descend into directory `directory1'? y
    rm: remove regular empty file `directory1/file2.txt'? y
    rm: remove regular empty file `directory1/file1.txt'? y
    rm: remove directory `directory1'? y

    In this example i pressed y to every prompt, as we wanted to delete everything.

    I would strongly advise that you use 'rm -ir' not just 'rm -r'
    -i means interactive. It provides the y/n prompts. use 'rm -r' at your peril as it will delete everything without telling you or giving you a second chance!
    Last edited: Mar 24, 2014
  3. dtse9

    dtse9 Member

    Jun 19, 2015
    Likes Received:
    is rm -f -r directory1 better?

Share This Page