This is an old revision of the document!


Basic Unix Commands

Unix has a lot of useful commands. If you want to find out more information about any of these commands, use the man command, which stands for manual. For example, to find out more about the ls command, use

> man ls

A Linux Cheat Shirt

Directory Access/Manipulation

Command Meaning
cd <dir> change directory
ls [<dir>] list the contents of the directory, current directory if no directory is given
mkdir <dir> create a directory
rmdir <dir> delete a directory; only works if the directory is empty
pwd to see the Present Working Directory (the directory that you're currently in)

ls has a lot of optional command-line arguments:

Command-line Argument Meaning
-l a long listing of the files
-r reverse order
-t sort by creation time

Example: ls -lrt lists the files in the current directory by creation time, from most recent to least recent, and lists all the information about the files

File Manipulation

Copy Files and Directories

  • cp <source> <destination> - copy files

To copy the contents of a directory, use the -r option. For example,

cp -r <source> <destination>

Move or Rename Files

  • mv <source> <destination> - move or rename files

Delete Files

  • rm <file> - delete files

View contents of Files

  • To view the contents of a file, you can use cat, less, or more. Examples:
cat <file>
more <file>
less <file>

Short Cuts

  • ~ - your home directory. Example use to go into your home directory:
cd ~
  • . - the current directory
  • .. - the parent directory
  • * - wildcard, matches anything. Example use to list all the java files in a directory:
ls -l *.java

Finding Files

The find command is very useful and powerful.

Example use:

find . -name "*.java"

The above command finds all the files that end with .java in the current directory and its subdirectories.

Finding Files Containing Text

Use the grep command to find files that contain particular text. For example, to find all the Java files that contain the text main.

grep main *.java

grep stands for Gnu Regular Expressions, so you can also search for regular expressions in files.

Printing Files

To print from the command-line, you can use the lpr command. Do not try to print PDF files or images this way. The syntax of the print command is

lpr -P<printername> filename

For example:

lpr -Pcslab test.py

The printer name is cslab when you're in P405 or advlab if you're in P413.

The command lpr means to print the file. The option 'P' says which printer to use (one of the CS lab printers), and the argument says which file to print–in this case test.py.

Creating Printable Files

To create a condensed, printable file of your lab, use the printLab.sh command.

The syntax of the command is

printLab.sh <path_to_lab_directory>

For example, if you're in your labs directory, to print lab1, you'd write

printLab.sh lab1
labs/unix_basics.1378913218.txt.gz · Last modified: 2013/09/11 15:26 by admin
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0