2009-06-17

Bash Tutorial: Part 1

I know a lot of people get scared any time the command line comes up when using a computer. Really, the command line shouldn't be scary, and learning how to use the command line in any operating system will only make you better at using your computer. As such, I figured I would do a couple posts as an introduction into the Linux shell I use most: the Bourne-again shell (Bash). Let's get started.

Moving around the shell
  • pwd
    Print working directory. When you first open a shell, it starts you in your home directory, usually /home/[username], also abbreviated ~. If you're ever not sure what directory you're currently working in, the pwd command will tell you:

    $ pwd
    /home/[username]
    $

  • ls
    List files. If you want to see what files and folders are in a directory, ls is the command to do it. If you just want to see the contents of the current directory, just use:

    $ ls

    In Linux, files whose name begin with . are hidden. To list the contents of the current directory and show hidden files, use:

    $ ls -a

    If you want to see more information about the files, such as who has permissions to read, run, and execute files; the owning user and group; the file size; and the last date modified, just use:

    $ ls -l

    The ls command does not simply restrict you to looking at files in your current working directory; you can look inside any directory you want. To see the long listing of all files in the /usr/bin directory, just use:

    $ ls -al /usr/bin

  • cd
    Change directories. Now that we've covered how to see your current working directory and listing files within directories, the next place to go is to change directories. In order to change directories to /etc/X11, just do:

    $ cd /etc/X11

    We can also use .. to move up a level:

    $ pwd
    /lib/modules/2.6.26-2-686/kernel/net/ipv6
    $ cd ..
    $ pwd
    /lib/modules/2.6.26-2-686/kernel/net
    $ cd ../..
    $ pwd
    /lib/modules/2.6.26-2-686
    $ cd ../2.6.26-1-686
    $ pwd
    /lib/modules/2.6.26-1-686

Working with files
  • cp
    Copy. The basic syntax for the cp command is:

    $ cp [source file] [destination file]

    For example, if you wanted to make a backup copy of your .bashrc file before making changes:

    $ cp .bashrc .bashrc-backup

    This is a good for copying single files, but if you want to make a copy of an entire directory, we can use the -r option to do a recursive copy:

    $ cp -r Documents Documents-backup

  • mv
    Move. The mv command works similar to cp:

    $ mv [source file] [destination file or directory]

    If you want to simply rename your .bashrc-backup file to fix edits made to your .bashrc file:

    $ mv .bashrc-backup .bashrc

    If you want to move a file into a different directory:

    $ mv .bahsrc-backup Backup/

    If you need to move or rename a directory, it works exactly the same with the mv command as moving files.

  • mkdir
    Make directory. If you want to create a new directory for all your digital pictures, just do:

    $ mkdir Pictures

    You can also create a any missing parent directories with the -p option:

    $ mkdir -p Pictures/Vacation/2009


  • rm
    Remove. If you want to delete the .bashrc-backup file, just do:

    $ rm .bashrc-backup

    If you want to delete a directory and all the files and subdirectories within it, you can use the -r recursive option:

    $ rm -r /Pictures/Vacation/2009

  • rmdir
    Remove directory. If you want to delete a directory, you can use the rmdir command. The rmdir command requires that the directory that you're trying to delete is empty:

    $ rmdir Pictures/Vacation/2009

    Similar to the recursive option that we've seen with other commands, we have the -p parents option to use with rmdir:

    $ rmdir -p Pictures/Vacation/2009

    The above behaves the same as the following:

    $ rmdir Pictures/Vacation/2009
    $ rmdir Pictures/Vacation
    $ rmdir Pictures

Learning more

We've covered a lot in this post, but there is still more that can be done with the above commands. Here are some resources that are available to learn more of the options available.
  • man
    Manual. The man command will bring up what is called the "man pages" for other commands. The man pages describe the syntax and available options for bash commands and programs. The syntax for man is as follows:

    $ man [command]

    So, if you wanted to see more information about the cp command:

    $ man cp

  • info
    Information. The info command is similar to the man command, but the info pages can be a little easier to learn from if you're not used to reading man pages. If you want to see the info page for the cp command:

    $ info cp

That does it for Part 1 of the introduction to Bash.

See you next time.

No comments:

Post a Comment