30
Basic Linux/UNIX Commands The symbol of Linux

Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Embed Size (px)

Citation preview

Page 1: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Basic Linux/UNIXCommands

The symbol of Linux

Page 2: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Unix doesn’t really care where you log in from, though some system administrators might.

Logging inLogging in

Log in from anywhere you have permissionHave graphical output sent anywhere you have permission

You must have a username (login id) to use a unix system

This identifies you to the system so it can manage your work properly.

Every user is a member of one or more groups of users.

This helps the system manage different types of user properly.

Page 3: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Connecting to embnet.orgConnected.

Welcome to the Bioinformatics Course.

Login:

Logging inLogging in

Connect to the unix machine using a suitable program on your local machine.

TelnetXtermSecure ShellKermitOther terminal emulators

username

unix is case sensitive. username is not the same as Username or USERNAME

Password:

unix doesn’t show anything on the screen as you type your password.

The system will be unavailable on friday afternoon for maintenance.You have new mail.username@linus ~>

You may get some messages here from the system administrator.

Page 4: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

username@linus ~>

The prompt can be customised to look how you wishyour present location

The shell or command lineThe shell or command line

Several different shells but they behave more or less the same

the machine you are logged in to

1. The Prompt.

your username

Page 5: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

The shell or command lineThe shell or command line

2. Commands

username@linus ~>

The shell breaks the command up into individual words

ls -ald *.txt

The first word is a command

ls -ald *.txtls -ald *.txt

The subsequent words form a list of arguments to the commandarguments beginning with - are options

ls -ald *.txtls -ald *.txt

* is a special character. It means ‘any group of characters’ (including none). The shell finds all the filenames that match anything.txt and adds them to the list of arguments

By default the boundary between words is a space.To get the shell to treat a phrase that includes spaces as a single word, put it in quotes like this: 'my word' or "my word".

Options control how the program runs. '-a -l -d' is equivalent to '-ald'

Page 6: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

String or DollarTreat the next word as a variable and write out its value

Backslash.Change the meaning of the next character.

Pipe.Pass the output of the command on the left as the input to the command on the right.

Redirect the commands output, eg. to a fileRedirect a commands input. eg. from a file instead of the keyboard.SemicolonSeperate commands typed in together.

More Special CharactersMore Special Characters

* ? " '

& | > <

`` $ \

Any group of characters including none.

Any single character.word delineation

