Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, May 11, 2013

Quick Linux (7): Little VIM

I didn't read the whole chapter, I just focused on few things I need right now:


Open VIM:
$vim +n filename        #Edits filename starting at line n
$vim + filename         #Edits filename starting at the last line
$vim +/pattern filename #Edits filename starting at the first line containing pattern
vim –r filename         #Recovers filename after a system crash
$vim –R filename        #Edits filename readonly (same as opening the file with view

Adding text:
i #before cursor 
I #before first non-blank character
a #after cursor
A #at the end of line
o #adds line below current line
O #line above current line
r #replaces current character
R #replaces characters starting with current character

Friday, May 10, 2013

Quick Linux (6): Shell (Overview)


Chapter 5: The Shell
ls -- -l #-- indicates the end of options
PATH = $PATH:. #adds current working directory to the path

s234-155:~ nemo$ who
nemo     console  May 10 17:55 
nemo     ttys000  May 10 21:41 

$ who #The device name that's displayed after your username is the filename of the screen and keyboard

$echo hello > t.txt #If the file exists, the shell will overwrite it

$echo hello >> t.txt #The append output symbol (>>) appends a file 

* The /dev/null device is a data sink, "bit bucket". You can redirect output that you do not want to keep or see to /dev/null

$ who | tee who.out | grep nemo #use tee to redirect output to both a file and standard output

$ jobs 

* To run a job in the background use & at the end of a command or bg

* To bring a job back to the foreground: type fg or % followed by the job number
to get the current running jobs type

* To kill a job $kill PID (process number)

[Wildcards]
? #generates filenames that have "tes" followed by a single character
$ ls test.tx?
test.txt

* #matches any number of characters unlike ? 
$ ls te*
test.txt

[] #match filenames containing the individual characters, use hyphen for range
s234-155:Linux nemo$ ls
another hello.txt linktest test.txt
s234-155:Linux nemo$ echo [hl]*
hello.txt linktest
s234-155:Linux nemo$ echo [a-f]*
another

special cases: [] with ! and ^
[!a-f]* #means not to include the characters at the beginning 
*[^ab] #don't end with those characters


[Builtins] 
(to list them $info bash, Shell Builtin Commands)
The shell does not fork a new process when running a builtin. 

Thursday, May 9, 2013

Quick Linux (5): File System (Permissions (2) / Links)


SetUID & SetGID: 
SetUID and SetGID gives the process owner's permissions and group's permissions 
chmod u+s test.txt #gives the program both my permissions and the group's permissions
-rw-r--r--  1 nemo  staff    0 May  8 14:56 test.txt
-rwSr--r--  1 nemo  staff    0 May  8 14:56 test.txt

Directory Access Permissions: 
ls -ld #display directories only
execute (can only use ls to list a file if filename is known) 
read (list dir contents with ls)

ACLs: Access Control Lists:
Fine grained access, not efficient. to read more (page 116)

Links:
A pointer to a file:
Hard Links: (old way)
file appears as another file with a different name if in the same directory
ln existing file newline #command to create a hard link
s234-80:Linux nemo$ ls -i #the i option shows the inode number is the same
4647467 another 4648460 linktest 4648460 test.txt

Soft Links (Symbolic Links):
- You can create a link to a directory unlike with hard links
- A symbolic link can point to any file, regardless its location but a hard link to a file must be in the same filesystem
- Can point to a non-existent file, if created again, it will point to the new one unlike hard links. 
- The size and times of the last modification are not the same with symbolic links unlike hard links.
- use the option ––symbolic or -s for symbolic with ln.
- Use absolute pathnames with symbolic links.


Wednesday, May 8, 2013

Quick Linux (4): File System (Important Directors, Permissions (1))


File System
ls - a #hidden files (shell config files in home, files starting with dot)
ls . or .. #. references working directory and .. parent
pwd #working directory 

less ~/.bashrc #tilde references home directory
less ~sam/.bashrc #references sam's home directory if you have access

mkdir -p #create a directory, -p to create multiple dirs in one command 
rmdir -f #removes dir, -f recursively delete sub directories and files

mv file subdir/ #move file from one dir to another
mv dir anotherdir #move one dir to another

