UNIX

COMMANDS





The following is a list of frequently used (at least by me) Unix commands.
Most have many options that I have not shown, so check the man pages or a reference book.
I am not a guru, but I do use shell in my day to day work environment.
It can really make your life much easier if you learn the basics.
The descriptions given were made as simple and to the point as possible.
Reference for this list is UNIX IN A NUTSHELLby Daniel Gilly and the staff of O'Reilly & Associates. Inc.
NOTE: Please refer to the man pages or Unix In A Nutshell for more details.



Quick Reference


A C D E F G H I K L M N P R S T




awk
Back to Quick Reference
Awk is actually a complete programming language, however the most important thing awk does, as far as basic shell programming is concerned, is print columns. Prints the first column of the who command to the screen. who | awk '{print $1}' Assign the shell variable LIST, the names of everyone logged in. LIST=`who | awk '{print $1}'`
cat
Back to Quick Reference
Reads files and prints them on standard output (the screen). Reads standard input, (key board) if no files are specified. Can combine several files into a new file using >> to append file to an existing file. Example Reads myfile one line at a time and displays to the screen. cat myfile Reads myfile and appends it to the end of yourfile cat myfile >> yourfile Reads listfile one line at a time and places each line in $i. If listfile contained filenames, each file would be printed. for i in `cat listfile` do lp $i done
cd cd is actually a built in shell command, but for all intents and purpose we will call it a unix command. cd is used to change directory. This can be used in a shell script to move to a directory other than the current, then any commands in the script after the cd will take place in the directory you changed to. (see second example) Example Change directory to the /usr/bin directory. cd /usr/bin Shell script code fragment from a shell script that is exists, and is run form /u/doug/bin cd /u/datafiles mv data.dat data.xxx mv data.idx data.xxx cd /usr/accounting dbbuild accounting.sch cd /u/datafiles mv ... etc etc
chmod Change the (permissions) of one or more files. Only the owner of the file or root can change the permissions. Example Makes the shell script myfile executable and readable for every one but only the owner can write to it. chmod 755 myfile
clear Clears the terminal screen. Example Shell script fragment. Clears the screen as soon as the script starts. #!/bin/sh clear echo "" echo "How are you today $LOGNAME?" read ANSWER
compress Reduce the size of a file. The compressed file will have a .Z extension. Use uncompress to restore to original size. Example compress myfile uncompress myfile.Z
cp Copies myfile to yourfile, if yourfile already exists, it will be over written by myfile. If youfile is a directory then myfile will be copied into the directory. Example Copies myfile to yourfile. cp myfile yourfile Copies myfile to /usr/tmp cp myfile /usr/tmp
crontab Executes a command at a give time. Run crontab on your your cron file or specify a crontab file to add to the crontab directory. The cron file has 5 columns and a command to execute. The columns are from left to right: Minute Hour Day of month Month Day of week command To create a cron file; edit a file and call it your login name (like vi doug). In this file you put a line like the one in the Example. Use commas to separate multiple values, a - for a range and * for all. Once you have your file, type crontab file name to submit the file to the crontab directory. (Usually /usr/spool/cron/crontabs). Example Runs an accounting report to generate bills at 1:30 AM on the 1st and 16th of every month. 30 1 1,16 * * /u/accounting/bills
date
Back to Quick Reference
Displays current date and time. Has many formatting options, but I will show the option I used the most. Example Current date and time. date Appends the Julian date to the unload file. (Julian date is the day of the year; 001-365.) unload.4ge > unloadfile.`date +%j`
df Displays the number of free disk blocks and inodes available on all mounted file systems. Example Reports on just the file system you are currently on. df .
diff Compares two files line by line, word for word. Reports any differences to standard out.(screen) If file 1 has something not in file 2, the lines will be printed to the screen with a < before each line. If file 2 has differences, that text will have > before each line. Example Reports the line that differ between myfile and yourfile. diff myfile yourfile
du Dispalys disk usage in 512-byte blocks, used by each named directory and it's sub directories. (The default is the current directory.) Example Reports all usage from the root directory down. du /
echo
Back to Quick Reference
Prints whatever is between the quotes. Example Prints Hellow world! to the screen. echo "Hello world!"
egrep Search the given files for a pattern. egrep usually will run faster than grep or fgrep. egrep supports the | (logical or) in the pattern definition. Example Searches for the word text or postscript in all the files of the current directory. Prints the line containing the pattern and the name of the file to standard out (screen). egrep '(text|postscript)' *
expr This thing does all kind of stuff but lets look at it from a basic shell script point of view. expr allows you to arithmetic. Example Adds 1 to the variable counter. counter=30 counter=`expr $counter + 1` Increments a counter each time through a loop, and exit when the counter is set to 5. counter=0 while [ $counter -ne 5 ] do echo "Can I go yet?" sleep 1 counter=`expr $counter + 1` done echo "Ok you can go it's been 5 seconds"
file
Back to Quick Reference
Determines the type of file, i.e. text, Executable, etc. Example The output of file for the shell script myfile could be as follows file myfile myfile: English text
find find starts at the given directory and descends the directory tree in search of the file specified. Example Finds the file myfile starting at the root directory. (could take a long time on a big system) find / -name myfile -print
ftp Transfer files to and from remote systems on a network. Both systems must be set up with an IP address in the rhost or host files, as well as a lot of other stuff. The person running ftp must have a login on both machines or the remote system must support anonymous ftp. Example ftp session to get myfile and send yourfile. ftp computername #starts ftp and connects to remote site > login #asks for login and pass word > cd /u/mydirectory #cd to the directory that has the file > get myfile #get the file you want > put yourfile #send the file you want to send. > exit #log out > bye #exit ftp
fold fold breaks the lines of the file named so they are no more than 80 columns wide (the default) . This is particularly useful if you are using a laser printer and need to print files that have lines longer than 80 columns. (i.e. shell scripts that the line has rapped around on.) Example folds the file myfile so the laser printer won`t chop off the ends of long lines. fold myfile | lp -dlaser
grep
Back to Quick Reference
Search one or more files for a pattern. Example Searches for the word FROM in all file that end in .4gl grep FROM *.4gl
head
Back to Quick Reference
Displays the first few lines of a file. Example Displays the first 24 lines of myfile. head -24 myfile
id
Back to Quick Reference
Lists user and group id. Example id
kill
Back to Quick Reference
Used to kill or stop a program or process that can't be stopped normally. You must be the owner of the process (you must have been the one that started it) to kill it. Example kill -9 3288 # Note: 3288 is the pid (process id) see # the command ps
login
Back to Quick Reference
Connect and identify yourself to the system. At the beginning of each terminal session the system will prompts you for your logname and password.
logname Displays you login name.
lp Sends files to the printer. Example Sends myfile to the default printer lp myfile Sends myfile to the printer called laser1. lp -dlaser1 myfile
lpstat Displays the status of the lp print queue. Example Shows all status information. lpstat -t
ls Lists all files in the current directrory if no name of file is given. Example Lists all files in current directory ls Does a long listing on all files in current directory ls -l
man
Back to Quick Reference
Displays the online manual pages that exist on most systems. Example Displays manual entree for the man command. man man Displays manual entree for the mv command. man mv
mkdir Creates the named directory. You must have write permission in the parent directory to create a sub directory. Example Creates a directory called mydirectory mkdir mydirectory
more Displays the named file to the screen one screen at a time. Example Pages through myfile. more myfile Pages a large directory listing. ls | more
nohup
Back to Quick Reference
Allows you to logout and have the program you nohuped to continue to execute. You must use the & at the end of the command. (nohup = no hangup) Any output from the program will be sent to a file called nohup.out Example nohups and places the program myprog in the background. nohup myprog&
pack
Back to Quick Reference
pack is an older compression program similar to compress. When a file is packed, it has an extension of .z To restore the file to it's original condition use unpack. Example Reduces the size of myfile with pack. pack myfile Restores myfile to it's original size. unpack myfile
page Works just the same as more Example Pages through the file myfile page myfile
passwd Make or change your password. Only the owner or root can change a password. Example Type passwd after you login. Follow the instructions on the screen.
pcat Displays line by line a file that has been compressed with pack. Example Displays myfile.z line by line to the screen, just like cat pcat myfile.z
ps Reports on active processes for the user that ran the command. If you use the -ef options reports all active processes. Example Displays all the active processes for who ever typed the command. ps Displays all active processes. ps -ef Lists all active processes and sends that list to grep via pipe, grep filters out all processes that don't contain the sting .4ge. ps -ef | grep .4ge
pwd Displays the full pathname of you current directory Example pwd
read
Back to Quick Reference
Reads one line of standard input and assigns it to a variable. THIS IS REALLY A BOURNE SHELL COMMAND Not a Unix command! Example Shell script fragment. Prompts the user to enter an answer. Assigns the input to the variable ANSWER. echo "Do you wish to continue? >>\c" read ANSWER case $ANSWER in [yY]) echo "Here we go!";; #if $ANSWER is y or Y echo and go on *) echo "Bye Bye!"; exit;; #if $ANSWER is anything else exit esac
rlogin Connect your terminal on the local host system, to remote system. The two systems must be networked together. The .rhosts file in your home directory of the remote machine has the hostnames that you are allowed to connect to without a password. Example Connects to a system called catseye rlogin catseye
rm Removes one or more files. You must have write permission in the directory that contains the file, but you do not have to have write permission for the file itself. If you don't have write permission on the file you will be prompted y or n to override the permissions of the file. Example Deletes the file called myfile. rm myfile Will remove all files in the current directory, but will prompt y/n before each file. rm -i *
rmdir Removes the directory you name. The directory must be empty of all files, and sub directories. Example Removes the directory mydirectory rmdir mydiretory
sed
Back to Quick Reference
sed or Stream Editor edits files without interaction from user. Example Replaces all occurrences of a \ in the file myfile with the string Here is where it was. sed 's/\\/Here is where it was\g' myfile > fixedfile
sh sh is the STANDARD COMMAND INTERPRETER (the Bourne shell) that executes commands from the terminal or script (file). Always write shell scripts using the Bourne shell!! The reason for this is; the Bourne shell will be on every single Unix machine you will ever find. It is best to make scripts as portable as you possibly can, so USE THE BOURNE SHELL Put #!/bin/sh as the first line of your script to force the OS to use sh.
sleep Waits a the number of seconds specified before executing the next command. Example Displays the message I am asleep for 5 seconds, and then displays I am awake. echo "I am asleep" sleep 5 echo "I am awake"
sort Sorts all the lines of a given file in alphabetical order. Output is to standard out. Example Sorts myfile alphabetical sort myfile Sorts myfile in reverse order sort -r myfile Sorts myfile in arithmetic order sort -n myfile Sorts myfile and puts the output back in myfile sort -o myfile myfile
tail
Back to Quick Reference
Prints the last 10 lines of a file to standard out. Example Prints the last 10 lines of myfile tail myfile Displays the last 10 lines of myfile in real time and continuously until the interrupt key is hit. tail -f myfile
tar Creates and restores archives from tape or file. If creating a tap archive the device must be specified i.e /dev/rmt0. If the archive is to be a file use a name in place of the device. Example Creates a archive called dougtar that contains myfile, herfile, hisfile and yourfile tar cvf dougtar myfile herfile hisfile yourfile Lists the contents in the archive dougtar tar tvf dougtar Extracts all the files from the archive dougtar tar xvf dougtar Archives the directory /u/doug to floppy. tar cvf /dev/rfd0 /u/doug
tee Alows anything that normally would go to standard out, (the screen) to be sent to a file or files as well as to the screen. Example Displays myfile to the screen and places a copy of the file in herfile. cat myfile | tee herfile Sorts myfile, displays it to the screen (default for sort) and puts the sorted output in a file called mysort. sort myfile | tee mysort
touch Updates the time and date accessed of the named files to the current time and date. Will also create an empty file if the named file does not exist. Example Script fragment. Makes sure the file myfile exists before the remove command is executed on the file. (to prevent error message) touch myfile rm myfile
tr Used to change characters in a file or delete characters. Example Removes any ? from myfile tr -r ? myfile
Back to Quick Reference


MORE TO COME!

Back to Unix Page




Back to Doug's home page.