Cause the process to run in the backgroundBackticks (not ').Take the output of the command as an argument

Some special characters can lose their special meaning if they are inside quotes.

;

Page 7: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

OrganizationOrganization

"Everything is a file"

•An ordinary file contains data.•A directory contains other files.•A link is a file that is a shortcut to another file.The data could be an image, a document, a set of instructions (a program) or any fixed information.This is also known as a folder on some systems. A directory can contain other directories (which are

then known as sub-directories.)Files can have more than one name, and be in

different directories at the same time

•There are many other types of file .

You don't need to worry about these. You probably won't come across one in normal use of the system.

Page 8: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Organization of the file systemOrganization of the file system

//

The top of the file system is the directory '/', commonly known as the root directory

binbin usr etchome

Several subdirectories under the root directoryusername

Another subdirectory.project

seq2 seq1seq3seq4

letterprotAn example users home directory with a subdirectory and several files

Any file in the file system can be uniquely identified by describing the path to it from the root directory.

/home/username/prot

//

/home/username/prot

home

/home/username/prot

username

/home/username/prot

prot

Page 9: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Organization of the file systemOrganization of the file system

//

binbin usr etchome

username

project

seq2 seq1seq3seq4

letterprot

Any process is located somewhere in the filesystem

The command 'pwd' will tell you where.

username@linus ~> pwd/home/username '~' is a unix shortcut meaning

'your home directory'

Page 10: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Looking at the file systemLooking at the file system

//

binbin usr etchome

username

project

seq2 seq1seq3seq4

letterprot

'ls' lists the files in a directory or directories

username@linus ~> lsprot letter projectusername@linus ~>project:seq1 seq2 seq3 seq4

ls project

Without an argument, ls lists all the files that don't start with . in the current directoryThere are many options to ls that allow you to select and control the information it presents.

Page 11: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Basic shell commands - lsBasic shell commands - ls

• ls: list existing files in current directory– Short listing: ls– List all files: ls –a– Detailed long list: ls –l– All of the above: ls -al

Page 12: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Moving around the file systemMoving around the file system

//

binbin usr etchome

username

project

seq2 seq1seq3seq4

letterprot

'directory' is the directory to which you want to move. The name can be written as the full path (from root) or as the relative path (from your current directory)

You can move to a different directory with the command 'cd directory '

username@linus ~> cd /home/username/projectusername@linus ~/project> pwd/home/username/project

username@linus ~> cd projectusername@linus ~/project> pwd/home/username/project

username@linus ~/project> cd ..

'..' means the parent directory.'.' means the current directory.

..

username@linus ~> pwd/home/username

username@linus ~>

repeat using the relative path

Page 13: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Basic shell commands - cdBasic shell commands - cd

• cd – changing directory– To a subdirectory: cd Desktop– Giving a complete path: cd

/home/<username>/Desktop– To the next directory above: cd ..

Page 14: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Changing the file systemChanging the file system

//

binbin usr etchome

username

project

seq2 seq1seq3seq4

letterprot

You can create a new subdirectory in the current directory with the command ' mkdir directory '

username@linus ~> mkdir modelusername@linus ~>

model

Page 15: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Changing the file systemChanging the file system

//

binbin usr etchome

username

project

seq2 seq1seq3seq4

letterprot

You can delete an empty subdirectory with the command ' rmdir directory '

username@linus ~> rmdir model

model

username@linus ~>

modelYou can delete a file with the command ' rm file '

rm protusername@linus ~>

You can delete a subdirectory and its contents with the command ' rm -rf directory '

Page 16: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

More about files: filenamesMore about files: filenamesFilenames can contain any normal text character including spaces and special characters.

Filenames can be almost any length.

It is best to stick to a-z, A-Z, _, -, and numbers.

It is best to keep them short as it saves typing.

If a filename contains a special character or a space you may need to put quotes around the whole path.

Special characters in filenames can cause problems with some programs.

Page 17: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

You can view the contents of one or more files a page at a time on the screen with the command: ' more file1 file2 ...'You can print the first few lines of a file with the command: 'head file1 file2 ...'

More about files: reading filesMore about files: reading files

You can print the contents of one or more files to the screen with the command: 'cat file1 file2 ...'

cat prints the whole file at once, so a file longer than just a few lines will run off the top of your screen.

more will let you search through a file, go backwards and forwards and has many other functions.

The last few lines can be viewed with 'tail'

Page 18: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

More about files: editing filesMore about files: editing files

You can change the content of text files and create new files with a text editor.

Text editors edit text. They do not try to format the text like word processors.

PICO

A novice friendly basic text editor used as standard on many systems. Start with the command 'pico filename'

EMACS

A powerful editing environment which can be programmed. It has many modes for auto layout of program code. Start with the command 'emacs filename'

VI

A powerful editor which can be somewhat confusing for newcomers. It is designed for rapid editing of text files and programming. Start with the command 'vi filename'

Page 19: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

If newfilename is a directory, then the file will be copied to 'newfilename/oldfilename'

You can copy a file with the command 'cp oldfilename newfilename'

username@linus ~>letter projectusername@linus ~>

More about files: copying filesMore about files: copying files

ls

cp letter draftusername@linus ~> lsdraft letter projectusername@linus ~>Warning:

If a file called newfilename already exists then it will be overwritten.The command 'mv oldfilename newfilename'

can be used to rename a file

Page 20: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

•Permissions determine who can read, write, or execute a given file.

More about files: permissionsMore about files: permissions

•Every file is protected to a greater or lesser extent.

OwnerGroupWorld

The user who owns the file

All the other users in the system.

•Files can have read, write or execute permission for each of the three types of user.

Other users in the same group as the user who owns the file.

Page 21: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

You can view the permissions for a file by listing it in long format with the command 'ls -l filename'

username@linus ~> ls -l letter-rwxr--r-- 1 username users 6048 Aug 17 16:07 letter

The letter lThe file type:- - ordinary filed - directoryl - link (shortcut)

Permissions for the owner

-rwxr--r--

Permissions for the owners group

-rwxr--r--

Permissions for everyone else

-rwxr--r-- username

The user who owns the file

users

The files group

6048

The files size

Aug 17 16:07

The date the file was last modified

letter

The files name

More about files: permissionsMore about files: permissions

Page 22: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

username@linus ~>

You can change the permissions for a file with the command 'chmod change filename'

-rwxr--r-- 1 username users 6048 Aug 17 16:07 letterusername@linus ~>

More about files: permissionsMore about files: permissions

ls -l letter

chmod o-r letterchmod o-r letter

For whom you are changing permissions:o - otherg - groupu - usera - all

chmod o-r letter

Permissions being changed:r - read permissionw - write permissionx - execute (run) permission

chmod o-r letter

How you are changing permissions:- - remove these permissions+ - add these permissions= - set permissions to this

username@linus ~>-rwxr----- 1 username users 6048 Aug 17 16:07 letterusername@linus ~>

ls -l letter

change is the modification you want to make to the files permissions

Page 23: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

You can get help on a command by using the command ' man command'

Getting helpGetting help

This will bring up the manual page and show it to you screen by screen

If you do not know what a command is called, use the option '-k' to get a list of commands that may be relevant'man -k word'

Try using the options '-h', '-help', or '--help' if you can't find the man page.

This will find all manual pages containing word in the short description of the command.

Page 24: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Finding commands quicklyFinding commands quicklyusername@linus ~> history

123 17:55 more food 124 17:58 sort foodusername@linus ~> less .bash_history

username@linus ~>

In Linux

history | grep man

102 15:04 man scp 125 18:05 history | grep man

List of all commands previously typed with the word “man”

Page 25: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Finding commands quicklyFinding commands quickly

> history

> less .bash_history

> history | grep man– You see a list of all the commands you type

with the word man in them

Page 26: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Process ManagementProcess Managementusername@linus ~> ps

PID TTY TIME CMD 22244 pts/27 0:00 tcshusername@linus ~> top

username@linus ~>Ctrl-C to break out

kill -9 16417

last pid: 22413; load averages: 1.14, 1.20, 1.45 16:19:311089 processes:1064 sleeping, 2 running, 17 zombie, 2 stopped, 4 on cpuCPU states: % idle, % user, % kernel, % iowait, % swapMemory: 4096M real, 3310M swap in use, 7852M swap free

PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND16417 lakshmin 1 53 0 28M 20M cpu0 3:07 1.09% netscape13020 root 1 59 0 57M 42M cpu3 114:24 0.91% Xsun29164 root 1 32 0 2960K 1536K cpu1 612:54 0.77% top

Kill [-s signal] | -p] [–a] PID

