Some UNIX Commands and Tools
navigating the file system
- Use pwd to find the absolute path of the current directory.
- Use cd to change directories. Go to /etc and do a directory listing (ls, -r, -a, -l, -ld).
- Use file to determine the types of the files in etc with names beginning with
p.
- Try ls .., ls ../.., ls / and ls ~.
- Change to parent, root and home (without specifying absolute paths).
- From your home directory, try file etc/system. Why doesn't it work?
displaying files
- Try cat /etc/system. Now try less /etc/system. Use the arrows to scroll. Search for patterns (/pattern and
?pattern).
- Try more /etc/system. What is the difference between less and
more? Check the man page (see below)!
getting help
- Try man less to answer the previous question.
- Guess how you can find out about formatting options for the man pages.
pipelines and redirection
- Try man less | less.
- Try ls /etc/system | less.
- Now try ls /etc/system > sys.
- Read input to a program from a file:
- Write a C program that prompts the user for two numbers and displays the average.
- Create a text file input.dat with two numbers.
- Now run the program with input coming from the file: a.out < input.dat.
- Try a.out > average and then a.out >> average.
- Something like this might be convenient: ls /etc > etc
- How does pipelining differ from redirection?
manipulating files and directories
- Point a browser to the class folder and download the folder UnixLab to your desktop.
- Copy from UnixLab to your home directory: cp movies.txt ~
- What happens with cp Days/Monday ~/today followed by Days/Friday ~/today?
- Try cp HTML/* ~ and then cp -i HTML/* ~.
- Copy multiple files: cp /etc/hosts /etc/passwd .
- Delete a file: rm Logs/templog.
- Copy UnixLab to your home directory. It's tedious unless you do it recursively (-r).
- 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
- ls log?
- ls log[3-7]
- ls log[!3-7]
- ls l*
- ls *day
- ls *o*
- cp log? ..
- cp log[5-7] ~
- ls ???day
- ls log [2-468]
- ls log [!2-469]
- Can you figure out how to copy the file log* to your home directory? A trick to disable metacharacters is needed.
- Brace expansion: mkdir {Work,Holi}days and mv {thurs,fri}day Holidays
comparing files
- Compare HTML/i.html and i2.html using diff.
- Useful options: -i, -b and -y.
linking files
- Change to UnixLab
- Create a (hard) link: ln loglist lglst.
- Display the link count: ls -l loglist.
- Move loglist to your home directory and then try cat lglst.
- Does rm lglst remove the link or the file?
- Make another link (ln loglist lglst) and remove loglist. Now
cat lglst. Explain?
- Now let's make a symbolic (soft) link: ln -s loglist lglst.
- See any difference between a hard and soft link? Try removing loglist and then
cat lglst.
- Which is like a Windows shortcut — hard or soft link?
- What's the point of a soft link, anyway?
archiving and compressing files
- Change to UnixLab/Logs.
- Archive some files: tar -cvf logs.tar log[3-8].
- Compress: gzip logs.tar.
- Unzip: gunzip logs.tar (.gz not needed).
- Compress: bzip2 logs.tar log[3-8].
- Unzip: bunzip2 logs.tar.bz2.
permissions
- Change to UnixLab/Logs.
- Do a long listing and look at the permissions of each file.
- Do a long listing after each of the following:
- chmod u-w log1
- chmod g+w log1
- chmod o+w log1
- chmod u=rwx,go=r log 1
- chmod 751 log[1-5]
- Now go back to UnixLab and type chmod u-w Logs.
- Change to Logs and do a directory listing. Display a file. Try to delete it.
- Go back to UnixLab and type chmod 660 Logs. Now try to list files in
Logs or change to it.
- You can find the default file creation mask by typing umask (and try it with the
-S option).
- Now type: umask 135. Touch a file and do a long listing.
- Umask is built into bash: help umask.
searching for files
- From your home directory, type: find . -name loglist.
- Try find UnixLab -type d.
- 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.
- cat UnixLab/Days/monday and touch UnixLab/Days/tuesday. Then:
- find UnixLab -amin -2 (accessed less then 2 minutes ago). [Does not work in Solaris]
- find UnixLab -mmin -2 (modified less then 2 minutes ago). [Does not work in Solaris]
- find UnixLab/Days -mtime +3 (modified more than 3 days ago).
- We can combine search criteria:
- Touch two files in UnixLab/Days and then try find UnixLab -amin -10 -type f.
- find UnixLab -name log[2-5] -or -name '*\*'.
- Find actions:
- find UnixLab -name ???day -ls.
- find UnixLab -amin -20 -exec cp {} . ';'. (The braces are a placeholder for the matching pathname.)
- find UnixLab -amin -20 -ok cp {} . ';'.
- find UnixLab -name ???day -exec chmod u-w {} . ';'
grepping
- grep img i.html
- grep -c img i.html
- grep 197[5-9] movies.txt
- grep ^One movies.txt (pattern at beginning of line)
- grep ^S movies.txt | grep 's ('
- grep x movies.txt | wc -l
customizing bash
- 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.
- touch history.txt and then set -o noclobber. Now try date > history.txt.
- Try set -o ignoreeof and then try to exit the shell with ctrl-d.
- Variables are named holders of text strings: OpSys="This is Solaris.". Compare: echo OpSys and echo $OpSys.
- Variable substitution: Welcome="Welcome to UNIX. $OpSys" and then echo $Welcome.
- Bash maintains its own variables, such as HOME, PWD and HOSTNAME (echo them).
- The PS1 variable is the shell's prompt string. Try PS1="Well? ".
- Bash has built-in escaped characters that provide status for interactive shells. Try PS1="I am \u on \H in \w. It's \A: ".
- Echo $PATH We can add the location of the C compiler to the path: PATH=$PATH:/bin.
- You can see all bash variables: just type set. Even better: set | less or set | grep HIST.
- An alias creates an alternative name for a command line. Try alias rm='rm -i' and test it. And: alias hm='cd /home/dcoles'.
- Type alias for a list of all aliases.
- The system-wide script /etc/profile runs when at login time.
- 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.