Some UNIX Commands and Tools

navigating the file system

  1. Use pwd to find the absolute path of the current directory.
  2. Use cd to change directories. Go to /etc and do a directory listing (ls, -r, -a, -l, -ld).
  3. Use file to determine the types of the files in etc with names beginning with p.
  4. Try ls .., ls ../.., ls / and ls ~.
  5. Change to parent, root and home (without specifying absolute paths).
  6. From your home directory, try file etc/system. Why doesn't it work?

displaying files

  1. Try cat /etc/system. Now try less /etc/system. Use the arrows to scroll. Search for patterns (/pattern and ?pattern).
  2. Try more /etc/system. What is the difference between less and more? Check the man page (see below)!

getting help

  1. Try man less to answer the previous question.
  2. Guess how you can find out about formatting options for the man pages.

pipelines and redirection

  1. Try man less | less.
  2. Try ls /etc/system | less.
  3. Now try ls /etc/system > sys.
  4. Read input to a program from a file:
  5. Try a.out > average and then a.out >> average.
  6. Something like this might be convenient: ls /etc > etc
  7. How does pipelining differ from redirection?

manipulating files and directories

  1. Point a browser to the class folder and download the folder UnixLab to your desktop.
  2. Copy from UnixLab to your home directory: cp movies.txt ~
  3. What happens with cp Days/Monday ~/today followed by Days/Friday ~/today?
  4. Try cp HTML/* ~ and then cp -i HTML/* ~.
  5. Copy multiple files: cp /etc/hosts /etc/passwd .
  6. Delete a file: rm Logs/templog.
  7. Copy UnixLab to your home directory. It's tedious unless you do it recursively (-r).
  8. Make a directory (mkdir Temp) and then remove it (rmdir Temp). You cannot remove UnixLab from your desktop in this way — instead try removing all files recursively.

globbing

  1. ls log?
  2. ls log[3-7]
  3. ls log[!3-7]
  4. ls l*
  5. ls *day
  6. ls *o*
  7. cp log? ..
  8. cp log[5-7] ~
  9. ls ???day
  10. ls log [2-468]
  11. ls log [!2-469]
  12. Can you figure out how to copy the file log* to your home directory? A trick to disable metacharacters is needed.
  13. Brace expansion: mkdir {Work,Holi}days and mv {thurs,fri}day Holidays

comparing files

  1. Compare HTML/i.html and i2.html using diff.
  2. Useful options: -i, -b and -y.

linking files

  1. Change to UnixLab
  2. Create a (hard) link: ln loglist lglst.
  3. Display the link count: ls -l loglist.
  4. Move loglist to your home directory and then try cat lglst.
  5. Does rm lglst remove the link or the file?
  6. Make another link (ln loglist lglst) and remove loglist. Now cat lglst. Explain?
  7. Now let's make a symbolic (soft) link: ln -s loglist lglst.
  8. See any difference between a hard and soft link? Try removing loglist and then cat lglst.
  9. Which is like a Windows shortcut — hard or soft link?
  10. What's the point of a soft link, anyway?

archiving and compressing files

  1. Change to UnixLab/Logs.
  2. Archive some files: tar -cvf logs.tar log[3-8].
  3. Compress: gzip logs.tar.
  4. Unzip: gunzip logs.tar (.gz not needed).
  5. Compress: bzip2 logs.tar log[3-8].
  6. Unzip: bunzip2 logs.tar.bz2.

permissions

  1. Change to UnixLab/Logs.
  2. Do a long listing and look at the permissions of each file.
  3. Do a long listing after each of the following:
  4. Now go back to UnixLab and type chmod u-w Logs.
  5. Change to Logs and do a directory listing. Display a file. Try to delete it.
  6. Go back to UnixLab and type chmod 660 Logs. Now try to list files in Logs or change to it.
  7. You can find the default file creation mask by typing umask (and try it with the -S option).
  8. Now type: umask 135. Touch a file and do a long listing.
  9. Umask is built into bash: help umask.

searching for files

  1. From your home directory, type: find . -name loglist.
  2. Try find UnixLab -type d.
  3. Make a hard link to UnixLab/Days/friday. Use li -i to get the inode number for this file. Now you can find the file and link using the -inum option with the inode number.
  4. cat UnixLab/Days/monday and touch UnixLab/Days/tuesday. Then:
  5. We can combine search criteria:
  6. Find actions:

grepping

  1. grep img i.html
  2. grep -c img i.html
  3. grep 197[5-9] movies.txt
  4. grep ^One movies.txt (pattern at beginning of line)
  5. grep ^S movies.txt | grep 's ('
  6. grep x movies.txt | wc -l

customizing bash

  1. set -o will display current shell options. Turn off the history option with set +o history. Now type some commands and then try to use the up-arrow to see the history. Turn the option back on with set -o history.
  2. touch history.txt and then set -o noclobber. Now try date > history.txt.
  3. Try set -o ignoreeof and then try to exit the shell with ctrl-d.
  4. Variables are named holders of text strings: OpSys="This is Solaris.". Compare: echo OpSys and echo $OpSys.
  5. Variable substitution: Welcome="Welcome to UNIX. $OpSys" and then echo $Welcome.
  6. Bash maintains its own variables, such as HOME, PWD and HOSTNAME (echo them).
  7. The PS1 variable is the shell's prompt string. Try PS1="Well? ".
  8. Bash has built-in escaped characters that provide status for interactive shells. Try PS1="I am \u on \H in \w. It's \A: ".
  9. Echo $PATH We can add the location of the C compiler to the path: PATH=$PATH:/bin.
  10. You can see all bash variables: just type set. Even better: set | less or set | grep HIST.
  11. An alias creates an alternative name for a command line. Try alias rm='rm -i' and test it. And: alias hm='cd /home/dcoles'.
  12. Type alias for a list of all aliases.
  13. The system-wide script /etc/profile runs when at login time.
  14. When you start a shell (not a login shell), bash reads and executes commands in ~/.bashrc (if that file exists). Edit the file to set the prompt string and add the C compiler to the path.

not really an introduction to scripting

  • Save this in a file, and run it with the dot command:
       #!/bin/bash
       echo "Hello, $USER"
       echo "Listing files in current directory ($PWD)"
       ls -l
    
  • One more:
       #!/bin/sh
       echo "Enter your name : "
       read name
       echo "Hello, $name "
    
    That's not much — entire books have been written on Bash scripting — but for now I'm just trying to give you a ghost of a trace of a peek at the subject.