cd //切换到主目录 cd ~ //同上 cd - //回到上一次访问的目录 cd .. //回到上一层目录 cd /blabla //绝对地址,从 / 开始 cd blabla //相对地址,从当前目录开始 cd ./blabla //同上 cd .//blabla //同上,多个///可视作一个/
##pwd 显示当前目录
1
pwd
ls
显示目录下的内容
1 2 3 4 5
ls //显示当前目录下的内容 ls -a //显示当前目录下的内容,包括隐藏文件 ls -l //显示更详细的内容,包括日期,大小,权限等 ls -i //显示inode l //显示目录下的内容(Ubuntu)
Used for viewing files that are not very long; it does not provide any scroll-back.
tac
Used to look at a file backwards, starting with the last line.
less
Used to view larger files because it is a paging program; it pauses at each screenful of text, provides scroll-back capabilities, and lets you search and navigate within the file. Note: Use / to search for a pattern in the forward direction and ? for a pattern in the backward direction.
tail
Used to print the last 10 lines of a file by default. You can change the number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.
head
The opposite of tail; by default it prints the first 10 lines of a file.
$ do_something > all-output-file 2>&1 or $ do_something >& all-output-file
参考文档
man
1 2 3 4 5 6
man bla //显示跟bla有关的详细信息 man -f bla //显示bla的概要,与whatis一样 man -k bla //显示跟bla有关的指令的概要,与apropos一样 //man pages分成 1 - 9个章节,章节后面可带字母 man 3 printf //显示在第3章的printf man -a printf //显示所有章节的printf,一章一章的展示
info
GUN Info System
1
info blabla //显示blabla的信息
help
1 2 3 4
help // 显示当前shell内置的指令的简要使用方法 bla -h //显示bla指令的简要帮助页,某些指令可能没有 bla --help // 同上 help的速度比man、info要快
mount /dev/sda5 /home //将dev/sda5挂载到/home文件夹 mount //显示当前挂载的文件系统 df -Th //同上,同时显示使用情况 /etc/fstab //修改该文件,可开机自动挂载
The Network Filesystem
Server
1 2 3 4
sudo service nfs start //start nfs service /etc/exports //contains the directories and permissions that a host is willing to share with other systems over NFS. exportfs -av //update change sudo service nfs restart
Client
1 2 3
/etc/fstab //to modified to accomplish nfs servername:/projects /mnt/nfs/projects nfs defaults 0 0 //sample mount servername:/projects /mnt/nfs/projects //one-time mount,without reboot
FileSystem Architecture
Directory
Usage
/
root directory
/home
Each user has a home directory, usually placed under /home
/root
root user’s home directory
/bin
contains executable binaries, essential commands used in single-user mode, and essential commands required by all system users
/sbin
essential binaries related to system administration
/usr/bin
Commands that are not essential for the system in single-user mode
/usr/sbin
for less essential system administration programs
/dev
contains device nodes,Linux creating them dynamically when devices are found except for network devices
/var
contains files that are expected to change in size and content as the system is running such as the System log files: /var/log Packages and database files: /var/lib Print queues: /var/spool Temp files: /var/tmp
/etc
home for system configuration files
/boot
contains the few essential files needed to boot the system
/lib
contains libraries (common code shared by applications and needed for them to run) for the essential programs in /bin and /sbin
/media
typically located where removable media, such as CDs, DVDs and USB drives are mounted
/opt
Optional application software packages.
/sys
Virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes.
/srv
Site-specific data served up by the system. Seldom used.
/tmp
Temporary files; on some distributions erased across a reboot and/or may actually be a ramdisk in memory.
/usr
Multi-user applications, utilities and data.
/usr/
include Header files used to compile applications.
/usr/lib
Libraries for programs in /usr/bin and /usr/sbin.
/usr/lib64
64-bit libraries for 64-bit programs in /usr/bin and /usr/sbin.
/usr/sbin
Non-essential system binaries, such as system daemons.
/usr/share
Shared data used by applications, generally architecture-independent.
/usr/src
Source code, usually for the Linux kernel.
/usr/X11R6
X Window configuration files; generally obsolete.
/usr/local
Data and programs specific to the local machine. Subdirectories include bin, sbin, lib, share, include, etc.
/usr/bin
This is the primary directory of executable commands on the system.
Comparing Files and File Types
diff & patch
diff Option
Usage
-c
Provides a listing of differences that include 3 lines of context before and after the lines differing in content
-r
Used to recursively compare subdirectories as well as the current directory
-i
Ignore the case of letters
-w
Ignore differences in spaces and tabs (white space)
passwd username //passwd属于SUID(Set owner User ID upon execution)命令,即用户修改自己密码时该命令不需要root权限
添加组
1 2 3 4 5
sudo groupadd lo //添加组,名为lo sudo groupdel lo //删除 groups candy //查看candy的分组 sudo usermod -G lo candy //添加candy至组lo //不能将candy直接从lo组移除,需要用上面的语句从新设置candy的分组
sed s/is/are/ file //把file第一行第一个is换为are sed s/is/are/g file //把第一行所有is换位are sed s/is/are/g file1 file2 //多文件同时操作
awk 提取内容
It is a powerful utility and interpreted programming language.
It is used to manipulate data files, retrieving, and processing text.
It works well with fields (containing a single piece of data, essentially a column) and records (a collection of fields, essentially a line in a file).
Command
Usage
awk ‘command’ var=value file
Specify a command directly at the command line
awk -f scriptfile var=value file
Specify a file that contains the script to be executed along with f
awk ‘{ print $0 }’ /etc/passwd
Print entire file of /etc/passwd
awk -F: ‘{ print $1 }’ /etc/passwd
Print first field (column) of every line, separated by a space
su可切换至root账户,需要root的密码 sudo可临时使用root权限,需要用户自己的密码,仅限在/etc/sudoers or /etc/sudoers.d/ 下定义的用户 当使用sudo或者使用失败时,记录会保存在/var/log/secure (Debian) 或者 /var/log/messages or /var/log/secure 编辑sudoers文件可用visudo命令,基本结构为: who where = (as_whom) what
password 密码
用户密码存放在/etc/shadow,访问该文件需要root权限 /etc/shadow不存放明文密码,该密码通常用SHA-512(Secure Hashing Algorithm 512 bits, developed by the U.S. National Security Agency (NSA) )算法得到
1
echo -n test | sha512sum // 获得字符串test的sha512算法加密后的密文