Lecture 4 Linux Basic Commands
Lecture 4 Linux Basic Commands
Class Meeting #
Slide : 1
WARNING!
Class Meeting #
Slide : 2
Logging In As Root
Regardless of whether you ve chosen a graphical or console login screen, you ll have to supply a login account name and the password associated with that account. From a console screen, for example, you ll see something like:
Red Hat Linux release 9.0 Kernel 2. xx on an i686 localhost login:root Password: yourrootpassword
Linux Basic Commands Class Meeting # Slide : 3
Shell Prompt
Now that you have logged in, you will see a shell prompt.
[root@localhost /root]#
This is where you will spend most of your time as system administrator.
Class Meeting #
Slide : 4
[Ctrl]-[D]
Or just type logout at the prompt:
System Shutdown
The Need To Shutdown
The Linux operating system keeps the more current versions of the "table of contents", or inode table, in memory to speed disk access. If the system is not shutdown properly the inode table stored in memory is not written to the disk so the table of contents will not be correct and files will be lost. Never, under any circumstances, shutdown your Linux system simply by pressing the power button
Class Meeting #
Slide : 6
Shutting down in this matter will forcibly log off any other users who will lose whatever their working on
Class Meeting #
Slide : 7
Class Meeting #
Slide : 8
halt Command
#halt
Since they are based on the UNIX operating system, some versions of Linux allow you to use the commands "fasthalt" or "haltsys" to immediately bring the system down in a safe and orderly fashion.
Class Meeting #
Slide : 9
Class Meeting #
Slide : 10
WARNING!
Make certain that you ve saved your work before halting or restarting your system from the shell prompt. Running applications will be closed and you won t have the option of saving your work or your session.
Class Meeting #
Slide : 11
Class Meeting #
Slide : 12
[root@localhost /root]#ls If there were files, you would see their names listed in columns with no indication of what they are for.
To see a hidden file you have to use the command
[root@localhost /root]#ls -a
Another variant ls -l which lists the contents in long format.
[root@localhost /root]#ls -l
Linux Basic Commands Class Meeting # Slide : 13
ls (cont.)
They can be strung together in any way that is convenient for example ls -a -l, ls -l -a or ls -al | either of these will list all files in long format. [root@localhost /root]#ls a -l [root@localhost /root]#ls l -a [root@localhost /root]#ls -al
Class Meeting #
Slide : 15
pico -- joe
Class Meeting #
Slide : 16
joe Text Editor The ^ represents the control Key Use ^KH to get help then esc and , and . to back and forward respectively.
Class Meeting #
Slide : 17
EXIT ^KX Save and Exit ^C Abort ^KZ Shell FILE ^KE Edit ^KR Insert ^KD Save
Class Meeting #
Slide : 18
SEARCH
^KF find Text ^L Find Next
Class Meeting #
Slide : 19
GO TO
^U Prev. Screen ^V Next Screen ^A Beginning of line ^E End of Line ^KU Top of file ^KV End of File ^KL To Line Number
Linux Basic Commands Class Meeting # Slide : 20
BLOCK
^KB Begin ^KK End ^KM Move ^KC Copy ^KW File ^KY Delete ^K/ Filter
Linux Basic Commands Class Meeting # Slide : 21
SPELL
Class Meeting #
Slide : 22
You can also type info <command> for help on many basic commands. Some packages will however not have info pages.
Class Meeting #
Slide : 24
Manipulating directories
cd
change directories
#cd directory1/directory2
And similarly you can get back to where you were with
#cd ..
By simply typing cd you get back to your home directory no matter where ever you are
#cd
Linux Basic Commands Class Meeting # Slide : 25
# ls | more
Class Meeting #
Slide : 26
#ls | less
Class Meeting #
Slide : 27
Class Meeting #
Slide : 28
Directories [pwd]
The command pwd stands for present working directory (also called the current directory) and is used to tell you what directory you are currently in.
#pwd
Class Meeting #
Slide : 29
Directories [rmdir]
rmdir Remove empty directories
#rmdir p dir1/dir2/dir3
Class Meeting #
Slide : 30
Directories [rm]
rm Remove files
#rm rf filename #rm rf directoryname
Both commands are dangerous to use as a root user
Class Meeting #
Slide : 31
# cp myfile newfile
Otherwise, you can enter the full path name, like
# cp /home/jack/myfile /home/jack/newfile
Class Meeting #
Slide : 32
(Relative path)
Class Meeting #
Slide : 33
Directories [cp]
SYNOPSIS cp [options] source dest OPTIONS P, parents the command cp parents a/b/c existing_dir copies the file a/b/c to existing_dir/a/b/c, creating any missing intermediate directories. p, preserve Preserve the original files owner, group, permissions, and timestamps. Copy directories recursively, copying all non directories as if they were regular files.
Class Meeting # Slide : 34
-r
Linux Basic Commands
Basics of Wildcards
Wildcards * ? [] Example Jo* Jo*y UL*l*s.c ?.h Meaning Match any sequence of one or more characters. Match any single character. Match one of the enclosed characters or range. Meaning Files that begin with Jo Files that begin Jo and end in y Files that begin with UL, contain an l and end in s.c All files that begin with a single character followed by .h
Class Meeting # Slide : 35
ls [A-M]* list the files that begin with A through M [a-dJW-Y]* matches all files beginning with a, b, c, d, J, W, X or Y. *[a-d]id matches all files ending with aid, bid, cid or did.
Class Meeting #
Slide : 36
Class Meeting #
Slide : 37
bc
A calculator program that handles arbitrary precision (very large) numbers. It is useful for doing any kind of calculation on the command line. It use is left as an exercise.
[root@localhost /root]# bc
Class Meeting #
Slide : 38
Class Meeting #
Slide : 39
whoami
Prints out your login name. [root@localhost /root]#whoami
Class Meeting #
Slide : 40
date
Prints out the current date and time.
[root@localhost /root]#date
Class Meeting #
Slide : 41
[root@localhost /root]# df -h
Class Meeting #
Slide : 42
Class Meeting #
Slide : 43
uname
Prints out the name of the Unix operating system you are currently using.
Class Meeting #
Slide : 44
wc
wc [-c] [-w] [-l] <filename>
Counts the number characters/bytes (with -c), words (with -w) or lines (with-l) in a file. [root@localhost /root]# wc c /etc/passwd
Class Meeting #
Slide : 45
But cat has just demonstrated the definition of standard input and standard output. Your input was read from the keyboard (standard input), and that input was then directed to your terminal (standard output).
Class Meeting #
Slide : 47
Using Redirection
Redirection means causing the shell to change what it considers standard input or where the standard output is going. To redirect standard output, we ll use the > symbol. Placing > after the cat command
Class Meeting #
Slide : 48
Class Meeting #
Slide : 49
Caution
You can easily overwrite an existing file! Make sure the name of the file you re creating doesn t match the name of a preexisting file, unless you want to replace it.
Class Meeting #
Slide : 50
Exercise
Create another file named home.txt having the following contents bring the coffee home take off shoes put on sneakers make some coffee relax! Check the file using cat command ?
Class Meeting #
Slide : 51
Now it s time to check our handiwork. Type: [newuser@localhost /newuser]# cat myfile
Class Meeting #
Slide : 52
#cat sneakers.txt
Class Meeting #
Slide : 53
Class Meeting #
Slide : 54
Type
$ date > date.dat $ cat date.dat $ ls > list.dat $ cat list.dat
Class Meeting #
Slide : 56
touch command
This command updates the timestamp of a file or directory. If the named file does not exists, it will be created empty.
Class Meeting #
Slide : 57
# file portrate.jpg
will tell you that portrate.jpg is a JPEG image data,
Class Meeting #
Slide : 58
#stat /tmp
Class Meeting #
Slide : 59
which command
To locate the exact path of a program, you can use the which command Type
#which httpd
Class Meeting #
Slide : 60
cmp
Syntax: cmp [-Is] file1 file2 This command will compare the contents of two files. If there are no differences within the two files, cmp by default will be silent.
Class Meeting #
Slide : 61
cmp
To demonstrate, filel.txt contains: this is file 1 the quick brown fox jumps over the lazy dog.
and file2.txt contains: this is file 2 the quick brown fox jumps over the lazy dog.
Class Meeting #
Slide : 62
Demonstration of cmp
[root@localhost /root]# cmp file1.txt file2.txt file1.txt file2.txt differ: char 14, line 1
The results of cmp correctly identify character 14, line 1 as the unequal character between the two files.
Class Meeting #
Slide : 63
head
Syntax: head [-count | -n number] filename This command will display the first few lines of a file. By default, the first 10 lines of a file are displayed. However, you could use the preceding options to specify a different number of lines.
Class Meeting #
Slide : 64
tail
Class Meeting #
Slide : 65
locate
An alternative command is locate <filename>. This searches through a previously created database of all the files on the system, and hence finds files instantaneously. Its counterpart updatedb is used to update the database of files used by locate. On some systems updatedb runs automatically every day at 04h00.
Class Meeting #
Slide : 66
grep
[root@localhost /root]# grep [-viw] pattern file(s)
The grep command allows you to search for one or more files for particular character patterns. Every line of each file that contains the pattern is displayed at the terminal. The grep command is useful when you have lots of files and you want to find out which ones contain words or phrases.
Class Meeting #
Slide : 67
grep
Using the option, we can display the inverse of a pattern. Perhaps we want to select the lines in data.txt that do not contain the word "the": If the option was not specified, then any word containing "the" would match, like "toge[the]r." The -w option specifies that the pattern must be a whole word. [root@localhost /root]# grep -vw 'the' data.txt And finally, the option ignores the difference between upper and lowercase letters when searching for the pattern.
Linux Basic Commands Class Meeting # Slide : 68
-v
-w
-i
Change to the root directory, and enter find. find will work for a long time if you enter it as you have press Ctrl-C to stop it. Now change back to your home directory and type find again. You will see all your personal files.
Class Meeting #
Slide : 69
Class Meeting #
Slide : 70
find (cont.)
find -name <filename> will find only files that have the name <filename>. For instance, find -name '*.c Will find all files that end in a .c extension without the quote characters will not work. find -name Mary Jones.letter will find the file with the name Mary Jones.letter. find -size [[+|-]]<size> will find only files that have a size larger (for +) or smaller (for -) than <size> kilobytes, or the same as <size> kilobytes if the sign is not specified.
Class Meeting #
Slide : 71
Try this
find / -name shutdown print find / -name core type f ok rm {} \; (remove file during search)
find / -name passwd type f ok cp {} /root \; (copy file during search) find . name *.gif atime 1 exec ls l {} \; (find all files that have been accessed during past 24 hrs) find . type f empty (This displays all empty files in the current directory)
Class Meeting # Slide : 72
Exercise:
Class Meeting #
Slide : 73
Homework
Homework this week: Next week:
Class Meeting #
Slide : 74