0% found this document useful (0 votes)
68 views

Unix Commands

This document provides an overview of common UNIX commands. It begins by introducing shells and their purpose of allowing users to execute both internal shell functions and external programs. It then describes the basic format of commands and covers shell input/output and command input/output. The remainder of the document lists and briefly describes several important general commands like ls, cat, man and date as well as commands for working with files, directories and the filesystem.

Uploaded by

suraj kanal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Unix Commands

This document provides an overview of common UNIX commands. It begins by introducing shells and their purpose of allowing users to execute both internal shell functions and external programs. It then describes the basic format of commands and covers shell input/output and command input/output. The remainder of the document lists and briefly describes several important general commands like ls, cat, man and date as well as commands for working with files, directories and the filesystem.

Uploaded by

suraj kanal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

UNIX Commands

COMP 444/5201
Revision 1.4
January 25, 2005

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 1


cordia.ca
Contents
• Shell Intro
• Command Format
• Shell I/O
• Command I/O
• Command Overview

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 2


cordia.ca
Shell Intro
• A system program that allows a user to execute:
– shell functions (internal commands)
– other programs (external commands)
– shell scripts
• Linux/UNIX has a bunch of them, the most common are
– tcsh, an expanded version of csh (Bill Joy, Berkley, Sun)
– bash, one of the most popular and rich in functionality shells, an
expansion of sh (AT&T Bell Labs)
– ksh, Korn Shell
– zhs
– ...

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 3


cordia.ca
Command Format
• Format: command name and 0 or more
arguments:
% commandname [arg1] ... [argN]
• By % sign I mean prompt here and hereafter.
• Arguments can be
– options (switches to the command to indicate a
mode of operation) ; usually prefixed with a hyphen
(-) or two (--) in GNU style
– non-options, or operands, basically the data to work
with (actual data, or a file name)
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 4
cordia.ca
Shell I/O
• Shell is a “power-user” interface, so the user interacts with
the shell by typing in the commands.
• The shell interprets the commands, that may produce some
results, they go back to the user and the control is given
back to the user when a command completes (in general).
• In the case of external commands, shell executes actual
programs that may call functions of the OS kernel.
• These system commands are often wrapped around a so-
called system calls, to ask the kernel to perform an
operation (usually privileged) on your behalf.

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 5


cordia.ca
Command I/O
• Input to shell:
– Command name and arguments typed by the user
• Input to a command:
– Keyboard, file, or other commands
• Standard input: keyboard.
• Standard output: screen.
• These STDIN and STDOUT are often together referred to as a terminal.
• Both standard input and standard output can be redirected from/to a file or other
command.
• File redirection:
– < input
– > output
– >> output append

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 6


cordia.ca
Commands

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 7


cordia.ca
man
• Manual Pages
• The first command to remember
• Contains info about almost everything :-)
– other commands
– system calls
– c/library functions
– other utils, applications, configuration files
• To read about man itself type:
% man man
• NOTE: unfortunately there’s no
% man woman ...
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 8
cordia.ca
which
• Displays a path name of a command.
• Searches a path environmental variable for the
command and displays the absolute path.
• To find which tcsh and bash are actually in
use, type:
% which tcsh
% which bash
• % man which for more details

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 9


cordia.ca
chsh
• Change Login Shell
• Login shell is the shell that interprets commands after you
logged in by default.
• You can change it with chsh (provided that your system
admin allowed you to do so).
• To list all possible shells, depending on implementation:
% chsh -l
% cat /etc/shells
• % chsh with no arguments will prompt you for the shell.

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 10


cordia.ca
whereis
• Display all locations of a command (or
some other binary, man page, or a source
file).
• Searchers all directories to find commands
that match whereis’ argument
• % whereis tcsh

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 11


cordia.ca
General Commands

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 12


cordia.ca
passwd
• Change your login password.
• A very good idea after you got a new one.
• It’s usually a paranoid program asking your password to
have at least 6 chars in the password, at least two
alphabetical and one numerical characters. Some other
restrictions (e.g. dictionary words or previous password
similarity) may apply.
• Depending on a privilege, one can change user’s and group
passwords as well as real name, login shell, etc.
• % man passwd
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 13
cordia.ca
date
• Guess what :-)
• Displays dates in various formats
• % date
• % date -u
– in GMT
• % man date

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 14


cordia.ca
cal
• Calendar • % cal current month
• % cal 2 2000 Feb 2000, leap year
– for month
• % cal 2 2100 not a leap year
– entire year • % cal 2 2400 leap year
• Years range: 1 - 9999 • % cal 9 1752 11 days skipped
• % cal 0 error
• No year 0
• % cal 2002 whole year
• Calendar was
corrected in 1752 -
removed 11 days

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 15


cordia.ca
clear
• Clears the screen
• There’s an alias for it: Ctrl+L
• Example sequence:
– % cal
– % clear
– % cal
– Ctrl+L

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 16


cordia.ca
sleep
• “Sleeping” is doing nothing for some time.
• Usually used for delays in shell scripts.
• % sleep 2 2 seconds pause

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 17


cordia.ca
Command Grouping
• Semicolon: “;”
• Often grouping acts as if it were a single
command, so an output of different
commands can be redirected to a file:
• % (date; cal; date) > out.txt

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 18


cordia.ca
alias
• Defined a new name for a command
• % alias
– with no arguments lists currently active aliases
• % alias newcommand oldcommand
– defines a newcommand
• % alias cl cal 2003
• % cl
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 19
cordia.ca
unalias
• Removes alias
• Requires an argument.
• % unalias cl

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 20


