Anda di halaman 1dari 50

UNIX/Linux Commands

-Mangesh Kaldhone

Jan 2015

Contents

Shell Intro
Command Format
Shell I/O
Command I/O
Command Overview

Jan 2015

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
...

Jan 2015

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)

Jan 2015

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 socalled system calls, to ask the kernel to perform an operation
(usually privileged) on your behalf.

Jan 2015

Command I/O

Jan 2015

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 append
> output

man

Jan 2015

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 theres no
% man woman ...

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

Jan 2015

chsh

Jan 2015

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.

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

Jan 2015

10

General Commands

Jan 2015

11

passwd

Change your login password.


A very good idea after you got a new one.
Its 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 users and group
passwords as well as real name, login shell, etc.
% man passwd

Jan 2015

12

date

Guess what :-)


Displays dates in various formats
% date
% date -u
in GMT
% man date

Jan 2015

13

cal

Calendar
for month
entire year
Years range: 1 - 9999
No year 0
Calendar was corrected
in 1752 - removed 11
days

Jan 2015

% cal
% cal
year
% cal
% cal
% cal
% cal
% cal

2 2000

current month
Feb 2000, leap

2 2100
2 2400
9 1752
0
2002

not a leap year


leap year
11 days skipped
error
whole year

14

clear

Clears the screen


Theres an alias for it: Ctrl+L
Example sequence:
% cal
% clear
% cal
Ctrl+L

Jan 2015

15

sleep

Sleeping is doing nothing for some time.


Usually used for delays in shell scripts.
% sleep 2 2 seconds pause

Jan 2015

16

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

Jan 2015

17

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

Jan 2015

18

unalias

Removes alias
Requires an argument.
% unalias cl

Jan 2015

19

history

Jan 2015

Display a history of
recently used commands
% history
all commands in the
history
% history 10
last 10
% history -r 10
reverse order
% !!
repeat last command

% !n
repeat command n in the
history
% !-1
repeat last command = !!
% !-2
repeat second last
command
% !ca
repeat last command that
begins with ca
20

redirect

Standard Output : Most command line programs that


display their results onstandard output. This can be
redirected using > character. When >> is used the results are
appended, the new results are added to the end of the file.
ls > filename
ls >> filename
Standard Input : Many commands can accept input from a
facility calledstandard input, but like standard output, it can
be redirected using < character
sort < file_list.txt
sort < file_list.txt > sorted_file.txt

Jan 2015

21

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

Jan 2015

22

ps

Theps(i.e.,process status)commandis used to provide


information about the currently runningprocesses, including
theirprocess identification numbers(PIDs).
A process, also referred to as atask, is anexecuting(i.e.,
running) instance of a program. Every process is assigned a
unique PID by the system. The basic syntax of ps is
ps [options]
The four items are displayed PID, TTY, TIME and CMD. TIME
is the amount of CPU time process has been running. CMD is
the name of the command that launched the process. TTY is
the name of theconsoleorterminal.
-a all users; -u detailed info.

Jan 2015

23

top

topprovides an ongoing look at processor activity in real


time. It displays a listing of the most CPU-intensive tasks on
the system, and can provide an interactive interface for
manipulating processes.
It can sort the tasks by CPU usage, memory usage and
runtime, can be better configured than the standard top from
the procps suite.
Most features can either be selected by an interactive
command or by specifying the feature in the personal or
system-wide configuration file.
-d delay between scrren update; -p pid to monitor.

Jan 2015

24

tee

Tee command is used to store and view (both at the same


time) the output of any other command.
Tee command writes to the STDOUT, and to a file at a time.
ls
=> will display output
ls > file
=> will send output to file
ls | tee file
=> will display output and send it to file
ls -l | tee file.txt | sed 's/old/new/
Will send ls output to file and to sed too.

Jan 2015

25

kill

Thekillcommandis used onLinuxand otherUnix-likeoperating


systemsto terminateprocesseswithout having to log out
orreboot(i.e., restart) the computer.
The syntax for kill is
kill [signaloroption] PID(s)
Process Identification Number.
The kill command has a misleading name because it does not
actuallykillprocesses. Rather, it sends signals to them. Each
process is supplied with a set of standardsignal handlersby the
operating system in order to deal with incoming signals. When
no signal is explicitly included in the command,signal 15,
named SIGTERM, is sent by default. If this fails, the
strongersignal 9, called SIGKILL, should be used. For example,
the following command would nearly guarantee that process 485
would be killed:
kill -9 485

Jan 2015

26

apropos

Jan 2015

Search man pages for a


substring.
% apropos word
Equivalent:
% man -k word

% apropos date
% man -k date
% apropos password

27

exit / logout

Exit from your login session.


% exit
% logout

Jan 2015

28

shutdown

Causes system to shutdown or reboot cleanly.


May require superuser privileges
% shutdown -h now
- stop
% shutdown -r now
- reboot

Jan 2015

29

Files

Jan 2015

30

ls
List directory contents
Has whole bunch of
options, see man ls for
details.
% ls
all files except those
starting with a .

% ls -a
all

% ls -A
all without . and ..
Jan 2015

% ls -F
append / to dirs and *
to executables

% ls -l
long format

% ls -al
% ls -lt
sort by modification time
(latest - earliest)

% ls -ltr
reverse
31

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).
Jan 2015

32

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

Jan 2015

33

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

Jan 2015

34

touch

By touching a file you either create it if it did not exists (with 0


length).
Or you update its last modification and access times.
There are options to override the default behavior.
% touch file
% man touch

Jan 2015

35

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.

Jan 2015

36

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
Jan 2015

37

rm

Removes file(s) and/or directories.

% rm file1 [file2] ...


% rm -r dir1 [dir2] ...
% rm -r file1 dir1 dir2 file4 ...

Jan 2015

38

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
Jan 2015

39

find
Looks up a file in a directory tree.
% find . -name name
% find . \(-name w* -or -name W* \)

Jan 2015

40

mkdir

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

Jan 2015

41

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.

Jan 2015

42

pwd

Displays personal working directory, i.e. your current


directory.
% pwd

Jan 2015

43

rmdir

Removes a directory.
% rmdir dirname
Equivalent:
% rm -r dirname

Jan 2015

44

ln

Symbolic link or a shortcut in M$ terminology.

% ln s <real-name> <fake-name>

Jan 2015

45

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
Jan 2015

46

grep
Searches its input for a pattern.
The pattern can be a simple substring or a complex
regular expression.
If a line matches, its directed to STDOUT;
otherwise, its discarded.
% echo blah-foo | grep blah
Will print the matching line

% echo blah-foo | grep zee


Will not.

See a separate grep tutorial.


Jan 2015

47

.login and .shrc

You have two special files in your directory which make


customizing your session easy: .login, which is read every
time you log in; and .cshrc, which is read each time you begin
a new csh or tcsh shell.
In home directory, simple .login and .cshrc files may have
been installed for you by the system administrators; you are
free to change these files.
If they aren't already in place, you can put them there simply
by creating a text file with those names.
A pound sign (#) indicates comments and are not read by the
system.

Jan 2015

48

cont

the.cshrcfileall commands that need to be executed in each


new shell:
allsetcomands, including path setting
allsetenvcommands
aliascommands
aumaskcommand
Some Suggestions for your .cshrc file
set autolist
alias h history
alias work cd /proj/myproj/mangeshk/
alias ll ls lrt

Jan 2015

49

Thank you

Jan 2015

50

Anda mungkin juga menyukai