Introduction to the Command Line – Part 2

Read Introduction to the Command Line – Part 1, if you are absolutely clueless when you see these words: “Linux”, “cd”, “GUI”, and “CLI”.

By now, you should already know what the shell is, what Terminal is, and how you can use a few shortcuts to access files in your computer. Let’s move on to the next step, Manipulating files and using Wildcards.

 

Manipulating files and using Wildcards

 

Let’s explore some of the most frequently used Linux commands in file manipulation. These are my favorite five:

  • cp – copy files and directories
  • mv – move or rename files and directories
  • rm – remove files and directories (*exercise caution here – Linux does not have an undelete command. Once you delete something with rm, it’s gone.)
  • mkdir – create directories
  • ls – list files

Let’s explore Wildcards. In board games, a wildcard is “a playing card that can have any value, suit, color, or other property in a game at the discretion of the player holding it.” In computing, a wildcard (like a shortcut), is a symbol specified by you, that will match any character or sequence of characters in a search.

Wildcards allow you to specify and select filenames quickly.

  • * – matches zero or more non-space character, basically means “everything”
  • ? – matches exactly one non-space character, basically means “fill in something to replace one question mark”
  • characters – matches any character that is a member of the set characters. Some examples include [:alnum:], [:digit:], and [:lower:].
  • !characters – matches any character that is not a member of the set characters

For example, ca* will match any word starting with ca, such as cat, calibrate, catatonic and so on. On the other hand, ca? will only match three-letter words starting with ca, such as cat, cap, and so on. Note that two or more wildcards can be used simultaneously.

 

Using Commands

 

Commands can exist in 4 different forms – 1) executable programs written in C, C++, or scripting languages such as Ruby, 2) shell built-in commands (like the cd command), 3) shell functions, and 4) aliases that we define ourselves built from other existing commands. Listed below are four common ones.

  • type – Display information about command type
  • which – Locate a command
  • help – Display reference page for shell builtin
  • man – Display an on-line command reference

When all else fails, always use “help”.  Most programs will support the --help option. If you’re on a Mac, sometimes typing the man command (which stands for “manual) helps too, since it gives a more detailed description of each command. The only downside is, you’ll need to pair it with an argument for it to work.

Once you type the man command into your Terminal, you’ll see this prompt, “What manual page do you want?”. It is specifying that you type in an argument so it can show you a specific manual. Let’s try exploring the basic man mkdir command.

Once you press enter, you’ll see details such as “Name”, “Synopsis” and “Description”. Lots of good information there.

To exit the man command, simply type q.

 

Using Commands with Wildcards

 

Lastly, you can always combine commands with wildcards. This will make your life a whole lot easier. Some commonly used ones:

  • ls * – list all folders and all files within those folders
  • ls ? – list all folders and all files within those folders that have one character in thier names
  • ls a* – list all folders and all files within those folders that start with the letter “a”

 

Some Cool Shortcuts

 

I like include using the tab key as a smart “autocompletion” too. For example, if I were to type cd Desk, and then the tab key, I’d get cd Desktop/ immediately.

Another really cool shortcut is using the up key. This allows you to access the most recently typed commands. The more you press up, the further back you go.

Lastly, let’s say you want to go back “home” and you’re currently on cd Desktop/ with all its files listed. What you need to do to get back to your home directory is to use the tilde key. Simply type cd ~ to access your home directory. Then type ls to list all the files and folders in your home directory, just to double-check you are indeed home.

 

Some Commands to Avoid

 

Just as how you can perform magic with the command line, you’d also be exposing yourself to a lot of danger if you don’t use the commands properly.

If you ever come across these commands (say, if a more senior programmer tells you to run them), be sure to cross-check with someone else what these commands do, and if you really need to use them.

  • sudo rm -rf / – “sudo” allows you to temporarily become the superuser of your machine.  “rm” means to remove. This command basically gives your computer permission to start at the top of the file structure, and delete every single file on the computer without stopping, rendering it empty.
  • :(){ :|:& };: – This looks like nothing, but it’s actually one of the most dangerous commands you’ll ever see. It’s known as a fork bomb, rabbit virus, or wabbit. It’s basically a denial-of-service attack (DOS) where it will continue to fork out and replicate itself, asking for more system resources from your computer. The computer tries to process the code continuously and gets slowed down, exhausted, and finally crashes. Fortunately, you can reboot your computer to set things right again.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *