Basic Commands in Linux

Download Report

Transcript Basic Commands in Linux

Linux Workshop
Session 1
Khadka, Santosh and Panthi, Sanjeeb
Computer Science Department
Lamar University
What is covered?
Working with files
 Working with directories
 Some basic commands
 Process related commands
 Working with vi editor
 Brief Introduction to Nano Editor
Login!
Galaxy Account can be used to access all the Linux
machines in the lab 208, both locally and remotely
• To access locally, go to the any machines in 208 and use your
username and password to login.
• To access remotely, use any secure shell (ssh) software.
Example puTTY .
• Open a terminal by right clicking on your desktop.
• Type pwd -> shows the current working directory. You are in
your home directory.
Tools Required for Remote Access
• ssh client
- Example: putty for Windows platform
• sftp client
- Example: WinScp for Windows platform
• remote sftp compatible editor
- Example: jEdit for platform that supports java
Working with files
• To list all the files and/or folders in a current directory by
default.
ls [option]… [file]…
• Options:
• -a: to list all files
• -l: to list in long format showing file attributes and
permission
• --author : gives the author name of the files. Better use
with the combination of –l.
• File: Default is current working directory.
Working with files
• vi <filename> -opens a file to edit if exist, otherwise creates
new file to edit
• ls
- lists contents in current directory
• ls /etc - lists contents in directory /etc
• ls -a - this will list all file including those beginning with
the'.' that would normally be hidden from view.
• Output redirection - Copies the output to some other file.
Example: ls –l > filename (overwritten)
ls –l >> filename (appended)
Working with files
• ls -l
- this gives a long listing showing file attributes and file
permissions.
drwxrwxr-x 6 santoshk santoshk 4096 Sep 14 11:39 test
-rw-rw-r-- 1 santoshk santoshk 140 Nov 17 09:08 test1.c
• cat <filename>
- displays the content of the file
- example: cat filename
- cat –n filename (-n option for giving the line number)d
• cp <sourcefile> <destinationfile>
- copy the content of the file
- example: cp sourcefile.txt destnationfile.txt (source file is
not deleted/modified)
Working with files
• script
- to make script file
- records everything printed on your screen. The record is
recorded to the filename specified.
- starts scripting, once done, type in <ctrl>+d to end
scripting
Example:
script filename - makes a script file with the given filename
script -a filename - append the session record to filename,
rather than overwrite it
• mv <sourcefile> <destinationfile>
- example: mv sourcefile.txt destiantionfile.txt (the sourcefile
file is deleted)
Working with files
• tar - create archives and add or extract files
• tar –cvf output.tar inputfile
Example: tar –cvf aos.tar AOS
• restore a tar file
- tar -xvf inputfile.tar
- .tar file is not compressed by itself. It just bundles the list of
files in one folder.
- add -z option to create a compressed tar file
Example: tar -cvzf file.tar.gz
Working with files
.gzip – compress the files
Example
gzip <filename>
.gunzip –decompress the file
Example
gunzip <filename.gz>
Working with files
• wc
- counts the number of lines,words and bytes in the file
- example: wc –l filename
• head - outputs the first part of the file
- prints the first 10 lines by default
- example: head filename
head -n count filename
• tail - outputs the last part of the file.
- example: tail mylist.cpp
Working with files
• lpr - print command
- command: lpr -p printername filename
Example: lpr -p lab208Printer -#2 file1.txt file2.txt
(this will print two copies of file1.txt and file2.txt)
Note: This is not available at present.
Working with directories
• cd - changes the directory
- example: cd <foldername>
• mkdir - creates a directory under the current working
directory.
- example: mkdir <foldername>
• rmdir - remove files
- example: rmdir <directoryname> (for empty directory)
rmdir –rf directoryname (recursive deletion with force)
Working with files contd..
• Absolute path: start with / (root) to go specific directory
regardless of where you are now.
• Relative path : start with the current directory to go specific
directory.
• Example:
• If, pwd: /home/myfolder then to go /home/myfolder/c++
• Absolute path : cd /home/myfolder/c++
• Relative path : cd c++
A few basic commands
• man - help manual
- example: man keyword
- press <Space bar> to view the next page
- press <return> to view next line
- type “q” exit
• date - display or change the date
- example: date --date =‘2 days ago’
date --set=“2009-9-4 11:59”
date '+DATE: %m/%d/%y%n TIME:%H:%M:%S'
DATE: 02/08/01
TIME:16:44:55
A few basic commands
• chmod
- used to change the access permission
- used to set file permissions to owner, group, and
other
-rw-rw-r-- 1 santoshk santoshk 140 Nov 17 09:08 test1.c
- r = 4, w = 2, x = 1
- example: chmod 444 mylist
• clear
- clear the screen
• grep
- search the file for the specific text
- example: grep string filname
A few basic commands
• hostname - displays or set the system name
- example: hostname [name]
- with no arguments prints the current host name.
- with arguments sets the current host name to the
specified string.
• more
- display output one screen at a time
- related command: less
• quota
- display disk usage and limits
- related command: du, df
du –estimate the file space uses
df – disk space uses (amount of disk space available)
Process related commands
• ps
- gives information about the running processes
- example: ps -ef
ps -fu username
• kill - sends a signal to a process to kill (ends the running
process)
- example: kill -9 pid
• exit - exit from the shell. If there are suspended jobs one
cannot exit from the shell , so kill the processes
using the kill command.
Brief Introduction to vi Editor
•
•
•
•
•
•
•
vi filename
esc - to enter vi command mode
h: moves the cursor one character left (l right)
j :moves the cursor one character down (k up)
u :undo the last changes in the file
x :deletes the character under the cursor
d^: deletes all the characters from current cursor to beginning of
the line
• d$ :deletes all the characters from current cursor to end of the line
• dw: deletes one word from the cursor.
Brief Introduction to vi Editor contd..
• Insert text
Enter input mode and:
i a - insert text before ('i') or after ('a') the current character
I A - insert text at beginning ('I') or end ('A') of current line
o O - open new blank line after ('o') or before ('O') current line
r R - replace one(‘r’) or more character (‘R’) by overwriting
Brief Introduction to vi Editor
• Exiting and Saving
- Press esc and type
:q to quit
:wq or ZZ to save and quit
:q! to quit without saving (!= Forcefully)
:w to save the file
:w filename to save current file under name filename
• Copy and Paste text
nyy or nY - 'copies' n number of line
pP
- insert the contents of the paste buffer [ after /
before ] the current line/character.
Brief Introduction to vi Editor
• Delete text
x X
dw
dd
D
5dd
37Gdd
J
•
Undo
u
- Delete current ('x') or previous ('X') character
- Delete the current word
- Delete the current line
- Delete the rest of the line
- Delete the next five lines
- Delete line 37
- Join lines
- Undo most recent change to the file
Brief Introduction to vi Editor
• Global Searching and replace:
/text - Search forward for some <text>
?text - Search backward for some <text>
n
- Repeat the previous search for the 'next' occurrence
N
- Repeat the previous search but in the opposite
direction
' ' (two single quotes) - Go back to where you where
previously
:1,$s/oldtext/newtext/g - global substitutions
Brief Introduction to vi Editor
• Other popular commands
^
- go to start of line
$
- go to end of line
:1
- goes to top of file
:5
- goes to fifth line of file
:$
- goes to bottom of file
:set nu - will number all your lines
:set nonu - turn off line numbering
Ctrl-g - show line number of current line
Brief Introduction to Nano Editor
• To edit a file called filename, type nano filename.
• cntrl g - (^G) with display a Help file with a bunch of
information about using nano.
• cntrl o - (^O) or (f3) will write or save the file
• cntrl x - (^X) will exit the program and return you to the
prompt
• cntrl d - (^D) delete character currently under the cursor
• cntrl k - (^K) delete entire line
• cntrl u - (^U) paste text
• ^\
- search for (and replace) a string of characters
• BackSpace delete character currently in front of the cursor
Compiling files in linux
• C++ file
g++ [options] source_file –o outputfile
g++ myfile.cpp
g++ -Wall myfile.cpp -o outputfile
• C file
gcc [options] source_file –o outputfile
gcc myfile.c
• java file
javac Myfile.java
java Myfile
Debuggin c/c++ file
• gdb outputfile
• To set the break
• (gdb) break filename:line number
• (gdb) functionname
• To remove the break
• (gdb) clear function /line number
• To run
• (gdb) r
• (gdb) step (each step)
• (gdb) next [count] // how many line
Debuggin c/c++ file
• To watch the variables
• (gdb) watch variablename
• (gdb) watch expression
• To quit
• (gdb) q
Thank You !!!