The Linux Command Line

Lab design credit goes to Jack Applin

Description

  • This lab will cover many common tasks using bash.

  • You will accumulate a file of commands and show that file to the TA for credit.

  • When you see a numbered question (101, 102, …​) do the command, then copy & paste that command into your file.

  • It might be easier to keep a separate window where you edit the file of commands.

  • If you don’t know how to use an editor, use gedit or nano. Otherwise, use whatever editor you prefer. Avoid notepad—that’s a Windows program.

  • To obtain more information about a command, use man commandname or help commandname (for built-in commands).

  • Once you can view a file with cat, use it to check your work. For example, if you’re making a copy of a file, don’t just have faith that you did it correctly. Use cat to verify your success.

Part 1 — Paths

  • A Linux path is a series of components, separated by slashes (/).

  • UPPER and lower case are different; FOOBAR is different than foobar.

  • If it starts with / or ~, it’s an absolute path.

  • Otherwise, it’s a relative path—relative to the current directory.

  • Your current directory is where you are. When you log in, you’re in your home directory.

  • The cd command changes your current directory. With no argument, it takes you back to your home directory.

  • The pwd commands tells you where you are.

  • Special directories:

    • ~ is your home directory.

    • ~bob is bob’s home directory.

    • ~/foo is the directory foo underneath your home directory.

    • . (dot) is the current directory—think “here”.

    • .. (dot-dot) is the parent directory—one level up from where you are.

      1. Use the cd command to go to info’s home directory.

      2. What command will tell you where you are? Do it.

      3. Use cd to go to the bin directory under cs165’s home directory.

      4. What command will tell you where you are? Do it.

      5. Use cd to go back to your home directory.

You are copy & pasting all your commands into another file, to show the TA, right? It would be a shame to do all this and not have anything to show to the TA.

Part 2 — Viewing Files

  1. Use cat to view ~info/printers.

  2. Use cat to view ~cs165/pub/hamlet.txt. Did you get all that?

  3. Use less to view ~cs165/pub/hamlet.txt. Use q to exit less.

  4. Use head to view the beginning of ~cs165/pub/hamlet.txt.

  5. Use tail to view the end of ~cs165/pub/hamlet.txt.

Part 3 — Listing files, making files & directories

A few file commands:

  • ls: list files

  • ls -l: a long listing of files in the current directory

  • ls -l filename: a long listing of some file somewhere

  • ls -l directory: a long listing of another directory

  • ls -l -d directory: a long listing of the directory itself, not what it contains.

  • ls -ld directory: same thing

  • cp old new: copy a file

  • mv old new: move (rename) a file

  • rm file: remove (delete) a file

  • mkdir directory: create a directory

  • rmdir directory: remove an empty directory

  • rm -r directory: remove an directory, and the files it contains, and all the directories underneath it, etc.
    Be careful!

  • Go to your home directory.

    1. Copy the file dwarfs from ~cs165/pub to your home directory.

    2. Rename it to seven.

    3. Make a new directory called Zulu.

    4. Move seven to that new directory.

    5. Generate a long listing of seven.

    6. Get rid of that new directory.

Part 4 — Commands

  • date: show the current date & time

  • diff file1 file2: show the differences between two files

  • grep pattern file: show lines that match a pattern

  • wc file: count lines, words, and characters

    1. Copy the file dwarfs from ~cs165/pub to your home directory.

    2. How many lines are in dwarfs? Use a command do do the counting. Do not just cat the file and count it yourself.

    3. Use your favorite editor to alter the names of some of the dwarfs in your copy of dwarfs.

    4. Use diff to show the changes you’ve made.

    5. Use grep to find the dwarfs whose names contain a “u”.

Part 5 — Redirection

  • There are several ways to re-route the output of a command.

  • > sends the output to a file, overwriting any previous contents.

  • >> appends the output to a file, adding to any previous contents.

  • | sends the output to another program.

    1. Run the date command, sending the output to the file now.

    2. Use cat to add the file ~cs165/pub/dwarfs to the end of now.

    3. Use grep to show the three words in ~cs165/pub/common-words.txt that contain “xy”, and then send that output to sort to put them in alphabetical order.

Part 6 — Filename Patterns

  • You can use filename wildcards, or filename patterns, to select certain files.

  • Don’t confuse these patterns with regular expressions, which are used by grep.

  • A few filename pattern characters:

    • ?: matches any single character

    • *: matches anything of any length

    • [aq%]: The letter a, the letter q, or the percent sign.

    • [3-9]: Any digit 3 through 9, inclusive.

  • A few examples:

    • *: all files

    • a*b: any file whose name begins with a and ends with b.

    • zulu[aeiou]: any file whose name is with zulu followed immediately by a vowel, which ends the name: zulue, zulua, zuluo, but not zuluooo or zuluax.

  • Change your current directory to ~cs165/pub.

    1. List all the files whose names contain a dot.

    2. Copy all the files with eleven-letter names to your home directory. Do not do the counting yourself. This is a job for a computer. Imagine a directory with hundreds of files in it.

Part 7 — File Permissions

  • Each file has an owner and a group.

  • Most of you are in the group under, for undergrads. We might have a grad or two.

  • Each file has nine permission bits. Consider this, where % is my prompt:

% ls -l ~cs165/pub/dwarfs
-rw-r--r-- 1 cs165 class 45 Jan  2  2018 /s/bach/a/class/cs165/pub/dwarfs
  • The initial - indicates that this is a plain file.

  • The next three bits are for the owner (cs165, in this case). rw- means that the owner can read, write, but not execute this file.

  • The next three bits are for the group (class, in this case). r-- means that other users in this group can read, but not write or execute this file.

  • The next three bits say the same thing for everybody else (like you).

    1. Copy the dwarfs file to your home directory.

    2. In your home directory, execute chmod ug=rw dwarfs to give yourself (the user) and others in your group (other students) read & write access to this file.

    3. Verify those permissions using ls.