Americas

  • United States
sandra_henrystocker
Unix Dweeb

Creating and removing directory structures on Linux

How-To
Jan 03, 20235 mins
Linux

A few clever Linux commands can make it a lot easier to create, view and remove complex directory structures.

hand reaching out to folders and files
Credit: Thinkstock

Managing directories on Linux is easy, but the process gets more complex when you need to create, empty or remove large, complex directory structures. This post will take you from the most basic commands to some fairly complex ones that can help make the process easier.

mkdir

The mkdir command can create a single directory like this:

$ mkdir newdir

It can also create a complex directory and subdirectory structure with a command like the one below. The -p argument tells the command to create the base directory if it doesn’t already exist.

Each group of directory names that appears in the command shown – like {1,2,3} and {docs,script} – will result in a series of subdirectories being created at that level.

$  mkdir -p newdir/{1,2,3}/{docs,scripts}

You can add as many levels to the directory structure as you need simply by adding additional /{dir1,dir2} type specifications to the mkdir command. If you add the -v (verbose) option, the command will display each directory as it is created.

$ mkdir -pv newdir/{1,2,3}/{docs,scripts}
mkdir: created directory 'newdir'
mkdir: created directory 'newdir/1'
mkdir: created directory 'newdir/1/docs'
mkdir: created directory 'newdir/1/scripts'
mkdir: created directory 'newdir/2'
mkdir: created directory 'newdir/2/docs'
mkdir: created directory 'newdir/2/scripts'
mkdir: created directory 'newdir/3'
mkdir: created directory 'newdir/3/docs'
mkdir: created directory 'newdir/3/scripts'

You can view the directory structure after it is set up using a recursive ls command like this that displays the directories at each level:

$ ls -lR newdir
newdir:
total 12
drwxr-xr-x. 4 shs shs 4096 Dec 29 11:12 1
drwxr-xr-x. 4 shs shs 4096 Dec 29 11:12 2
drwxr-xr-x. 4 shs shs 4096 Dec 29 11:12 3

newdir/1:
total 8
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 docs
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 scripts

newdir/1/docs:
total 0

newdir/1/scripts:
total 0

newdir/2:
total 8
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 docs
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 scripts

newdir/2/docs:
total 0

newdir/2/scripts:
total 0

newdir/3:
total 8
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 docs
drwxr-xr-x. 2 shs shs 4096 Dec 29 11:12 scripts

newdir/3/docs:
total 0

newdir/3/scripts:
total 0

tree

Another and potentially more gratifying way to view a newly created directory structure is to use the tree command that displays the structure of a complex directory in a very easy to understand way like this:

$ tree newdir
newdir
├── 1
│   ├── docs
│   └── scripts
├── 2
│   ├── docs
│   └── scripts
└── 3
    ├── docs
    └── scripts

9 directories, 0 files

Once you add files to your new directories, the tree command will show those as well.

$ tree newdir/1
newdir/1
├── docs
│   └── notes   
└── scripts

2 directories, 1 file

touch

The touch command can be used to create a new file or to update the timestamp on an existing file. To add an empty file to the newdir/2/docs directory, you could use a command like this:

$ touch newdir/2/docs/notes

Using /dev/null

To empty a file, you can redirect /dev/null to it using a command like that shown below. In the sequence of commands, we list the current file, empty it using /dev/null and then list it again to verify that it has been emptied.

$ ls -l newdir/1/docs/notes
-rw-r--r--. 1 shs shs 139 Dec 29 11:42 newdir/1/docs/notes
$ cat /dev/null > newdir/1/docs/notes
$ ls -l newdir/1/docs/notes
-rw-r--r--. 1 shs shs 0 Dec 29 11:43 newdir/1/docs/notes

find

You can use the find command to recursively locate and display files or directories. In the command below, we use find to display the files in the newdir directory structure. Adding -ls provides long listings with file details.

$ find newdir -type f
newdir/2/docs/notes
newdir/1/docs/notes
$ find newdir -type f -ls
  5782884      0 -rw-r--r--   1 shs      shs             0 Dec 29 11:46 newdir/2/docs/notes
  5782883      0 -rw-r--r--   1 shs      shs             0 Dec 29 11:43 newdir/1/docs/notes

Moving into a directory and back again

If you use the cd command to move into a directory anywhere in your new directory structure, you can get back to the directory from which you typed the cd command using the cd – command as shown here:

$ pwd
/home/shs/newdir
$ cd 1/docs
$ ls -l
total 0
-rw-r--r--. 1 shs shs 0 Dec 29 11:43 notes
$ cd -
/home/shs/newdir

The cd – command always takes you back to where you were located in the file system before you typed the prior cd command.

Removing a complex directory structure

To remove a complex directory structure, assuming you have proper permissions to do so, simply use a recursive rm command like that shown below.

$ rm -rf newdir
$ ls -ld newdir
ls: cannot access 'newdir': No such file or directory

The directory and all of its contents will be removed.

Using scripts

Creating a complex directory structure, making changes and eventually removing it can be easy if you use the right commands. If you need to create or replace a specific structure frequently, doing that with a script can save you a lot of trouble. Here’s an example script that will create a specific structure or replace it as needed.

#!/bin/bash

if [ $# == 0 ]
then
  echo -n "dirname> "
  read dirname
else
  dirname=$1
fi

if [ ! -d $dirname ]; then
  mkdir -p $dirname/{users,files,notes}
  tree $dirname
else
  echo "$dirname exists. Remove it first? [y,n]"
  read ans
  if [ $ans == 'y' ]
    then
      rm -rf $dirname
      mkdir -p $dirname/{users,files,notes}
      tree $dirname
    else
      echo ok -- $dirname left as is
  fi
fi

Wrap-up

Creating and removing complex directory structures can be a lot easier with a few well-crafted commands or a clever script.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author