Important Directories:
/               Root
/root           Root home
/bin            Command binaries
/sbin           Essential system binaries (utilities for system administration)
/boot           Static files of the boot loader
/dev            Device files with udev containing active devices
/home           Users home directories (MAC OS X uses /Users)
/lib            Shared libraries 
/lib/modules    Loadable kernal modules
/mnt            Mount point for temporarily mounting filesystems
/opt            Optional software
/sys            Device pesudofilesystem
/tmp            Temporary files

etc:
/etc            Machine-local system configuration files (includes /etc/passwd)
/etc/opt        Configuration files for add-on software packages kept in /opt
/etc/X11        Machine-local system configuration files for X Windows System

usr:
/usr            Includes subdirectories that contain information used by the system 
/usr/bin        Standard utilities not used in single user mode or recovery?
/usr/games      Games
/usr/lib        Libraries
/usr/local      Added files to system? what about /opt?
/usr/sbin       Mon-essential binaries 
/usr/share      architecture independent data
/usr/share/doc  documentation
/usr/share/info GNU info
/usr/share/man  Online manuals
/usr/src        Source Code

var:
/var            Variable data (log files)
/var/log        Log files
/var/spool      Spooled application data 


Access Permissions:
ls -l #display file permissions

Example: 
drwxr-xr-x  3 nemo  staff  102 May  8 14:23 another
-rw-r--r--  1 nemo  staff    0 May  8 14:56 test.txt

from left to right
1) type of file (- file, d dir)
2) access (owner/group/others)
3) ACL flag? (null in the example)
4) links to the file (1)
5) owner (nemo)
6) file group (staff)
7) size in bytes (0)
8) date created/modified (May  8 14:56)
9) name of the file (test.txt)

chmod #change access permission: a (all users) o (other users) g (group) u (user/owner) r (read) x (execute) w (write) +- (add/remove)

You can use numerics instead, 4 (read), 2 (write), 1 (execute) 
chmod 755 file #gives all to owner, read/execute to the rest

Monday, May 6, 2013

Quick Linux (3): Compressing/Locating/Users

Compressing Commands:

rm -rf #remove folder 

bzip2 file #compress (-v to report reduction size) (-k to keep original)
bunzip file.bz2 #decompress
bzcat file.bz2 #view compressed file

gzip file #compress (not to be confused with zip which archives files)
gunzip file #decompress

tar #packs and unpacks archives (-t view -x extract (overwrites files with same filenames) -c create, -v verbose, -f (read or write from file)

Locating Commands: 
whereis echo #location of a command using standard directories ind of search path
which echo #location using the search path
type echo #whether a command is a builtin
slocate file #searches for a file, needs to be installed


User/System Information: 
who #lists users 
who am i #
finger #lists users more info might be disabled
w #lists users
write user #sending msg to user

Sunday, May 5, 2013

Quick Linux (2): Files

Files
rm -i #rm waits for a response first

cat file.txt #content of file

less #content of the file with "end"

more #content of the file without "end"

hostname #hostname!

cp #copy from source-file to destination-file, If the destination-file exists before you give a cp command, cp 
overwrites it.
cp -i #gives a warning if the file exist

mv #from existing-filename new-filename, also destroys existing files. use it to rename
mv -i $with warning 

lpr #printing

grep 'string' file #searches file for string (more on grep later)

head file #displays beginning of a file

tail -5 file #displays last 5 lines of a file

sort #displays file in order without changing it -u (unique lines only) -n (numerical order)

uniq #displays with duplicates removed

diff #comparing two files (try with -u)


file #look at file properties

date #date 

script #record session followed by exit to stop recording


Saturday, May 4, 2013

Quick Linux (1)


Book: A Practical Guide to Linux Commands Editors and Shell Programming 2nd Edition

1- CTRL+W  #delete a word
2- CTRL+U  #delete a line
3- sudo -i #spawns a new shell running with root
4- man 5 passwd #search only section 5 (SPACE to scroll, q to quit)
5- apropos who #searches keywords
6- whatis who #searches complete word matches
7-  info #more info

Sunday, April 14, 2013

Comparing two files

diff source_file dest_file
cmp source_file dest_file


Example: 
file1:
1
2
3

file2:
2
4


s234-80:Desktop nemo$ cmp file1.txt file2.txt 
file1.txt file2.txt differ: char 1, line 1
s234-80:Desktop nemo$ diff file1.txt file2.txt 
1d0
< 1
3c2
< 3
---
> 4
\ No newline at end of file
s234-80:Desktop nemo$