Richard's Linux Ch 4, 5

=================
Linux is case sensitive
=================

Ch 4. Filesystem

Filesystem - a set of data structures that holds directories of files.

Linux file system is a tree.

Filenames - skip

Filename extensions - skip

Hidden Files
$ ls -a --- list hidden files

Working directory
$ pwd --- print working directory
Startup files in home directory --- skip-

Absolute Pathnames
$ ls /home > tempfile
$ ls /usr/bin
$ ls ~/ --- your home directory
$ ls ~/.. --- one level up

Directory
$ mkdir literature
$ ls -F --- show a / at the back of each directory name
$ ls literature --- list content in the directory literature
$ ls -l literature ---
$ mkdir literature/promo
$ ls literature
$ cd literature
$ cd .. ---- one level up
$ ls . ---- current working directory, one dot
$ ls .. ---- parent directory
$ rmdir ./literature/promo ---- rm utility has a -r option, can recursively deletes files, including directories
$ rmdir literature

$ mv existing-file-list directory --- move files from one directory to another directory
$ mkdir literature
$ mv practice literature --- move file practice to literature directory
$ ls literature
$ rm -r literature --- this recursive delete is very powerful, be careful when using it

$ mv existing-directory-list new-directory ---- mv directories

Important Standard Directories and Files --- skipping
$ ls -l / ---- try this, skipping the rest, read the book

Access Permissions
$ ls -l ------ displays permissions
File Access Permissions --- Owner / Group / Other (Everyone) --- rwx (read/write/exe)

chmod: Change Access Permissions u for owner, g for group, o for other, a for all
$ ls -l practice2
-rw-r--r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod a+rw practice2
----- add read/write to all (a)
$ ls -l practice2
-rw-rw-rw- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod o-w practice2
--- remove w from others (o)
$ ls -l practice2

-rw-rw-r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod g-w practice2
----- removes w from group (g)
$ ls -l practice2
-rw-r--r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod u+x practice2
----- add exe to owner (u)
$ ls -l practice2
-rwxr--r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod u-x practice2
$ ls -l practice2

-rw-r--r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod u+rx practice2
$ ls -l practice2

-rwxr--r-- 1 leel faculty 749 Aug 22 00:04 practice2

Setuid and Setgid permissions
Execute a file with setuid permission, the process takes on the priviledge of the file's owner. (be careful, eg if owner is root) Similarly for setgid -> files' group.
p
$ chmod u+s practice2
$ ls -l practice2

-rwsr--r-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod g+s practice2
$ ls -l practice2

-rwsr-Sr-- 1 leel faculty 749 Aug 22 00:04 practice2

$ chmod ug-s practice2
$ ls -l practice2

-rwxr--r-- 1 leel faculty 749 Aug 22 00:04 practice2

 

Skipping the following topics on Ch 4:

Numeric Arguments to chmod

Directory Access Permissions

Access Rules

Setting Default Rules for Directory

Links (hard links & symbolic links)

 

Ch 5. The Shell

$ ps
PID TTY TIME CMD
19345 pts/1 00:00:00 bash
22575 pts/1 00:00:00 ps

--- So we are using bash shell

Command Line Syntax
command [arg1] [arg2] ... [argn] return

Arguments

e.g.
$ cp practice2 practice

Options - option is an argument that modifies the effect of a command
$ ls
$ ls -r --- reverse
$ ls -x --- horizontal sorted row
$ ls -rx --- combined r & x
$ ls -rl --- combine r & l

-- indicates end of option
$ ls -- mymemo1
$ ls -- -l -------- for a file with name -l

--help option --- need help
$ ls --help

Name of program can appear anywhere
$ cat mymemo1
$ >mymemo2 <mymemo1 cat --- input = mymemo1; output = mymemo2
$ ls mymemo2
$ cat mymemo2

PATH --- skipping, but need to know the concept
When you give a simple filename of a program, the shell searches through a list of directories for a filename that matches the specific name and for which you have execute permission. Ones specified by the variable named PATH.

Standard input and standard output
Standard ouput = place to send information = default is screen
Standard input = place to get information = default is keyboard
$ who --- returns filename of the screen and keyboard, let's say pts/2, that means your screen and keyboard is /dev/pts/2
e.g. cat --- concatenate file(s) or standard input to standard output
$ cat memo > /dev/pts/2 ---- output memo to standard output (screen)
$ cat < /dev/pts/2 ----- get input from standard input, same as cat
CONTROL-D to get out

Redirecting standard output (useful for programming e.g. Perl)
command [arguments] > filename
$ cat > sample.txt
$ cat sample.txt


$ cat memo
$ cat mymemo1
$ cat mymemo2
$ cat memo mymemo1 mymemo2 > memo3
---- combining 3 files

Redirecting standard input
command [arguments] < filename
$ cat < memo3

noclobber: Avoids Overwriting files --- skipping
$ set -o noclobber --- cannot overwrite exisiting file
$ set +o noclobber --- can overwrite existing file

Appending standard output to a file (useful for programming, e.g. Perl)
Append output symbol >>
$ cat memo
$ cat mymemo1
$ cat memo >> mymemo1
---- append memo at the back of mymemo1

/dev/null: making data disappear --- data sink (bit bucket)
$ echo "hi there" > /dev/null --- it is gone

$ cp memo memo10
$ cat memo10
$ cat /dev/null > memo10
---- read from data sink, get null string
$ cat memo10 --- nothing left in file
$ ls -l memo10 --- size should be 0

Pipes - use a pipe to connect standard output of one command to the standard input of another command
command_a [arguments] | command_b [arguments]
is same as
command_a [arguments] > temp
command_b [arguments] < temp
rm temp

e.g. tr string1 string2 --- tr accepts input from standard input, looks for characters that match one of the characters in string1, translates the matched character in string1 to the corresponding character in string2
$ tr abc ABC
aaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbccccccccccccc
------ user input
AAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBCCCCCCCCCCCCC ----- translate output

$ cat memo | tr abc ABC ---- change abc to ABC in output, but not the original file memo
$ tr abc ABC < memo ---- same effect as above

$ who
$ who > temp ---- technique to use temporary file to sort temporary results
$ sort < temp
$ rm temp

same as
$ who | sort

tee utility copies its standard input both to a file and to standard output
$ who | tee who.out | grep lee ---- grep myself
$ cat who.out ---- shows everybody

Running a Command in the Background
You don't to have to wait for commands to finish before running another command. => Multitasking
Job - series of one more more commands.
Use (&) before return, record PID (process indetification number).
e.g.
$ perl infinite_loop.pl &
[1] 17125

check job PID
$ top --------q or ctrl-c to quit, or...
$ ps -fu leel ------------ reporting process status, -f: does full listing, -u
$ kill 17125 --------kill job

Move a foreground job to background
ctrl - Z ---- suspend job
$ bg
Enter

Special characters ? - single character
$ ls mymemo?
$ ls ??me???

Special character * - zero or more characters
$ ls *me*
$ echo memo*

[ ] Special Characters - match filenames containing the individual characters.
$ echo mymemo[12]
$ ls echo [aeiou]*


Something about echo and ls:
echo is not about displaying file names. It is just displaying a string that it receives.
ls displays filenames directories etc.
echo receives string(s), and it send that screen to stdout. The screen matches a list of files is due to preliminary expansion that the shell does before passing the arguments to echo. It is coincidental."

Builtins - utility (command) that is built into shell, to display list of shell builtins:
$ info bash ------------ select Shell Builtin Commands > Bourne Shell Builtins



Extra:
To check memory size and processes:
$ top

To check free harddisk space
$ df -h
or
$ df -k