Command-line Basics: Working with Directories

joshtronic

Directories create a hierarchy and add structure to your file system. Got too many files? Group them into directories. Still too many? Group them into sub-directories. Repeat until you have the level of organization you desire.

And please, don’t call them “folders”.

Working with directories is very similar to how we work with files with only a few differences to the commands we use.

Getting Started

To get things started, let’s create some files and directories to play around with. Adding files to /tmp will save you from any clean up since the files will vanish after a reboot, but you can add the following directories anywhere you’d like:

$ cd /tmp
$ mkdir -p reptiles/{alligator,crocodile,turtle}
$ touch reptiles/{alligator,crocodile,turtle}/README.md
$ cd reptiles

Nothing fancy, just created some nested directories and created some empty read me files in them. If you’re interesting in learning more about creating both files and directories, be sure to check out our other article: Command-line Basics: Creating Files and Directories

Copying Directories

Whoops, guess we forgot to add a snake directory, how could we forget about our slithery reptile friends?

To create a snake directory, identical to our alligator directory, we will use the cp command, but tell it to do a recursive copy by including the -r, -R or --recursive argument (yes, either of the three will work):

$ cp -r alligator snake

Moving / Renaming Directories

Let’s say we wanted to rename our crocodile directory to be the slightly more descriptive nile-crocodile. We can use the mv command, and we don’t even need any additional arguments:

$ mv crocodile nile-crocodile

Renaming or moving a directory is the same as doing a cp -r and then removing the original directory.

Removing Directories

Removing directories is a bit more complex depending on if the directory has any files or directories inside of it or not.

If the directory is completely empty, you can simply use rmdir which stands for, you guessed it, remove directory:

$ mkdir empty-directory
$ rmdir empty-directory

You can also use the -p flag that we used in the getting started section to remove an entire tree of empty directories:

$ mkdir -p one/two/three
$ rmdir -p one/two/three

These are great if you created a directory on accident or already know if the directory or directories are empty, but what if we wanted to delete our turtle directory from earlier?

The rmdir command would simply let us know that the turtle directory is not empty and exit.

Don’t fret, we aren’t stuck with this non-empty directory forever, we just need to change our approach. To remove non-empty directories, we can use the rm command.

Not so fast, using rm on a directory will also error, telling us that it cannot remove a directory. Fortunately, we can call upon a familiar series of arguments from earlier, -r, -R or --recursive which will tell rm to remove the directories as well as their contents recursively.

A bit of a warning though, using rm -r and it’s more forceful sibling, rm -rf can have disastrous consequences if you’re not careful. I highly recommend including an additional argument, -i or -I.

-i will prompt you for confirmation on every file that’s being deleted and -I will only prompt if removing more than 3 files or when you’re removing recursively.

Bonus points if you take a moment to alias rm to always include this safeguard:

$ alias rm='rm -i'
# or
$ alias rm='rm -I'

Where were we, oh yeah, removing the turtle directory and it’s contents:

$ rm -ri turtle

No more errors, and only a few prompts to contend with to keep us safe.

Reading Directories

All these directories are well and good, but how do we know what we have going on in there?

Instead of pulling up your favorite GUI file manager and clicking around a bunch of times, we can list files and more from the command-line.

To list the files and directories in your current working directory, you can simply use the ls command:

$ ls

There’s more to the ls command than that, but for the sake of this post, running it without any arguments is sufficient.

The ls command is great if you just want to see what files and directories are in the root of the current directory but this can only get you so far. From time to time, you may want to see a full hierarchy of files and directories and their associated nesting.

To print this information, you can issue the tree command:

$ tree
$ tree -a # include hidden files
$ tree -d # directories only

Which will print out something similar to the following:

.
├── alligator
│   └── README.md
├── nile-crocodile
│   └── README.md
└── snake
    └── README.md

Unlike ls, tree isn’t always installed by default on systems so you may need to fire up your favorite package manager and install it.

Where Am I?

If you ever find yourself lost on the command-line, you can always find out where you are at in the file system by issuing the pwd command.

pwd or print working directory does just that, it prints your current working directory and you’re off to the races.

If you’d like to know more about jumping around on the file system you can check out Command-line Basics: Changing Directories

Missing the GUI?

If you’re still not convinced about working with directories on the command-line, there is a more visual alternative that is sure to trigger some nostalgic feels depending on your age.

It’s called GNU Midnight Commander, or mc and it provides a virtual shell for your file system. Similar to tree it may not be installed, but it’s pretty widely available under the mc package name.

  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#
#native_cta#