Page 27: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Determining File Sizes and Space UsageDetermining File Sizes and Space Usage

username@linus ~> dfFilesystem 1k-blocks Used Available Use% Mounted on/dev/md/dsk/d0 4131866 2249730 1840818 55% /swap 7993840 16 7993824 0% /var/run/dev/md/dsk/d7 5017602 957225 4010201 19% /export/homelinux:/export/home 209198492 148520117 39758526 79% /net/linux/

export/homeusername@linus ~> df -h

Page 28: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

FTP – File Transfer ProtocolFTP – File Transfer Protocolusername@linus ~>Connected to ftp.ncbi.nih.gov.220 FTP Server ready.Name (ftp.ncbi.nih.gov:huynh):

ftp ftp.ncbi.nih.gov

anonymous

230 Anonymous access granted, restrictions apply.

331 Anonymous login ok, send your complete email address as your password.Password:

ftp> bin200 Type set to Iftp> ls -l

ftp> cd blast

ftp> ls -l200 PORT command successful.150 Opening ASCII mode data connection for file list.dr-xr-xr-x 12 ftp anonymous 4096 May 20 17:04 blastdr-xr-xr-x 3 ftp anonymous 4096 Mar 2 20:50 cgapdr-xr-xr-x 4 ftp anonymous 4096 Jun 12 18:33 cn3ddr-xr-xr-x 11 ftp anonymous 4096 Dec 20 2001 entrezdr-xr-xr-x 5 ftp anonymous 4096 Jan 15 21:35 fa2htgsdr-xr-xr-x 11 ftp anonymous 12288 Jun 26 07:01 genbankdr-xr-xr-x 15 ftp anonymous 4096 Jun 19 14:22 genomesdr-xr-xr-x 7 ftp anonymous 4096 Feb 14 23:06 mmdbdr-xr-xr-x 4 ftp anonymous 12288 Jun 26 07:36 ncbi-asn1dr-xr-xr-x 73 ftp anonymous 4096 May 17 20:34 pubdr-xr-xr-x 2 ftp anonymous 4096 Dec 14 2001 pubmeddr-xr-xr-x 11 ftp anonymous 4096 Mar 7 17:07 refseqdr-xr-xr-x 59 ftp anonymous 4096 May 15 21:45 repositorydr-xr-xr-x 6 ftp anonymous 4096 Apr 26 19:27 sequindr-xr-xr-x 16 ftp anonymous 4096 Jun 14 22:32 snpdr-xr-xr-x 2 ftp anonymous 4096 Jan 26 1996 tech-reportsdr-xr-xr-x 12 ftp anonymous 4096 Dec 20 2001 toolbox226 Transfer complete.remote: -l1092 bytes received in 0.023 seconds (46.86 Kbytes/s)

