Vous êtes sur la page 1sur 8

=Unix for Mac OSX

1 - Introduction to UNIX.
Developed by AT&T employees at Bell Labs (1969-1971)
Employees worked by themselves on smaller project named UNICS ( Uniplexed
Information and Computing Service)
Renamed UNIX after become multiuser.

- System 5 first public release of linux.

- Branches and improvements (1977 - present)


- Open source: BSD (Berkeley Software Distribution), Lunix
- Closed source: Solaris (Sun/Oracle), AIX (IBM), HP/UX (Hewlett-Packard)
- Mixed source: Mac OSX (Apple)

- Kernels and Shells:


-sh: Thompson Shell
-sh: Bourne Shell (1977)
-csh: C Shell
-tcsh: Tabbed C Shell
-ksh: Korn Shell
-bash: Bourne-Again Shell
-zsh: Z Shell

echo $0 : Display current working shell. Not the starting one, current one.
man : keyword access manual for commands. Ex: man echo
man -k or apropos : look for keyword commands and description.

2 - Filesystem Basiscs
- pwd: show present working directory. Display current working directory.
- ls: list directory structure.
- ls -lah: ls with optional listings.
- cd : navigate to folder
- cd .. : return to parent directory.
- cd ../.. : move back directories. Back one them back the second.
- cd - : switch back to last directory.

Organization of unix hard drive file structure:

/ : Root
/bin : Binaries, programs
/sbin : System binaries, system programs
/dev : Devices: hard drivers, keyboard, mouse, etc.
/etc : System configurations
/home : User home directories
/lib : Libraries of code
/tmp : Temporary files
/var : Various, mostly foes the system uses

/usr
/usr/bin
/usr/etc
/usr/lib
/usr/local : User Programs, tools and libraries (not files)

Organization of Mac OS X Only File structure:


/Applications : Mac programs
/Library : Mac libraries of code
/Network : Networked devices
/System : Mac OSX
/Users : User home directories
/Volumes : Mounted volumes (hard drive, dvd, ipod)
.DS_Store : Hold folder view options, icons positions
~/.MacOSX : Directory for Mac OS X to store options
~/.Trash : User trash can
~/.hotfiles.btree : Track commonly-used files for optimization
~/.Spotlight-V100 Used by Spotlight for indexing

3 - Working with files and Directories

Prevent spaces when naming files or directories.

- touch : create a file


- nano : good linux text editor. Replaces VI.
- cat : read or concatenate content of file. If it just one file is given as
argument it just read one file. Only problem is when have long file
- more : paginated output. Cannot go back on the page.
- less : ability to scroll backwards
- head : display hear portion of the text document.
- tail : display lines from the end of file
- tail -f : display loses from the end of file and follow updates
- mkdir : create directory. -vp return verbose mode.
- mv : move file
- mv -n : no overwriting
- mv -f : force overwriting
- mv -I : interactive overwriting, “ask me”
- mv -v : verbose
- rm : remove files
- rmdir : remove only empty directories
- rm -R : remove files and directories recursively
- rm -Rv: remove file and directories recursively with verbose mode
- ln : creates a hard link to file. Expression is ln fileToLink hardLink.
Keeps track of the file in the hard drive.
- ln -s : creates a symbolic link. Keep track of path to the file.
- find <location> -name <filename.something>: search files and folders.

- WILDCARD CHARACTERS:
* zero or more characters (glob)
? any other character
[ ] : any character in the brackets.
- find ~ -name *.??? or find / -name ????.** or find / -name index[123].hmtl
look for name index combined with any of characters inside brackets.

- find / -name *.??? -and -not -path /Users/

4 - File Ownership and Privileges

- echo $HOME : output user home directory


- id -p : display groups who user belong to
- groups : display unix groups
- chown : change ownership. Ex: chown admin:staff filename
- chown -R: recursive ownership. Used for folders.
File permissions: Alpha Notation.

user group other


—————————————
read (r) yes yes yes
write (w) yes yes no
execute (x) yes no no

rwx rw- r— “9 caracter string”

rwx-rw—r—

Categories: User, Group, Others

Read ( see content).


Write (change content file or content of directory).
Execute (run a file or search inside for directory).

* Frequently files has execution off because they are not script. They are
text files.
* By default directories has execute turned on because they can be read and
search.

- chmod : change mode. Usage chmod “mode” “filename”


ugo => user, group, others

Usage:
chmod ugo=rwx “filename”
chmod u=rwx, g=rw-, o=r “filename”
chmod ug+w “filename”
chmod o-w “filename”
chmod ugo+w “filename” is equal to chmod a+w
chmod go-rw filename

Octonal notation

r=4, w=2, x=1

user group other


—————————————
read (r) 4 yes yes yes
write (w) 2 yes yes no
execute (x) 1 yes no no

rwx rw- r— “9 caracter string”

rwx-rw—r—

chmod u=rwx, g=rw-, o=r— same as chmod 764


rwxrw-r— = 764
chmod 777 filename : gives rwx permission for user, groups and others.

sudo = substitute user and do.


sudo -u name substitute the user by given username
cat /etc/sudoers = sudoers is a file store sudo config and who is allowed to
run sudo.

5 - Unix commands and programs:

; (semi-colon) concatenate multiple commands in one line.


echo : output info on terminal
which :
q, x, ctrl+q, ctrl+x or ESC: possible keys to get out of program.
It is possible to add ; (semi-colon) in between commands to execute multiple
commands.
whereis, which, whatis
echo $PATH : display path. PATH=/paths_to_look_for
uptime: display time computer is up
who: shows users logged in.
users: show name of loggedin users
uname -ap: display several informations about Darwin Kernel
df : display disk free space
df -h
du -d 1: display disk utilization and deep to 1 level.
ps: process status. Snapshot of current process. Only process owned by
current user.
ps -a: all process running including the ones not owned but user.
ps aux: list all process and background process.
top: show all current process live and dynamic.
top -n 10 -o cpu -s 2 -U arthur. Display top 10 process for owned by arthur
and sorted by cpu refreshing every 2 seconds. ? can be used to display possible
options.

