Linux bash shell Reference

 

1.       Loggin on

·         The easiest way to access the Linux operating system from the CET lab computers or from your computer at home is to use PuTTY, a SSH tool (there are many other SSH programs).

·         Enter the following host name: cet.ssu.portsmouth.oh.us

2.       Linux overview

·         Linux (and UNIX) is based on a hierarchical file system, which may be distributed among one or more physical computers.

·         The root directory of the file system is indicated by a /.  Other subdirectories lie in the root directory.  Some common ones are

·         /bin : Many common functions and programming libraries available to all users of the system.

·         /usr/include : The standard C .h files used when you use #include <xxx.h>

·         /home : This is where all student and faculty accounts reside.  Your account folder has the same name as your SSU ID, faculty have the same folder name as their email address.  You can’t look in someone else’s folder unless they explicitly give you permission to.

·         If you are currently located in your home directory and want to display the contents of your lab7.c file from your etec102 directory, you would type cat etec102/lab7.c.  If you were currently located in the root directory, you would type cat /home/1234567/etec102/lab7.c.  Your current location is called the current working directory.

·         If you get lost, you can type cd ~ at any time to return you to the root of your home directory (~ is a special environment variable that is initialized by Linux whenever you log on to the system).

·         If you want to see your current location, type pwd (print working directory).

3.       Command format

·         to invoke a Linux command, you usually use the following format : command_name arg1 arg2 ... where the arguments are prefaced by a hyphen.  For example, to use the ‘l’ and ‘a’ arguments of the ls command, you would type ls –l –a or ls –la (they both do the same thing).

·         Special Symbols and their usage

·         command > filename                    Runs command and sends the output to filename.

·         command < filename                    Runs command but instead of getting input from the keyboard, input is obtained from filename.

·         command >> filename  Runs command and appends the output to filename.

·         command1 | command2               Runs command1 and sends the output to command2.

·         Combinations are allowed, like command < infile > outfile  uses infile as input to command and sends the result to outfile.  command > outfile < infile works identically to the previous example.

·         To run a long command (or a command that you want to keep running while you do other things), you normally run it in the background.  To do this, append an ampersand to the end of the command (ls -l&).  You will immediately be returned to the command prompt and be given a process id (pid) number for the process.  To kill the process, use this number as an argument to the kill (or pkill) command.

4.       Environment Variables

·         These are variables (like in C) that hold a string value.

·         To see all of them, type env

·         Some notable variables:

·         PS1 : Defines what your prompt looks like

·         PATH : Shows a list (separated by colons) of places to look when a command is typed at the prompt – if it is not found in the first, look at the second path, etc.

·         HOME : defines your home directory.

5.       Some common UNIX operations

PS1=’[\w]:’

export PS1

PATH = $PATH:$HOME/bin:.

export PATH

 

6.       Interesting Things

7.       Basic Commands (and common arguments)

 

Command

Options

Description

Example

man

<command name>

shows the manual page for the command or tool in question.  Press <Break> to exit.

man cp

apropos

<text>

searches man pages for the requested text string

apropos printf

cal

 

Shows the current month in a nicely formatted display.

 

cat

<file name>

Outputs a text file to the screen

cat test.txt

> <file name>

Lets you enter text into a new file (text input continues until you hit CTRL-D)

cat > test.txt

g++

lots of options

Use the C/C++ compilers. 

g++ -c test.cpp

g++ -o test test.o

cd

<directory name>

Changes the current working directory.

cd /usr/bin/

cd etec102/lab7

cd ../../csci380

cd ~

chmod

<properties> <file>

Changes the properties of a file.  Properties is an octal number where the first three binary bits are the user's rwx, second are groups, and third is everyone.

chmod 755 index.html

clear

 

Clears the screen.

 

cp

<src> <dst>

Copies a file, src, to dst, possibly over-writing an existing file.

cp test2.txt test2.BAK

cp /usr/bin/data1.bin .

-r

Recursively copies an entire directory.

cp –r /usr/bin/witherej/data_files .

date

Date

Displays the current date and time.

 

diff

<file1> <file2>

Compares two text files.  The differences are displayed.

 

du

| sort –r –n

Disk usage (size of files)Pipes the output to the sort utility that sorts by size in descending order.

 

env

 

Shows your environment variables.

 

logout

 

Exit’s the UNIX shell.  If you log in through Window’s telnet program, this will close that window.

 

find

<start_dir> <options>

Searches for a directory starting with start_dir (/ for the root) using options.

find . –name ‘*s’ –print (finds all files in the current directory that end with s)

finger

 

lists all users currently on the UNIX system (more detailed than who).

 

grep

<regular expression> <file>

Searches for regular expressions (lab2) in file.  Displays the line containing that text.

grep ‘^f’ test3.txt

-n

Displays the line number

grep -n "xxx" test3.txt

ls

 

Directory listing.  Only file name is shown.  Use wildcards to show only a subset.

 

-l

shows detailed file listing including permissions, modified date, owner, etc.

 

-a

Show hidden files.  Useful if you want to modify your login script (be careful!)

 

mkdir

<new dir. name>

Creates a new sub-directory in the current working directory.

mkdir etec102

more

usually you pipe info from another command

Displays input one page at a time.  Usually you pipe (|) output from a file to more and it displays it one page at a time.

cat test.txt | more

mv

mv source dst

Moves (renames) source to be dst.

mv tempfile new_file

my my_dir ../my_new_dir

nano

<filename>

Opens the nano editor for text editing.  If there is no filename parameter, a blank file is opened.

pico test.txt

ps

 

Shows current processes you have running.  Each one will have a Process ID that you can use to refer to it (for example, when you kill a process).

 

-l

Shows the state of the process as well (O: running, S: sleeping, R: on a run queue, I: idle, Z: zombie, T: traced, X: waiting for more memory).

 

rm

 

Deletes a file.

rm test.txt

-i

Prompts you before deleting each file.

rm -i test.txt

-r

Recursively deletes a directory and its contents.  Be careful!

rm -r test_dir

rmdir

<dir. name>

Deletes a directory.  It must be empty for this command to work.

rmdir etec102

pwd

 

displays (prints) your current working directory.  Useful if your prompt does not display this info.