ftp> ls -lftp> cd executablesftp> ls -lftp> mget blast.linux.tar.Zmget blast.linux.tar.Z? y200 PORT command successful.150 Opening BINARY mode data connection for blast.linux.tar.Z (20222367 bytes).226 Transfer complete.local: blast.linux.tar.Z remote: blast.linux.tar.Z20222367 bytes received in 4.3 seconds (4579.68 Kbytes/s)ftp>

ftp> ls -l200 PORT command successful.150 Opening ASCII mode data connection for file list.dr-xr-xr-x 2 ftp anonymous 4096 Jun 25 1997 blasturldr-xr-xr-x 3 ftp anonymous 4096 Jul 1 09:46 dbdr-xr-xr-x 2 ftp anonymous 4096 Feb 26 22:59 demodr-xr-xr-x 3 ftp anonymous 4096 Apr 17 2001 documentsdr-xr-xr-x 5 ftp anonymous 4096 Jun 21 19:19 executablesdr-xr-xr-x 3 ftp anonymous 4096 Dec 29 1998 fmergedr-xr-xr-x 2 ftp anonymous 4096 Aug 26 1997 matricesdr-xr-xr-x 3 ftp anonymous 4096 Jun 7 1999 networkdr-xr-xr-x 12 ftp anonymous 4096 Jun 18 22:49 serverdr-xr-xr-x 2 ftp anonymous 4096 Jul 1 15:59 temp226 Transfer complete.remote: -l645 bytes received in 0.052 seconds (12.05 Kbytes/s)

ftp> ls 200 PORT command successful.150 Opening ASCII mode data connection for file list.oldspecialREADME.blsREADME.impblast.hpux11.tar.Zblast.hpux11.tar.gzblast.AIX-powerpc.tar.Zblast.hqxblastz.exeblast.solaris.tar.Zblast.solarisintel.tar.Zblast.linux.tar.Zblast.sgi.tar.Zblast.sgi32.tar.Zblast.macosx.tar.gzblast-FreeBSD-4.5-i386.tar.gzblast.alphaOSF1.tar.Zdelme226 Transfer complete.311 bytes received in 0.038 seconds (8.05 Kbytes/s)ftp>

Page 29: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Useful literatureUseful literature• Learning the UNIX operating system -

O'Reilly Press

• UNIX Quickguide - EMBnet• Running Linux, 3rd Edition – O’Reilly

Press

Page 30: Basic Linux/UNIX Commands The symbol of Linux. Unix doesn’t really care where you log in from, though some system administrators might. Logging in Log

Useful Online ResourceUseful Online Resource• http://www.freshmeat.net/

• http://www.linuxiso.org/• http://www.redhat.com/• http://www.tldp.org/• http://www.linux.org/• http://www.cygwin.com/