| ||||||||||
Help on any Unix command. | ||||||||||
man {command} | Type man ls to read the manual for the ls command. | |||||||||
| ||||||||||
List a directory | ||||||||||
ls {path} | It's ok to combine attributes, eg ls -laF gets a long listing
of all files with types. | |||||||||
ls -l {path} | Long listing, with date, size and permisions. | |||||||||
ls -a {path} | Show all files, including important .dot files that don't otherwise
show. | |||||||||
ls -F {path} | Show type of each file. "/" = directory, "*" =
executable. | |||||||||
ls {path} | more | Show listing one screen at a time. | |||||||||
| ||||||||||
Change to directory | ||||||||||
cd {dirname} | There must be a space between. | |||||||||
cd | Go back to home directory, useful if you're lost. | |||||||||
cd .. | Go back one directory. | |||||||||
| ||||||||||
Make a new directory | ||||||||||
mkdir {dirname} | ||||||||||
| ||||||||||
Remove a directory | ||||||||||
rmdir {dirname} | Only works if {dirname} is empty. | |||||||||
rm -r {dirname} | Remove all files and subdirs. | |||||||||
rm -rf {dirname} | Remove all files and subdir and does not ask if suspicious. | |||||||||
| ||||||||||
Print working directory | ||||||||||
pwd | Show where you are as full path. Useful if you're lost or
exploring. | |||||||||
| ||||||||||
Copy a file or directory | ||||||||||
cp {file1} {file2} | ||||||||||
cp -r {dir1} {dir2} | Recursive, copy directory and all subdirs. | |||||||||
cat {file1} {f2} ... > {newfile} | Combine many files into one. | |||||||||
cat {newfile} >> {oldfile} | Append newfile to end of oldfile. | |||||||||
| ||||||||||
Move (or rename) a file | ||||||||||
mv {oldfile} {newfile} | Moving a file and renaming it are the same thing. | |||||||||
mv {oldname} {newname} | ||||||||||
mv {old_directory}/{name} {new_directory}/{name} | ||||||||||
| ||||||||||
Delete a file | ||||||||||
rm {filespec} | ? and * are wildcards; "?" is any single
character; "*" is any string of characters. | |||||||||
ls {filespec} rm {filespec} | Good strategy: first list a group to make sure it's what's you
think... ...then delete it all at once. | |||||||||
| ||||||||||
View a text file | ||||||||||
more {filename} | View file one screen at a time. | |||||||||
less {filename} | Like more, with extra features. | |||||||||
cat {filename} | more | View file one screen at a time. | |||||||||
| ||||||||||
Edit a text file. | ||||||||||
nano {filename} | Never use MS Word for program files. MS Word inserts
invisible characters that break Bioinformatics programs.
vi and
emacs are also available. | |||||||||
| ||||||||||
Create a text file. | ||||||||||
cat > {filename} | Enter your text (multiple lines with enter are ok) and press
control-d to save. | |||||||||
nano {filename} | Create some text and save it. | |||||||||
| ||||||||||
Compare two files | ||||||||||
diff {file1} {file2} | Show the differences. | |||||||||
sdiff {file1} {file2} | Show files side by side. | |||||||||
| ||||||||||
Other text commands | ||||||||||
grep '{pattern}' {file} | Find regular expression in file. | |||||||||
sort {file1} > {file2} | Sort file1 and save as file2. | |||||||||
sort -n {file1} | Sort file1 numerically, not lexically. | |||||||||
sort -n -r {file1} | Sort file1 reverse numerically (largest first). | |||||||||
wc {file} | Count words in file. | |||||||||
| ||||||||||
Find files on system | ||||||||||
find ~user -name {fname} -ls | Works with wildcards. Handy for snooping. | |||||||||
| ||||||||||
Wildcards and Shortcuts | ||||||||||
* | Match any string of characters, eg page* gets page1, page10,
and page.txt. | |||||||||
? | Match any single character, eg page? gets page1 and page2, but
not page10. | |||||||||
[...] | Match any characters in a range, eg page[1-3] gets page1,
page2, and page3. | |||||||||
~ (tilde) | Short for your home directory, eg cd ~ will take you home. run_program ~/data/file will use data file from ~/data directory. | |||||||||
. | The current directory. | |||||||||
.. | One directory up the tree, eg ls .. | |||||||||
| ||||||||||
Pipes and Redirection | (You pipe a command to another command, and redirect it
to a file.) | |||||||||
{command} > {file} | Redirect output to a file, eg ls > list.txt writes directory
to file. | |||||||||
{command} >> {file} | Append output to an existing file, eg cat update >>
archive adds update to end of archive. | |||||||||
{command} < {file} | Get input from a file, eg sort < file.txt | |||||||||
{command} < {file1} > {file2} | Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt. | |||||||||
{command} | {command} | Pipe one command to another, eg ls | more gets directory and
sends it to more to show it one page at a time. | |||||||||
| ||||||||||
Permissions, important and tricky! | ||||||||||
Unix permissions concern who can read a file or
directory, write to it, and execute it. Permissions are
granted or withheld with a magic 3-digit number. The three digits
correspond to the owner (you); the group (?); and the
world (everyone else).
| ||||||||||
| ||||||||||
chmod u+rw {filespec} | Give yourself read and write permission | |||||||||
chmod u+x {filespec} | Give yourself execute permission. | |||||||||
chmod +x {filespec} | Make your script executable. | |||||||||
| ||||||||||
System info | ||||||||||
date | Show date and time. | |||||||||
df | Check system disk capacity. | |||||||||
du | Check your disk usage and show bytes in each directory. |
Long listings (ls -l) have this format:- file d directory, * executable ^ symbolic links (?) file size (bytes) file name / directory ^ ^ ^ ^ ^ drwxr-xr-x 11 mkummel 2560 Mar 7 23:25 public_html/ -rw-r--r-- 1 mkummel 10297 Mar 8 23:42 index.html ^ ^^^ user permission (rwx) date and time last modified ^^^ group permission (rwx) ^^^ world permission (rwx)
A Unix script is a text file of commands that can be executed, like a .bat file in DOS. Unix contains a powerful programming language with loops and variables that I don't really understand. Here's a useful example.Unix can't rename a bunch of files at once the way DOS can. This is a problem if you develop Web pages on a DOS machine and then upload them to your Unix Server. You might have a bunch of .htm files that you want to rename as .html files, but Unix makes you do it one by one. This is actually not a defect. (It's a feature!) Unix is just being more consistent than DOS. So make a script!
Make a text file (eg with nano) with the following lines. The first line is special. It tells Unix what program or shell should execute the script. Other # lines are comments.
#!/bin/sh # htm2html converts *.htm files to *.html for f in *.htm do set base=`basename $f .htm` echo $base.html # first do echo to see if the name is correct # mv $f $base.html # remove the initial # to actually rename the file doneSave this in your home directory as "htm2html.sh" . Then make it user-executable by typing chmod +x htm2html.sh. After this a * will appear by the file name when you ls -F, to show that it's executable. Change to a directory with .htm files and type ~/htm2html.sh, and it will do its stuff.Think about scripts whenever you find yourself doing the same tedious thing over and over.
Action DOS UNIX
change directory cd cd
change file protection attrib chmod
compare files comp diff
copy file copy cp
delete file del rm
delete directory rd rmdir
directory list dir ls
edit a file edit nano
environment set printenv
find string in file find grep
help help man
make directory md mkdir
move file move mv
rename file ren mv
show date and time date, time date
show disk space chkdsk df
show file type cat
show file by screens type filename | more more sort data sort sort