kill : kill process, kill -9 force to kill process.

Working with text files


wc: word count
sort: sort lines. sort -f treats uppercase and lowercase equaly.
sort -u: sort unique words, taking out the repeated ones.
uniq: filterin/out repeated lines

Usefull unix programs


cal /ncal : calendar. cal 2017
bc: calculator. scale=10 define precision.
expr : expression calculator
units: unit conversion

history: display previews commands list. Usage !-<number of command>.


!<first letters or numbers of command> + enter: will search for last command
using given keywords.
!!: same as !-1, referent to last command
!$: reference to the “argument” of previews commands. Not argument, the
argument.
history -<command number>: removes the command from the history list

6 - Directing input and output

/dev/stdin : keyboard input device


/dev/stdout: display, terminal output default device
command > output file: redirects the output to a file instead display it. ls
-lah > directory.txt.
cat newfile.txt newerfile.txt > joined_file.txt : concatenates both in one
file using > operator.
echo “word” >> textfile.txt : append the file to the end using >> operator.

sort < cars.txt : using the cars.txt as input for sort command.
sort -u cars.txt > sorted_unique_cars.txt : inputs cars list into sort method
and save the return into sorted_unique text file.
Parenthesis is not required because we always evaluate the input first.
< > operators need to be used with file for input or output. it cant be used
for readings.
| (pipe operator) always use output of command and not a file.
cat cars.txt | sort | uniq > sorted_uniq_ cars.txt

Suppressing output: /dev/null


“null device”, “bit bucket”, black hole. Similar to special files /dev/stdin
and /dev/stfout.
Unix discards any data sent there.

7 - Configuring your working environment


Profile, login, and resource files

Upon login at bash shell


/etc/profile
~/.bash_profile, ~/.bash_login, ~/.profile, ~/.login

Upon starting a new bash subshell


~/.bashrc

Upon loggin out of bash shell


~/.bash_logout

Command alias: create alias to commands. Edit .bashrc


alias ll=‘ls -lah’ create a alias to ls -lah just typing ll

alias ll='ls -lahG'


alias home='cd ~'
alias up='cd ..'
alias h='history'
alias mv='mv -i'
alias cp='cp -i'
alias rm='rm -i'
alias df='df -h'
alias du='du -h’
alias mkdir='mkdir -p'
export + command/variable/option will make accessible on application called
by bash.
export LESS=‘-M’
use $LESS to unwrap LESS variable

export PATH=“/usr/local/bin:$PATH” it concatenates custom path with pre-


existent one. It requires duble quotes, otherwise it will be taken as string
literal looking for directory named $PATH.

If you have mySQL installed: export


PATH=“/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH”

Customize history: To clean the history use: history -c

export HISTSIZE=10000 # 500 is default


export HISTFILESIZE=1000000
export HISTTIMEFORMAT='%b %d %I:%M %p ' # using strftime format
export HISTCONTROL=ignoreboth # ignoredups:ignorespace
export HISTIGNORE="history:pwd:exit:df:ls:ls -la:ll"

Customize Command Prompt: PS1=“->”


Formatting codes:
\u (username)
\s (current shell)
\w (current working directory full path)
\W (basename of curent working directory)
\d(date)
\D{format} (date in strftime format (“%Y-%m-%d”)
\A (Time in 24-hour HH:MM format)
\t (time in 24-hour HH:MM:SS format)
\@ (Time in 12-hour HH:MM am/pm format)
\T (Time in 12-hour HH:MM:SS format)
\H (hostname)
\h (hostname up to first “.”
\! (history number of this command)
\$ (when UID is 0 (root), a”#”, otherwise a “$”)
\\ (literal backslash)

8- UNIX POWER TOOLS

GREP: global regular expression print


Unix ed editor: g/re/p

Regular Expression
“regex” for short

grep apple fruit.txt return lines where word apple exists, not the word but
the lines.
grep -i: case insensitive.
grep -c : grep coint
grep -R ./ : recursive search on directory
grep -w : exactly match
grep -v : list what does not match. Inverse of regular search.
grep -n : give match and line numbers
grep -Rl: “l” suppress everything and just show the filename.
grep -RL: “-L” returns filename that does not match.
Example: ps aux | grep Terminal
history | grep nano | less

grep —color “word”: result of grep word will be righted

Edit grep color .bashrc

# GREP_COLOR codes
# Attributes: Text color: Background:
# 0 reset all 30 black 40 black
# 1 bright 31 red 41 red
# 2 dim 32 green 42 green
# 4 underscore 33 yellow 43 yellow
# 5 blink 34 blue 44 blue
# 7 reverse 35 purple 45 purple
# 8 hidden 36 cyan 46 cyan
# 37 white 47 white
# Separate with ";"
#
export GREP_COLOR="34;47"

# Specify options grep should use by default


export GREP_OPTIONS="--color=auto"

source ~/.bashrc: reload .bashrc config

grep —color ‘ea[cp]’ fruits.txt: grep to word with ea with third letter c or
p.

Regular expression Basics

SSH Commands:

ssh-keygen -R server-name
ssh-keygen -R server.ip.addre.ss
ssh-keygen -R 202.54.1.5
ssh-keygen -R server1.example.com

Legacy is disable use: ssh -oHostKeyAlgorithms=+ssh-dss root@xxx.xxx.xxx.xxx

Vous aimerez peut-être aussi