unix 是一个开放的系统。伯克利大学对unix进行了修改,制造了BSD( Berkeley Software Distribution ), 支持商业用途,许多商业公司使用了bsd进行修改,然后产生自己的系统。比如苹果的OSX.

而Linux是另一个类unix系统,试图与unix兼容, 要求完全开源。

Important Directories

/是根目录,所有的目录,文件和设备都在/下面

/bin下存有linux常用命令,以二进制文件的形式存在, bin是binary的缩写, [不同bin目录间的区别][1]

/boot目录用来存放Linux的内核

/cdrom目录用来挂载光驱文件系统, 你可以使用mount /path/命令来进行相关挂载操作(我不太理解手动mount的作用)

/dev目录下包含了所有linux系统使用的外部设备, devdevice的缩写

/etc用来存放各类系统管理的配置文件, etcet cetera的缩写,

/home/用来存放各位用户的数据,如你建立一个叫Marquez的用户,那么你的用户主目录就是/home/Mario/

/lib文件夹下存放有一些.so文件,这些文件的作用和windows下的.dll文件效果很像, 他们是各类程序,包括kernel程序在运行时需要调用的库, liblibrary的缩写

/lost+found是用来存放机器意外关机或系统崩溃时的文件碎片的

/mnt目录用来存放挂载式储存设备的挂载目录,比如cdrom等目录,mntmount的缩写

/media目录被有些发行版用来挂载带usb接口的移动硬盘,比如U盘,移动硬盘等

/opt文件夹里存有你的增强性program,optoption的缩写。

/proc文件夹里会存有各种系统信息,包括cpu信息,进程信息等, procprocess的缩写。

/selinux 用来存放selinux的相关文件,selinuxsecure linux的意思,用于安全增强。此外,在/etc/selinux/存有该program的配置文件

/srv文件夹用来存放你建立网络服务时所提供的各类数据,如www服务读取的数据可以放在/srv/www中, srvserve的缩写

/tmp用来存放程序运行时产生的临时文件, (据说里面产生的临时文件重启后会消失,待我实验一下), 实验结果: 重启后,虚拟机里的Ubuntu自己写的zen_log消失, OSX x下/tmp里的zen_log仍存在。

/usr文件夹用来存放不适合放在其他文件夹的额外的工具,是是linux系统中占用硬盘空间最大的目录,Ubuntu系统的游戏也放在此文件夹下
用户可用的程序和数据都会放在此文件夹下

/usr/share文件夹会用来存放shareble architecture-independent files, 这里的shareble可以理解为能用在众多地方的program和文件,比如vim, java, php, zsh, screen, doc等, 此文件夹里存放的program最好是不需要被修改的。

/usr/local文件夹用来存放用户在本地自己安装的program, 它的文件夹结构和/usr很像,只不过/usr/localrange更窄一点。
比如vim存放在/usr/share下,而w3m存放在/usr/local/share下, opensslperl存放在/usr/bin/下,git存放在/usr/local/bin
或许我们可以这样理解,/usr下分作两个区域,/usr/local用来存放所有用户自行安装的和小范围应用的program及其data, 而/usr/local以外的区域主要用来存放预置的和全局性的program.
同时/usr/local//usr的结构是一样的,就像在主git文件夹下再开了一个sub-git directory

/var文件夹会存放一些经常变动的文件,如/var/log里会存有各类日志,varvary的缩写

/root文件夹则是root用户的主目录

/sbin则是用来存放系统管理员的系统管理程序的目录。这个里面的命令,必须有root权限才能执行。

File Mechanism

On a UNIX system( Linux also ), everything is a file; if something is not a file, it is a process.

A Linux system, just like UNIX, makes no difference between a file and a directory, since a directory is just a file containing names of other files.

Below is the file type chart of Linux:

Symbol    Meaning
-       Regular file
d       Directory
l       Link
c       Special file
s       Socket          ?
p       Named pipe      ?
b       Block device    ?

In a Unix-style file system, an index node, informally refered to as an inode, is a data structure used to represent a filesystem object, which can be one of various things including a file or a directory. Each inode stores the attributes and disk block location(s) of the filesystem object's data. Filesystem object attributes may include manipulation metadata (e.g. change, access, modify time), as well as owner and permission data (e.g. group-id, user-id, permissions)

An inode is identified by an integer number, often referred to as an i-number or inode number. On many types of file system implementations, the maximum number of inodes is fixed at file system creation, limiting the maximum number of files the file system can hold. A typical allocation heuristic for inodes in a file system is one percent of total size.

A Linux directory lists other filesystem objects by name, noramally identifying the listed object by referring to its inode. The directory contains an entry for itself, its parent, and each of its children.

A file in the file system is basically a link to an inode.

A hard link then just creats another file with a link to the same underlying inode. When you delete a file it removes one link to the underlying inode. The inode is only deleted (or deletable/over-writable) when all links to the inode have been deleted.

A symbolic link(soft-link) is a link to another name in the file system. It can across filesystems, while hard-link can't.

FIFO

FIFO stands for First In, First Out, and has another name named pipe, it enables different processes to commnunicate. First, you use mkfifo magic_pipe to create a fifo file. Then you open two terminals, typed in each of them ls -lhrt > magic_pipe and cat < magic_pipe. You'll see magic happens, and you can even reverse their order, first type cat < magic_pipe, then ls -lhrt > magic_pipe. Still works! It's thrilling.

Output Redirection

Bash / ksh and other modern shell on Linux has three file descriptors:

  stdin (0)
  stdout (1)
  stderr (2)

Syntax To redirect all output to file

The syntax is as follows to redirect output (stdout) as follows:

command-name >  output.txt
command-name >  stdout.txt

Syntax To redirect all error to file

The syntax is as follows to redirect errors (stderr) as follows:

command-name 2> errors.txt
command-name 2> stderr.txt

Syntax to redirect both output (stdout) and errors (stderr) to different files

The syntax:

command1 > out.txt 2> err.txt
command2 -f -z -y > out.txt 2> err.txt

Syntax to redirect both output (stdout) and errors (stderr) to same file

The syntax is:

command1 > everything.txt 2>&1
command1 -arg > everything.txt 2>&1

Syntax to redirect errors (stderr) to null or zero devices

Data written to a null or zero special file is discarded by your system. This is useful to silence out errors (also know as 'error spam'):

command1 2> /dev/null
command1 2> /dev/zero
command2 -arg 2> /dev/null
command2 -arg 2> /dev/zero

Tip: Use tee command to redirect to both a file and the screen same time

The syntax is:

command1 |& tee log.txt

OR

command1 -arg |& tee log.txt

OR

command1 2>&1 | tee log.txt

Another usage:

#!/bin/bash
# My script to do blah ...
foo(){
 :
} 2>&1 | tee foo.log

OR

#!/bin/bash
# My script to do blah ...
{
   command1
   command2
} 2>&1 | tee script.log

results matching ""

    No results matching ""