How do I learn to use the Unix command line?

Q: At my new job, I can only use Unix. I have never used it before and have no idea what any commands are! Where do I start?

As sadistic as this may sound, I LOVE that people are getting forced into learning Unix. Diversity is a good thing and learning about other operating systems beyond Windows is a way of diversifying yourself. As scary as Unix may be to a beginner, it’s not as difficult as it seems. The way you learn to use Unix is to use Unix. Only through extended use will you actually learn to use it. There are a few things I should cover before I get into the commands. Things like directory structure and some basic terminology. After I layout some Unix basics, I’ll outline a few necessary commands for the most basic of Unix use, then I’ll cover a more advanced topic that isn’t necessary to master as a beginner.

Unix is a class of operating systems (OS). Many companies and individuals have published their own versions of the Unix OS. It started as a single OS in 1969, but has since been rewritten and derived to create the many OSes that exist today. Mac OS X, Linux, Sun Solaris, and FreeBSD are all Unix based operating systems. The following points will not make complete sense until coupled with the commands and the command output. Read them but don’t worry if you feel overwhelmed at first.

  • Every Unix user has a ‘home‘ directory. My username is PHR and my home directory is then /home/PHR. Any time a user logs in, they start in their home directory.
  • The path is the full directory location. If I have a documents directory in my home directory, its path is /home/PHR/documents.
  • There is one username that is the supreme being of the operating system. His name is root, but the job title might be ‘System Administrator‘ (sysadmin). Root users tend to have egos because they are omnipotent when it comes to the OS. Yes, this is a delusion of grandeur. Root can create anything and destroy EVERYTHING, including your existence on the OS.
  • Unless you are root (or have special permission), you should only create, manipulate, or install files in your home directory. This is the courteous thing to do. It’s rude to break another users experience because you changed a file that more than you use.
  • Unix is a multiuser environment by design. Users are members of one or more groups.
  • Permissions control what files a user can Read, Write, or Execute. Permissions are broken down by three classifications:
  • Owner - the owner or creator of the file.
  • Group - the group the file belongs to.
  • World - everyone in the world (OS).
  • The user interface in which a user types commands is called the command line or terminal.
  • The graphical user interface in which a user can use a mouse to click icons and menus is called the windows manager(wm). There are many different windows managers and everyone has a favorite. Unless you’re ready for a battle(can support your claim), do not insult another person’s favorite wm in their presence. If you do, you may be labeled a troll. After one becomes familiar with Unix, they become privy to the secret wars that exist behind the scenes.
  • Unix does NOT like spaces in file names or directory names. If you’re using spaces, you must use the escape character before the space.
  • I believe that Linux has the best community support system of any operating system in existence. It’s HUGE. If you need help search the many, many, many, many forums that exist to help new users of Linux or other Unix OSes. You may not want to ask root for help, they tend to be a little snooty :-P
  • With a few basic commands you can do plenty. I will present a list of commands with an explanation of them. The commands themselves are bold. Commands have options or switches that are included after the command itself and are designated by dashes (-). To execute a command, just type it into the command line and hit enter.

    • pwd : display the Present Working Directory, the directory that you are currently navigating . The full path is displayed.
    • ls : list the files in the current directory.
    • ls -a : display all files, including hidden and system files, in the present directory.
    • ls -l : long file listing. Show the permissions, owner, group, and last touched date.
    • ls [directory name] : Display the files in the named directory.
  • cd : change directory. The command that makes navigation possible. It’s very versatile.
    • cd : change to your home directory, no matter what directory you’re currently in.
    • cd .. : change to the parent directory. (.. always means the parent directory of the PWD).
    • cd ~ : change to the home directory.  (~ always means the current user’s home directory).
  • mkdir directory : create a directory named directory.
  • date : display the current date and time.
  • history : display the last few commands entered.
  • passwd : change your password.
  • cat : display the entire contents of a file (text only).
  • less : display the contents of a file one screen at a time.
  • echo “my text” : output my text to the screen
  • man command : display the manual for the specified command
  •  Some slightly more advanced usage
    The Unix command has a wonderful concept built in called ‘redirection’. You can use the output of one command as the input of another command, or stick the output into a text file.

    • Pipe, | - The pipe, |, is used to direct the output of one command into the input of the next. Let us assume that in our current directory there is a single file called hamlet. In hamlet, the entire Shakespeare play is written in plain text. We could very easily type less hamlet and the contents would be displayed. We can do the exact same thing by typing ls | less into the command line.  The file name, hamlet, would have gone to the screen but we redirected it into the input of the less command. Had there been more than one file, this would have turned out differently, but that’s for you to experiment with. Piping works with all commands that have output and input. The pipe is Shift+\
    • > - The > is used to redirect the output of a command into a file. echo “This is text for a file” > test will create a text file called test that contains the text This is text for a file. ls > listing will create a file called listing that contains the directory listing of the current directory. ls > test will overwrite the first file named test with a new file that contains the directory listing of the current directory.
    • >> - The >> is used to concatenate the output of a command to the end of a file. First we make a file of the directory listing: ls > concatExample Then we add a description to the end of the file to let anyone who reads the file what it is: echo “this is the contents of my home directory” >> concatExample
    • - The output of the command inside these back quotes is replaced with the output of the command itself. The back quotes share a key with the ~. This redirect is the most confusing in my opinion. Most times the pipe can serve the purpose just fine, but there are cases where it’s useful. Take the previous example. What if we weren’t in our home directory? We COULD type out what directory we were in or we could make Unix do it for us. `pwd` is replaced with the directory path, the output of the pwd command.
    • ls > concatExample
    • echo “this is the contents of `pwd`” >> concatExample

    Phew. There’s a lot to digest there. Remember that you won’t get all of this at once. This was a very quick and dirty guide. Unix is big and complex, but it’s easy to learn. Practice makes perfect and experimentation is also key. Try new combinations of commands. See what you can do with the operating system. Use this reference manual and try different commands. For more tutorials, here’s a Google search.

  • How many cores are in a Core and Core 2?
  • PCHR
  • Cingular: The Resolution
  • 0 Responses to “How do I learn to use the Unix command line?”


    1. No Comments

    Leave a Reply

    You must login to post a comment.




     

    Creative Commons License

    Archives

     

    Locations of visitors to this page