Working with Unix
Working With Unix
Unix Command Syntax
Unix Overview
1
Unix Command Syntax
Each Unix command has a defined syntax:
• Abbreviated spelling
• Required elements
Most Unix commands have options:
• The long (-l) or all (-a) used by ls
Unix Command Syntax
There are 3 methods to research the
syntax of Unix commands too see a
complete list of requirements and options
• Use the man command and browse the
Man Help Pages
-> man <command> <enter>
Unix Overview
2
Unix Command Syntax
• Type in the command name at the Unix
prompt and hit enter
-> <command> <enter>
• Use a Google search
-> Unix <command> command
Unix Command Syntax
• Use the man command
Unix Overview
3
Unix Command Syntax
• type command at prompt
Unix Command Syntax
Use a Google search
Unix Overview
4
Unix Command Syntax
Results of Google search
Working With Unix
Special Characters
Unix Overview
5
Special Characters
In Unix, special characters such as input
or output redirection, regular expressions
(such as wildcards and delimiters), and
other special pattern-matching
characters have a special meaning.
Special Characters
Unix special characters might not work
in every Unix shell or might have
different options in different shells.
Simply try the special character and see
if it works in the current shell.
Unix Overview
6
Special Characters
Special
Characters
Explanation
*
Matches any set of characters
?
Matches a single character
-
Often used to prefix a parameter
/
Directory marker and root directory
\
Line continuation or other escapes
.
The current directory
< or >
Redirect Input or Output
|
Pipe output
&
Run command in background
$
Prefix for environment variable
Special Characters
* Command – match all characters
• ls t*
-> list all files starting with letter t
? Command – match 1 character
• ls prog?.cob
-> list all 5-character prog .cob files
Unix Overview
7
Special Characters
< Command – redirect input
• runproc < data_in
-> get input data from data_in not keyboard
> Command – redirect output
• runproc > data_out
-> print output to file data_out not monitor
Special Characters
| Command – pipe redirection
• command1 | command2
-> use the output from command1 as input
to command2
& Command – run command in background
• command1 &
Unix Overview
8
Working With Unix
Basic Commands
Basic Commands
The following tables list some of the
available Unix commands with their
usage and examples.
Unix Overview
9
Basic Commands
File Commands
cat
Usage: Display a files contents to the screen
Example: cat myfile
-> list file on screen
chgrp
Usage: Change group ownership of a file
Example: chgrp newgroup filename
chmod
Usage: Change permissions of a file
Example: chmod 777 myfile
-> use octal
chmod rwxr--r-- myfile -> use permissions
chmod u=rwx,g=r,o=r -> use each group
chmod a=rwx
-> use all group
cp
Usage: Copy files
Example: cp oldfile newfile
Basic Commands
File Commands
Unix Overview
cd
Usage: Change directories
Example: cd
-> go to home directory
cd ..
-> go back 1 directory
cd /…./
-> change directory path
ls
Usage: List directory contents
Example: ls
-> list file names
ls –a
-> list all files
ls –l
-> list file attributes
ls –r
-> reverse order
ls –t
-> based on modification
ls –alrt
-> combination
10
Basic Commands
File Commands
mkdir
Usage: Create a directory
Example: mkdir newdir
mv
Usage: Rename file
Example: mv oldfile newfile -> renames to newfile
pwd
Usage: Print working directory
Example: pwd
rm
Usage: Remove a file
Example: rm oldfile
rmdir
Usage: Remove a directory
Example: rmdir
touch
Usage: Create an empty file
Example: touch newfile
Basic Commands
Process Commands
Unix Overview
ps
Usage: Show processes and their PID
Example: ps
-> show my processe
ps –ef
-> show all processes
kill
Usage: Send signal to process
Example: kill -20 <PID> -> suspend process <PID>
kill -9 <PID> -> stop process <PID>
&
Usage: Run command in background
Example: command1 &
<ctrl>z
Usage: Suspend a running process to regain the
Unix prompt
<ctrl>d
Usage: End of input
Usage: Alternate way to logout
11
Basic Commands
Miscellaneous Commands
clear
Usage: Clear screen
Example: clear
df
Usage: Display disk information
Example: df
diff
Usage: Display file differences
Example: diff myfile yourfile
exit
Usage: End Unix session
Example: exit
find
Usage: find a file name – start in this directory and
find a file named “myfile” and print the path to it
Example: find . –name “myfile” -print
finger
Usage: Check for active users or user existence
Example: finger
finger accountName
Basic Commands
Miscellaneous Commands
Unix Overview
grep
gzip
Usage: find specified text in files
gunzip
Usage: Restore compressed files
history
Usage: Display list of commands
Example: history
man
Usage: Invokes Man Help Pages
Example: man cp
more
Usage: Display first page of file to screen and the use
<enter> to display more. <ctrl>c return to prompt
Example: more myfile
Usage: Compress a set of files.
Compressed file extension .tar.gz
12
Basic Commands
Commands
Unix Overview
passwd
Usage: changes password – follow prompts
Example: passwd
w
Usage: Gives summary of users
Example: w
wc
Usage: Counts number of lines, words, bytes
Example: cat myfile | wc
-> lines, bytes, words
who | wc –l
-> number users
ps –e | wc –l
-> number of processes
who
Usage: Displays list of active users
Example: who
whoami
Usage: Displays logon ID
Example: whoami
13