Top
Posted: April 7, 2010

Basic Unix Commands

hit “Tab” twice to list all available commands (there’s a lot!)
help also lists some commands and their options
man command to get the full list of options available for that command
q to exit man pages

Navigation

cd to change directory
When you open the terminal it should default to your user area, so just typing cd desktop should be all you need to get to the desktop.
.. represents parent directory
cd .. to move to parent directory
cd ../.. to move to parent directory of the parent directory
/. represents ‘root’ – thus cd /. to move to root directory
cd ~ or just cd to move back to home directory
* is the wildcard
For example: cd De* from home directory will move you to the desktop, unless there is another folder in your home directory with a name that starts with ‘De’
ls or ls -l or ls -al to view contents of directory
pwd to view where you are

Working with Files

touch filename to create file if it does not already exist
mkdir directoryname to create a new directory
rm filename to delete a file
rmdir directoryname to delete a directory
rm -r directoryname to delete a directory with files in it
mv filename newdirectory to move a file
mv file1 file2 to rename a file

Writing and Copying Files

echo Hi there! > file.txt writes string to file (erasing preexisting data)
echo Hi there! >> file.txt appends string to the end of file
cp file.txt copy.txt to copy a file
cat < file.txt > copy.txt also
cat < file1 >> file2 appends file1 to the end of file2
cat file1 file2 >> file3 concatenates file1 and file2 and appends them to the end of file3

Looking at Files

more filename to view contents of a file
pico filename opens file with a simple text editor
Some other text editors are ed, awk and emacs

Sort Files

sort filename alphanumerically sorts file by line and sends output to screen (STDOUT)
NB: Alphanumeric order starts with numbers, then uppercases, and then lowercases
sort file.txt > output.txt writes sorted file to output.txt
sort file.txt -o output.txt same
sort -r file.txt > output.txt sorts in reverse order
uniq file.txt > output.txt eliminates duplicate lines
sort file.txt | uniq > output.txt sorts and eliminates duplicates in one shot via a ‘pipe’
find . -exec grep -H -n ‘hello’ {} \; lists the files that contain a given string

Some Other File Operators

split, join, comm, cmp, diff, sed
grep searches for a given string in a file (lots of options for grep)
du filename tells how much space a file occupies
du -h filename reports in easy to read (human) format

Permissions

chmod [options] file
Either octal numbers or letters can be used as the options following the chmod command

Octal

3 digits with first being the owner, the second being the group and the third the world
(4) readable, (2) writable, (1) executable
Example:
chmod 755 file gives permissions to read and execute by all, and to write by the owner

Letters

r=read w=write x=execute
u=owner g=group o=others a=all
Examples:
chmod a+r file allow all users permission to read (same as chmod +r file)
chmod go-rw file take away permissions from the group and the world to read and write

Programs and Processes

ps -x index of running processes
kill -9 #ofapp stop/quit process (excellent for killing a frozen program)
./program to run program
./program > output.txt writes output to file
nohup mycommand & runs command w/out hangups and in the background and writes output to nohup.out
For example: nohup ./a.out 25 &

-V shows version
For example: perl -V shows version of installed perl

Links

ln hard link
ln -s symbolic link

Symbolic link example:
ln -s /Library/Perl
New alias to perl directory added to current directory

Hard link example:
touch file1.txt
ln file1.txt file2.txt
echo “ABCD” > file1.txt

Both files will now have “ABCD”
Changing either one of the files will change the other since they point to the same address (inode) on the disk, as can be seen by ls -i

Some Miscellaneous Tools

date to show the current time and date
cal to show calendar
bc to start calculator, quit to exit
This is one mean little calculator (e.g. try 2^500)
The calculator can also read program files
bc -q to suppress welcome statements
echo 2^12 | bc runs the command on a single line without having to start and quit bc

Tar and Compress

Tar is used to zip several files into one file
Gzip compresses (one of several available compressors)
Usually they are used together to compile and compress
Both files and directorys can be tarred and compressed
tar -cvf file.tar file/directory makes .tar of file/directory
tar -cvf file.tar *.txt makes .tar of all text files in current directory
gzip -c file.txt > file.gz makes .gz of file.txt
tar -czvf file.tgz file/directory makes .tgz of file/directory
also zip file.zip inputfile1 inputfile2
or zip -r file.zip directory for directory
find . \( -name *.cpp -o -name *.h \) -exec tar -uvf archive.tar {} \; find and tar

Decompress

gunzip -c file.tar.gz > file.tar unzips
tar -xvf file.tar untars
tar -xvzf file.tar.gz does both at once
also unzip file.zip