Command Line Traps to Avoid

For now, we’ll add just one major trap when using the command line. Bash is very unforgiving of errors. If you want to remove all the files in the current directory and below, you can could do this (but don’t):

me@ubu:~/Desktop/examples$ rm -rf *

This tells the command to delete all files in the current directory, recursively, and force it (don’t ask). But what if you were not in the directory you thought you were in?

me@ubu:~/Desktop/examples$ rm ubuntu Sax.ogg

If you meant to delete the file "ubuntu Sax.ogg", you did not. Instead, you would have deleted a file named "ubuntu" and another named "Sax.ogg" if they both existed. That’s one reason we renamed it. The space is a delimiter. You could have done it like either of these:

me@ubu:~/Desktop/examples$ rm 'ubuntu Sax.ogg' me@ubu:~/Desktop/examples$ rm ubuntu⧵ Sax.ogg

The second line above uses a backslash as an escape character. It means, "treat the next character, a space, as a space character rather than a delimiter."

One of the very worst traps would be if you were running as the superuser (root) you could delete all the files on your hard drive! Do NOT do this:

root@ubu:~$ rm -rf / tmp*

You meant to delete everything in the /tmp directory, which might have been OK. But you made a mistake and put a space between the slash and the tmp*. So, as the superuser, you told bash to delete everything in all directories on your computer, without asking. And also any directory or file in the current directory beginning with the letters tmp. Big mistake!


Printed from — Command Line Traps to Avoid.