cordia.ca
history
• Display a history of • % !n
recently used commands – repeat command n in the
• % history history
– all commands in the history • % !-1
• % history 10 – repeat last command = !!
– last 10 • % !-2
• % history -r 10 – repeat second last command
– reverse order • % !ca
• % !! – repeat last command that
begins with ‘ca’
– repeat last command

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 21


cordia.ca
apropos
• Search man pages for • % apropos date
a substring. • % man -k date
• % apropos word • % apropos password
• Equivalent:
• % man -k word

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 22


cordia.ca
exit / logout
• Exit from your login session.
• % exit
• % logout

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 23


cordia.ca
shutdown
• Causes system to shutdown or reboot
cleanly.
• May require superuser privileges
• % shutdown -h now - stop
• % shutdown -r now - reboot

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 24


cordia.ca
Files

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 25


cordia.ca
ls
• List directory contents • % ls -F
• Has whole bunch of options, – append “/” to dirs and “*” to
see man ls for details. executables

• % ls • % ls -l
– all files except those starting – long format
with a “.” • % ls -al
• % ls -a • % ls -lt
– all – sort by modification time
• % ls -A (latest - earliest)
– all without “.” and “..” • % ls -ltr
– reverse

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 26


cordia.ca
cat
• Display and concatenate files.
• % cat
– Will read from STDIN and print to STDOT every line you enter.
• % cat file1 [file2] ...
– Will concatenate all files in one and print them to STDOUT
• % cat > filename
– Will take whatever you type from STDIN and will put it into the
file filename
• To exit cat or cat > filename type Ctrl+D to
indicate EOF (End of File).
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 27
cordia.ca
more / less
• Pagers to display contents of large files
page by page or scroll line by line up and
down.
• Have a lot of viewing options and search
capability.
• Interactive. To exit: ‘q’

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 28


cordia.ca
less
• less ("less is more") a bit more smart than the more command
• to display contents of a file:
– % less filename
• To display line numbers:
– % less -N filename
• To display a prompt:
– % less -P"Press 'q' to quit" filename
• Combine the two:
– % less -NP"Blah-blah-blah" filename
• For more information:
– % man less

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 29


cordia.ca
touch
• By touching a file you either create it if it did
not exists (with 0 length).
• Or you update it’s last modification and access
times.
• There are options to override the default
behavior.
• % touch file
• % man touch
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 30
cordia.ca
cp
• Copies files / directories.
• % cp [options] <source> <destination>
• % cp file1 file2
• % cp file1 [file2] … /directory
• Useful option: -i to prevent overwriting existing files
and prompt the user to confirm.

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 31


cordia.ca
mv
• Moves or renames files/directories.
• % mv <source> <destination>
– The <source> gets removed
• % mv file1 dir/
• % mv file1 file2
– rename
• % mv file1 file2 dir/
• % mv dir1 dir2
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 32
cordia.ca
rm
• Removes file(s) and/or directories.
• % rm file1 [file2] ...
• % rm -r dir1 [dir2] ...
• % rm -r file1 dir1 dir2 file4 ...

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 33


cordia.ca
script
• Writes a log (a typescript) of whatever happened in
the terminal to a file.
• % script [file]
• % script
– all log is saved into a file named typescript
• % script file
– all log is saved into a file named file
• To exit logging, type:
– % exit
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 34
cordia.ca
find
• Looks up a file in a directory tree.
• % find . -name name
• % find . \(-name ‘w*’ -or -name ‘W*’ \)

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 35


cordia.ca
mkdir
• Creates a directory.
• % mkdir newdir
• Often people make an alias of md for it.

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 36


cordia.ca
cd
• Changes your current directory to a new one.
• % cd /some/other/dir
– Absolute path
• % cd subdir
– Assuming subdir is in the current directory.
• % cd
– Returns you to your home directory.

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 37


cordia.ca
pwd
• Displays personal working directory, i.e.
your current directory.
• % pwd

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 38


cordia.ca
rmdir
• Removes a directory.
• % rmdir dirname
• Equivalent:
– % rm -r dirname

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 39


cordia.ca
ln
• Symbolic link or a “shortcut” in M$
terminology.
• % ln –s <real-name> <fake-name>

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 40


cordia.ca
chmod
• Changes file permissions
• Possible invocations
– % chmod 600 filename
– -rw------- 1 user group 2785 Feb 8 14:18 filename
(a bit not intuitive where 600 comes from)
– % chmod u+rw filename
(the same thing, more readable)
– For the assignment:
• % chmod u+x myshellscript
(mysshellscript is now executable)
• -rwx------ 1 user group 2785 Feb 8 14:18 myshellscript

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 41


cordia.ca
grep
• Searches its input for a pattern.
• The pattern can be a simple substring or a complex regular
expression.
• If a line matches, it’s directed to STDOUT; otherwise, it’s
discarded.
• % echo “blah-foo” | grep blah
– Will print the matching line
• % echo “blah-foo” | grep zee
– Will not.
• See a separate grep tutorial.
July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 42
cordia.ca
Pipes
• What's a pipe?
– is a method of interprocess communication (IPC)
– in shells a '|' symbol used
– it means that the output of one program (on one side of
a pipe) serves as an input for the program on another
end.
– a set of "piped" commands is often called a pipeline
• Why it's useful?
– Because by combining simple OS utilities one can
easily solve more complex tasks

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 43


cordia.ca
More on UNIX Commands
and Editors
• http://www.cs.concordia.ca/help/

July 10, 2003 Serguei A. Mokhov, mokhov@cs.con 44


cordia.ca

You might also like