Common Unix
Commands
Chapter 27
The following is a list of UNIX commands that we have found helpful when modifying web sites on the server. Most UNIX commands have many options and parameters which we have not listed here. Instead we have given examples of practical uses. For more complete information on most command, you can refer to the online manual by typing man [command] at the UNIX prompt. Some commands you can type [command] --help or [command] -?
Note, when we specify something in brackets like so: [filename] that is to indicate that you type in a filename or whatever. Do not include the brackets in your command.
Navigating UNIX:
/ (refers to the root directory on the server)
./ (the current directory that you are in)
../ (parent directory of your current directory)
pwd (shows what you current directory is - giving the full path)
ls (lists all the files in your current directory)
ls -al (lists filenames + information)
ls -alR (lists filenames + information in all subdirectories)
ls -alR | more (lists filenames + information in all subdirectories,
pausing when the screen become full)
ls -alR > result.txt (lists filenames + information in all subdirectories,
and ouputs the results to a file instead of the screen)
ls *.html (lists all files ending with .html)
ls -al /home/usr/bob/ (lists files + info for /home/usr/bob)
cd (changes you to a new directory)
cd images
cd / (changes you to the root directory)
cd /home/usr/images
cd .. (this goes back one directory)
Moving, Copying and Deleting Files:
mv [old name] [new name] (move/rename a file)
cp [filename] [new filename] (copy a file)
rm [filename] (delete a file)
rm * (delete all files in your current directory)
rm *.html (delete all files ending in .html
in your current directory)
Creating, Moving, Copying and
Deleting Directories:
mkdir [directoryname] (creates a new directory)
ls -d */ (lists all directories within current directory)
cp -r [directoryname] [new directoryname] (copy a directory and all
files/directories in it)
rmdir [directoryname] (remove a directory if it is empty)
rm -r [directoryname] (remove a directory and all files in it)
Searching Files and Directories
find / -name [filename] -print (search the whole server for a file)
find . -name [filename] -print (search for a file starting with
the current directory)
find / -name [directoryname] - type d -print
(search the whole server for a direcory)
grep [text] [filename] (search for text within a file)
sed s/[oldtext]/[newtext]/g [filename] (searches file and replaces all
occurances
of [oldtext] with [newtext]
File and Directory Permissions
There are three levels of file permission: read, write and execute. In
addition, there are three groups to which you can assign permission,
The file owner, the user group, and everyone. The command chmod followed
by three numbers is used to change permissons. The first number is
the permission for the owner, the second for the group and the third
for everyone. Here are how the levels of permission translate:
0 = --- (no permission)
1 = --x (execute only)
2 = -w- (write only)
3 = -wx (write and execute)
4 = r-- (read only)
5 = r-x (read and execute)
6 = rw- (read and write)
7 = rwx (read, write and execute)
We prefer that the group always have permission of 0. This prevents other
users on the server from browsing files via Telnet and FTP. Here are the
most common file permissions used:
chmod 604 [filename] (minimum permission for www HTML file)
chmod 705 [directoryname] (minimum permission for www directories)
chmod 705 [filename] (minimum permission for www scripts & programs)
chmod 606 [filename] (permission for datafiles used by www scripts)
chmod 703 [directoryname] (write-only permission for public FTP uploading)
Scheduling Tasks
You can schedule tasks to run automatically by using the UNIX cron command.
To use this, you create a text file with cron instructions, then process
this file. cron instructions are basically UNIX commands with extra info
about the time that they will run.
One important thing to note is that it is best to use full paths when
creating your cron file. As an example, create a file called mycronfile
and in it place one line:
0 1 * * * cp /usr/www/file.txt /usr/www/backup.txt
now at the command line, type the following:
crontab mycronfile
You have just scheduled an automated task! This task will run at the time
specified until you decide you want to cancel it.
There are six fields in this file. The first five represent the time that
the job will run. The sixth field is a UNIX command that will run at the
specified time. The above example will run every night at 1AM, at which
time it will copy a file.
Here is how the fields break down:
Field 1 | Field 2 | Field 3 | Field 4 | Field 5
Minutes | Hours | Day of Month | Month | Day of Week
(0-59) | (0-23) | (1-31) | (1-12) | (0-6)
You can enter a number in the field, a range of numbers, or an * to indicate
all.
Here are a few more examples. These examples use the ls command, which would be
pretty useless. Note the time that it runs, though.
0 1 * * 1-5 ls (this would run every Monday-Friday at 1am)
0 1 * * 1,3,5 ls (this would run every Monday, Wednesday and Friday at 1am)
10 2 1 * * ls (this would run at 2:10am on the first of every month)
0 1 1 1 * ls (this would run at 1am on January 1 every year)
If you have a more complicated command that you want to run, it is sometimes
helpful to create a shell script and have that script run. You specify the
shell script as you would any UNIX command. For example:
0 1 * * * /usr/www/myscript
There are some other crontab switches that are useful:
crontab -l (lists your currently scheduled tasks)
crontab -r (delete all currently scheduled tasks)
crontab -e (directly edit your scheduled tasks)