Linux / Software Development

Dr. Torq: ‘rm -r *’ and Other Linux Command Line Gotchas

16 Nov 2019 7:00am, by
Editor’s Note: Contact Rob “drtorq” Reilly for consultation, speaking appearances and commissioned projects at [email protected] or 407-718-3274.

It has been said that the Linux command line is cryptic and difficult to learn. But I’d call it comprehensive. Like anything else, you need to know a few fundamentals to get started putting the system to work doing important computing jobs. The Linux command line is very powerful. Of course, with that power comes user responsibility and the potential to do harm. Knowing what to do is important, but so is knowing maybe what not to do.

Don’t let it intimidate you. That’s just the way it is.

In my Embrace the Linux Command Line post a few months back, we discussed a few commands you could type into a terminal window on a Raspberry Pi. Linux being ubiquitous, you can do the same thing on a notebook, desktop or server. For that matter, the command-line works great if you remotely use it without a window manager and windows over a network connection.

Today we’ll explore a couple of areas you should be aware of that will help you make good command-line decisions.

The Infamous rm Command

You’ve probably heard of the rm command. rm removes files from the system. It is an extremely powerful command and should be treated with appropriate respect.

Like other Linux programs, rm has a handful of command-line options. Type “rm –help” to get a listing.

rm –help command output

A particularly notorious rm usage is with the -R option and an “*” for the file names. It has caused much heartache among system administrators.

The –R option orders rm to travel down into the sub-directories. The * means match ALL files that it finds. You can easily get rid of a lot of files, very quickly, this way. It’s particularly unsettling if you execute the command at the top-level (/) directory as the root user, which has god-like powers on a Linux system.

Suppose you were a busy, somewhat inexperienced system administrator doing some clean up on a Sun Unix machine. That was me, logged in as root, many moons ago. I was flipping around through the directories getting rid of old log files and cleaning out unused files when it happened.

Somehow I had issued a cd / to take me up to the topmost directory. I must have been distracted because I thought I was still in the log directory. About three seconds after typing “rm -R *” and hitting enter, I realized my error. It was too late.

About a minute later I heard grumbling from the two users I knew were on the machine. It was a small ComputerVision — Sun Microsystems Unix-based CAD graphics system, so the real damage came from the tool designers losing the changes they had made to their CAD models that morning. It happens. Most every Unix or Linux system administrator has made a similar snafu.

Needless to say, I had to spend the remainder of the day and that night rebuilding the system from tape and backups. I had backups of the user data from the previous day so we lost maybe four hours of production work. The tool designers also had to do other work while I repaired their system. More lost production time, now for two people. I was in the doghouse for a little while after that one.

Fortunately, as a regular user, you aren’t likely to wipe out your machine with the rm command. You can remove your own files inadvertently, though, so always make sure you know where you are in your directory structure. And, use the “rm -R *” combination sparingly with rm.

It might be instructional to burn a micro-SD with Raspbian Linux and put it in a Raspberry Pi. Then execute “rm -R *” at the / directory as root, to see what happens. You can always just re-burn the micro-SD to get a new system back again. Might as well get the worst over with and out of the way.

Disk Full Scenario

A typical time when you might run into trouble with rm is when your disk is or almost full and you have to scramble to clear out some files. Sometimes you’ll get a message in a terminal saying you are out of disk space. The machine might not crash when you fill up the disk. So, you probably have a little time to get rid of some files and reclaim some space.

We can find out the status of the disk with the df command.

df command output

I like to use the -h command-line option so I can see the file space sizes in mega and gigabytes.

There is a 750GB disk in my antique ASUS Linux notebook. You could just use the whole disk for your computer, although I like to divide it into “partitions.” Partitions break up the disk space and associate each section with a /dev name. That way I can have the Linux operating system and applications in one partition and my user files in another partition. When I periodically upgrade to a new version of Linux, I just reformat the root (/) directory and copy a new Linux image on top of it. This has no effect on user files out in the /home directory. I associate the /dev/sda1 partition with the / directory. The /home directory is tied to the /dev/sda5 partition.

Looking at the df listing above we can see that the root file system (/) is /dev/sda1 and is sized at 92GB. I’ve only used 35GB for the operating system and applications, which is about 40% of the total partition space. There are about 53 GB still available for more system and application files.

Further down in the listing is the /dev/sda5 partition, associated with the /home directory. If you see the use percentage up above 95%, it might be time to delete unused files or move some of them into another partition or onto an external archive drive.

Copying And Moving Files

The cp command copies files from one place to another. mv moves files around.

Using both are pretty straightforward. Here are a couple of examples.

linux-notebook% cp rob.txt test.txt

linux-notebook% mv rob.txt test.txt

The first one copies one file to a new name. The second one renames the rob.txt to test.txt.

You can also copy and move directories. Say you have a directory named rob1. Under rob1 you have a file named test.txt. You can copy the directory and the file to a new directory. You’ll have to use the -r option to go down into the directory and copy the file located there as well.

linux-notebook% cp -r rob1 rob2

Now there will be a directory named rob1 with the test.txt file, as well as, a directory named rob2 with a test.txt file.

What happens if we issue an “mv rob1 rob2″?

Linux will move the rob1 directory right over to rob2. That removes the rob1 directory. Oh, and it overwrites whatever is in rob2, if it already exists. Linux won’t ask you if you want to overwrite the directory. It will just go ahead and do what you tell it to do.

We kind of get back to that user responsibility thing with mv, so again, be cognizant of where you are in the directory tree and what you want to actually do with your files.

Wrap Up

The Linux command-line is a powerful tool for all types of computing jobs. It is a little dangerous because there aren’t a lot of safeguards built-in to keep you out of trouble.

Knowing about some of the pitfalls will help you make good command-line decisions. Mistakes will be made and it’s just part of the learning process. Stick with it and move forward. Sometimes it takes a little while. Linux is very comprehensive.

Feature image by TeeFarm from Pixabay.