文章目录
  1. 1. Exploring the Filesystem
    1. 1.1. cd
    2. 1.2. ls
    3. 1.3. tree
    4. 1.4. mkdir
    5. 1.5. rm -r
    6. 1.6. ln
    7. 1.7. pushd
    8. 1.8. popd
    9. 1.9. dirs
  2. 2. Searching for Files
    1. 2.1. locate
    2. 2.2. 通配符Wildcard
    3. 2.3. find
  3. 3. working with files
    1. 3.1. viewing files
    2. 3.2. touch
    3. 3.3. 删除文件
  4. 4. IO
  5. 5. 参考文档
    1. 5.1. man
    2. 5.2. info
    3. 5.3. help
  6. 6. File Operations
    1. 6.1. FileSystems
      1. 6.1.1. Mount Points
      2. 6.1.2. The Network Filesystem
        1. 6.1.2.1. Server
        2. 6.1.2.2. Client
    2. 6.2. FileSystem Architecture
    3. 6.3. Comparing Files and File Types
      1. 6.3.1. diff & patch
      2. 6.3.2. file 查看文件类型
    4. 6.4. Backing Up and Compressing Data
      1. 6.4.1. rsync
      2. 6.4.2. Compressing Data
      3. 6.4.3. tar 打包
      4. 6.4.4. dd Disk-to-Disk Copying
  7. 7. USER ENVIRONMENT
    1. 7.1. 识别用户
    2. 7.2. 添加用户
    3. 7.3. 修改用户密码
    4. 7.4. 添加组
    5. 7.5. 配置文件
    6. 7.6. 环境变量
      1. 7.6.1. 主目录 $HOME
      2. 7.6.2. 路径 $PATH
      3. 7.6.3. 提示语句 PS1
    7. 7.7. 命令历史
    8. 7.8. 命令别名
    9. 7.9. 文件权限
  8. 8. su 切换用户,不提倡使用
  9. 9. sudo 临时使用root权限
  10. 10. 关机,重启
    1. 10.1. shutdown 关机,重启 (root)
    2. 10.2. halt 关机(root)
    3. 10.3. poweroff 关机(root)
    4. 10.4. init 0 关机(root)
    5. 10.5. 关闭图形界面
  11. 11. 安装应用程序
  12. 12. 程序定位
    1. 12.1. which
    2. 12.2. whereis
  13. 13. 文本操作
    1. 13.1. echo
    2. 13.2. cat
    3. 13.3. nano
    4. 13.4. gedit
    5. 13.5. vim
    6. 13.6. sed 多文本操作
    7. 13.7. awk 提取内容
  14. 14. File Manipulation Utilities
    1. 14.1. sort 排序
    2. 14.2. uniq 去重
    3. 14.3. paste 黏贴
    4. 14.4. join 连接
    5. 14.5. split 分割
  15. 15. 正则表达式
  16. 16. Security
    1. 16.1. last 每个账户上次(包含正在登陆的)登陆的时间
    2. 16.2. root
    3. 16.3. su 与 sudo
    4. 16.4. password 密码
      1. 16.4.1. chage 用来规划用户密码,比如密码过期时间,多久要修改密码等
      2. 16.4.2. PAM (Pluggable Authentication Modules) 用来增强passwd命令
    5. 16.5. boot loader

LinuxFoundationX: LFS101x.2 Introduction to Linux

Exploring the Filesystem

cd

切换目录

1
2
3
4
5
6
7
8
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)

tree

按树的结构显示目录

1
2
tree //当面目录按树结构展示
tree -d //只展示目录

mkdir

新建目录

1
2
3
mkdir blabla //在当前目录新建blabla文件夹
mkdir ./blabla //同上
mkdir bla/blabla //在当前目录下的bla文件夹内新建blabla文件夹

rm -r

删除文件夹

1
rm -r blabla //删除当前目录下的blabla文件夹

ln

链接Links

1
2
3
4
5
ln file1 file2 //hard link,inode相同,相当于取别名,删除一个链接不会删除文件(如有有其他别名的话)
ln -s file1 file2 //soft link,inode不同
ln -s bla/blabla blabla // bla/blabla可以是文件夹
ln -s bla // 空链接(dangling link)
// 不同的编辑器可能对链接的文件的操作方式不同,可能在同一个文件修改,也可能新建一个文件

pushd

前往并将目录压栈

1
pushd tmp //前往当前目录下的tmp文件夹(跟cd相同),同时将tmp文件夹压栈

popd

弹出并前往栈顶目录

1
popd

dirs

查看栈中的目录

1
dirs

Searching for Files

locate

查找所有匹配的结果

1
locate zip | grep bin //查找zip,结果当作输入给后面的grep指令,再进一步缩小范围

locate是通过文件的数据库查找的,数据库通常1天更新一次,可以手动更新

1
updatedb //更新数据库

通配符Wildcard

Wildcard result
? 一个任意字符
* 零个或任意多个任意字符
[set] set里的其中一个字符
[!set] set里的字符外的一个字符
1
2
3
4
5
Sample
ls he?.out
ls hel.*
ls h[a-e][al].out
ls h[!f-z]l.out

find

查找指定目录下的所有文件(包含目录以及子目录和子目录下的文件),速度有些慢

1
2
3
4
5
6
7
8
9
10
11
12
13
find //输出当前目录下的的所有文件
find . //同上
find . -name a //当前目录下的名为a的文件/文件夹
find . -name "a?c" //模糊查找用引号
find . -type d -name tmp //查找名为tmp的文件夹
find . -type f -name tmp //查找名为tmp的文件
find -name "*.swp" -exec rm {} ';' //查找名为*.swp的文件,将其地址放到{}内,对每个结果执行-exec 后面的指令
find -name "*.swp" -exec rm {} \; //末尾必须为';'(包含引号)或\;(包含分号)
find -name "*.swp" -ok rm {} \; //类似-exec,只是操作时会提示确认
find / -ctime 3 //查找/下3天(0表示今天)修改过权限的文件,对应的有-atime最后访问 -mtime最后修改
find -mmin -3 //查找当前目录下3分钟内修改过的文件,对应的有-amin和-cmin,-3表示3以内,+3表示3以上
find -size 0 //查找大小为0块的文件(512字节)
find -size +2M //大于2M的文件,bytes (c), kilobytes (k), megabytes (M), gigabytes (G), etc

working with files

viewing files

command usage
cat 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.

touch

修改访问、修改时间或者新建空文件

1
2
touch a.txt //新建名为a.txt的空文件
touch -t 03201600 a.txt //修改a.txt的修改时间为3月20日16点

删除文件

command usage
mv 移动或重命名文件或文件夹
rm 删除文件
rm -f 强制删除文件
rm -i 删除并提示确认
rm -r 删除文件夹以及文件夹下的所有文件
rmdir 删除空的文件夹

IO

name descriptor redirect
stdin 0 <
stdout 1 >
stderr 2 2>

同时输出stdout和stderr

1
2
3
$ 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要快

File Operations

FileSystems

Mount Points

设备需要挂载到某个文件夹后才能使用,如果文件夹不为空,则挂载后不能访问原有文件,得解除挂载后才能访问

1
2
3
4
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)
1
2
3
4
5
6
diff a.txt b.txt
diff3 MY-FILE COMMON-FILE YOUR-FILE // 3 files
diff -Nur originalfile newfile > patchfile
diff -au filea fileb > patchfile
patch -p1 < patchfile
patch originalfile patchfile

file 查看文件类型

1
2
3
file hello.c
file a
file hello.o

Backing Up and Compressing Data

rsync

copies only the parts of files that have actually changed, it can be very fast than cp

1
2
rsync sourcefile destinationfile
rsync -r sourcedir destinationdir

Compressing Data

Command Usage
gzip The most frequently used Linux compression utility
bzip2 Produces files significantly smaller than those produced by gzip
xz The most space efficient compression utility used in Linux
zip Is often required to examine and decompress archives from other operating systems
Command Usage
gzip * Compresses all files in the current directory; each file is compressed and renamed with a .gz extension.
gzip -r projectX Compresses all files in the projectX directory along with all files in all of the directories under projectX.
gunzip foo De-compresses foo found in the file foo.gz. Under the hood, gunzip command is actually the same as gzip –d.
bzip2 * Compress all of the files in the current directory and replaces each file with a file renamed with a .bz2 extension.
bunzip2 *.bz2 Decompress all of the files with an extension of .bz2 in the current directory. Under the hood, bunzip2 is the same as calling bzip2 -d.
xz * Compress all of the files in the current directory and replace each file with one with a .xz extension.
xz foo Compress the file foo into foo.xz using the default compression level (-6), and remove foo if compression succeeds.
xz -dk bar.xz Decompress bar.xz into bar and don’t remove bar.xz even if decompression is successful.
xz -dcf a.txt b.txt.xz > abcd.txt Decompress a mix of compressed and uncompressed files to standard output, using a single command.
xz -d *.xz Decompress the files compressed using xz.
zip backup * Compresses all files in the current directory and places them in the file backup.zip.
zip -r backup.zip ~ Archives your login directory (~) and all files and directories under it in the file backup.zip.
unzip backup.zip Extracts all files in the file backup.zip and places them in the current directory.

tar 打包

Command Usage
tar cf a.tar . 当前文件夹下的文件打包
tar cf b.tar ../tmp 当前文件夹(名为tmp)下文件打包,且仍在tmp下
tar tvf a.tar 查看a.tar的内容
tar xvf mydir.tar Extract all the files in mydir.tar into the mydir directory
tar zcvf mydir.tar.gz mydir Create the archive and compress with gzip
tar jcvf mydir.tar.bz2 mydir Create the archive and compress with bz2
tar Jcvf mydir.tar.xz mydir Create the archive and compress with xz
tar xvf mydir.tar.gz Extract all the files in mydir.tar.gz into the mydir directory. Note you do not have to tell tar it is in gzip format.

dd Disk-to-Disk Copying

1
2
3
dd if=/dev/sda of=sda.mbr bs=512 count=1 //将sda的前512字节拷贝到当前目录,名为sda.mbr
dd if=/dev/sda of=/dev/sdb //将sda拷贝到sdb,会清空sdb原有文件!
dd if=filea of=fileb conv=ucase //将filea拷贝到fileb,同时将小写转为大写

USER ENVIRONMENT

识别用户

1
2
who //显示当前登陆用户
whoami //显示当前用户

添加用户

1
2
3
4
5
sudo useradd candy //添加用户candy
//设置其主目录为/home/candy,基础文件拷贝自/etc/skel,在/etc/passwd添加一行描述
//设置默认shell为/bin/bash
sudo userdel candy //删除用户candy
//删除用户为上面的反操作,但是不会删除其主目录,添加-r会删除主目录

修改用户密码

1
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的分组

配置文件

~/.bashrc //新打开的shell会从此读取配置信息,如环境变量,别名等

环境变量

1
2
3
4
5
6
7
8
9
10
11
set //显示变量,很多
env
export
echo $SHELL //SHELL是一个变量,用$可以读取变量内容,用echo将其显示
export VARIABLE=value //设置VARIABLE的值,并声明为环境变量
VARIABLE=value; export VARIABLE //同上
//注意'='两边不能有空格
新设置的变量在bash关闭后自动丢弃,要设置永久变量
需要在~/.bashrc后面添加一行该变量的声明语句
source ~/.bashrc //立即使用刚刚设置的变量,不用新开bash
. ~/.bashrc //同上

主目录 $HOME

路径 $PATH

PATH中包含一些路径,路径间用:分开,中间含有一个空的路径(或.)表示当前路径

1
export PATH=$HOME/bin:$PATH //添加主目录的bin到PATH前面

提示语句 PS1

PS1是命令行起始地那段提示语,可以修改

1
2
3
4
5
6
\u - User name 
\h - Host name
\w - Current working directory
\! - History number of this command
\d - Date
PS1='\u \w \d ' //显示“用户名 当前目录 日期 ”

命令历史

1
2
3
4
5
6
7
键盘向上箭头 //上一条语句
向下箭头 //下一条语句
history //显示命令历史,存在~/.bash_history下
变量:
HISTFILE //历史文件的存储地址
HISTFILESIZE //最大历史文件大小(行数)
HISTSIZE //最大当前执行期历史大小(行数)
key usage
!! 执行上条语句
CTRL-R 查找执行过的语句
!n 执行第n条指令(n对应history的行数)
!$ 上条指令的最后一个参数
!string 上一条起始为string的指令
CTRL-L 清空屏幕
CTRL-Z 将当前进程放到后台挂起
CTRL-C 强制结束当前进程
CTRL-A 移到行首
CTRL-W 删除光标前的所有文字
CTRL-U 删除行首到光标间的文字
CTRL-E 移到行尾
TAB 自动补完命令,目录等

命令别名

1
2
3
alias xx='ls -l' //为ls -l取别名xx,输入xx等于输入ls -l
//别名只是暂时的,关闭窗口自动消失
//取永久别名可以在~/.bashrc声明别名

文件权限

1
2
sudo chown root file1  //修改file1的owner为root
sudo chgrp bin file1 //修改file1的group为bin

权限分为 r 可读、w 可写、x 可执行,同时分别对应 user group other
如-rw-rw-r– //-表示不可

1
2
3
rwx:rwx:rwx
u: g: o //可用u、g、o表示三种类型
chmod uo+x,g-w testf //修改testf的权限,用户以及其他用户添加可执行,组减去可写

用数字表示对应的权限

1
2
3
4 可读
2 可写
1 可执行

因此5对应4+1即可读可执行,7对应rwx等

1
2
chmod 700 testf //修改testf为用户可读可写可执行,组以及其他用户都不行
//注意数字仍然分3位对应3中类型

su 切换用户,不提倡使用

1
2
su candy //切换到用户candy
su //切换到root

sudo 临时使用root权限

1
2
3
4
5
6
ubuntu安装时自动设置
手动设置:
su //接着输入root密码
echo "username ALL=(ALL) ALL" > /etc/sudoers.d/username //设置配置文件
chmod 440 /etc/sudoers.d/username //修改配置文件的权限
需要root的权限的操作只需在操作前添加sudo 即可

关机,重启

shutdown 关机,重启 (root)

1
2
3
shutdown -h now //立即关机
shutdown -r now //立即重启
shutdown -h 10:00 "shutting down for a break." //10:00关机,同时给其他用户发送通知

halt 关机(root)

1
halt

poweroff 关机(root)

1
poweroff

init 0 关机(root)

1
init 0

关闭图形界面

1
2
3
sudo service gdm stop // 关闭图形界面,Debian-based systems
sudo service lightdm stop //同上
sudo telinit 3 // RPM-based systems

安装应用程序

package Manager Low Level High Level
Debian dpkg apt-get
SUSE rpm zypper
Fedora rpm yum

Debian

1
2
3
4
5
6
sudo apt-cache search lynx //查找名为lynx的程序
sudo apt-get install lynx //安装
sudo apt-get policy lynx //查找信息
sudo apt-get remove lynx //删除
sudo dpkg -l //查找所有已安装程序
sudo apt-get upgrade //升级程序

yum/zypper

1
2
3
4
5
6
7
sudo yum/zypper search lynx //yum for Fedora, zypper for SUSE
sudo yum/zypper install lynx
sudo yum/zypper info lynx
sudo yum/zypper remove lynx
sudo yum/zypper list install
sudo rpm -qa //同上
sudo yum/zypper update

程序定位

which

1
which diff //显示diff的地址

whereis

1
whereis diff //显示diff的地址(查找范围更广)

文本操作

echo

1
2
3
echo hello > hello //把hello写入hello文件(替换掉原有内容)
echo helloo >> hello //在hello文件后面添加helloo
echo -e string\nstring // -e可以识别转义字符

cat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat hello //查看hello
cat file1 file2 file3 // 可以同时查看多个文件(连接多个文件)
cat > hello //写入新内容到hello
blah blah
blah
[CTRL-D] //写入blah blah\nblah, 按ctrl-d会保存到文件
cat << EOF > hello //连续写入内容到hello,直到输入EOF(EOF不会被写入)
> a
> ab
> EOF

cat << a > hello //同上,输入a截止
> b
> bc
> a

nano

简单的编辑器

1
nano hello //编辑hello

gedit

图形化编辑器,类似notepad

1
gedit hello // 编辑hello

vim

(vi升级版,现vi已被vim取代)

1
2
vim hello //编辑hello
vimtutor //vim使用指导,有中文版,建议跟着练习,可能花费1小时左右

Command / key Usage
vi myfile Start the vi editor and edit the myfile file
vi -r myfile Start vi and edit myfile in recovery mode from a system crash
:r file2 Read in file2 and insert at current position
:w Write to the file
:w myfile Write out the file to myfile
:w! file2 Overwrite file2
:x or :wq Exit vi and write out modified file
:q Quit vi
:q! Quit vi even though modifications have not been saved
arrow keys To move up, down, left and right
j or To move one line down
k To move one line up
h or Backspace To move one character left
l or Space To move one character right
0 To move to beginning of line
$ To move to end of line
w To move to beginning of next word
e To move to the end of next word
:0 or 1G To move to beginning of file
:n or nG To move to line n
:$ or G To move to last line in file
CTRL-F or Page Down To move forward one page
CTRL-B or Page Up To move backward one page
^l To refresh and center screen
/pattern Search forward for pattern
?pattern Search backward for pattern
n Move to next occurrence of search pattern
N Move to previous occurrence of search pattern
a Append text after cursor; stop upon Escape key
A Append text at end of current line; stop upon Escape key
i Insert text before cursor; stop upon Escape key
I Insert text at beginning of current line; stop upon Escape key
o Start a new line below current line, insert text there; stop upon Escape key
O Start a new line above current line, insert text there; stop upon Escape key
r Replace character at current position
R Replace text starting with current position; stop upon Escape key
x Delete character at current position
Nx Delete N characters, starting at current position
dw Delete the word at the current position
D Delete the rest of the current line
dd Delete the current line
Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (copy) the current line and put it in buffer
Nyy or yNy Yank (copy) N lines and put it in buffer
p Paste at the current position the yanked line or lines from the buffer.

##ALL READ AND NO LAB MAKES JACK A BOY CAN’T USE VIM

sed 多文本操作

sed 默认将结果输出到屏幕

command usage
sed -e command Specify editing commands at the command line, operate on file and put the output on standard out (e.g., the terminal)
sed -f scriptfile Specify a scriptfile containing sed commands, operate on file and put output on standard out.
sed s/pattern/replace_string/ file 替换掉第一行的第一个pattern为replace_string
sed s/pattern/replace_string/g file 替换掉整行的pattern
sed 1,3s/pattern/replace_string/g file 替换掉指定范围行
sed -i s/pattern/replace_string/g file 保存到原文件,不提倡
sed s/pattern/replace_string/g file1 > file2 保存到指定文件
1
2
3
4
5
6
7
sed -e 's/01/JAN/' -e 's/02/FEB/' -e 's/03/MAR/' -e 's/04/APR/' -e 's/05/MAY/' \ 
-e 's/06/JUN/' -e 's/07/JUL/' -e 's/08/AUG/' -e 's/09/SEP/' -e 's/10/OCT/' \
-e 's/11/NOV/' -e 's/12/DEC/' file //To convert 01/02/… to JAN/FEB/… from file

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
awk -F: ‘{ print $1 $6 }’ /etc/passwd Print first and sixth field of every line

File Manipulation Utilities

sort 排序

sort默认安装ASCII字符排序

1
2
3
sort file 
sort -r file // 逆序
sort -u file // 排序,同时去除重复

uniq 去重

1
2
3
sort file | uniq // same as sort -u file
uniq file
uniq -c file // 同时输出重复字数

paste 黏贴

可将两文件并排合并

1
2
3
4
5
6
7
8
9
10
11
12
假如有文件name
bob
alice
文件phone
110
120
执行:
paste name phone
将得到
bob 110
alice 120
paste -d ':' name phone //指定分隔符为':',默认为'\t'

join 连接

相当于等值连接,必须要有一列值相同

1
2
3
4
5
6
7
8
9
10
承接上面
paste name phone > contact
假设有文件addr
bob somewhere
alice anywhere

join contact addr
将得到:
bob 110 somewhere
alice 120 anywhere

split 分割

默认将大文件分割为多个1000行的小文件,默认小文件为原文件名后加后缀

1
2
wc -l american-english //查看有多少行
split american-english dictionary // 将american-english 分为多个名为dictionary加后缀的小文件

正则表达式

Search Patterns Usage
.(dot) Match any single character
a z Match a or z
$ Match end of string
* Match preceding item 0 or more times

example

the quick brown fox jumped over the lazy dog

Command Usage
a.. matches azy
b. j. matches both br and ju
..$ matches og
l.* matches lazy dog
l.*y matches lazy
the.* matches the whole sentence

Security

last 每个账户上次(包含正在登陆的)登陆的时间

1
last

root

root 账户拥有最高的权限,当以root用户登录时,shell提示符为 # (默认情况下)
root权限通常用于

  • Creating, removing and managing user accounts.
  • Managing software packages.
  • Removing or modifying system files.
  • Restarting system services.

su 与 sudo

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算法加密后的密文

chage 用来规划用户密码,比如密码过期时间,多久要修改密码等

1
chage --list username //显示用户的密码规划

PAM (Pluggable Authentication Modules) 用来增强passwd命令

boot loader

1
grub-md5-crypt  //用md5加密

修改grub2的配置时,不应手动修改/boot/grub/grub/cfg,而应修改/etc/grub.d下的文件然后执行

1
update-grub

文章目录
  1. 1. Exploring the Filesystem
    1. 1.1. cd
    2. 1.2. ls
    3. 1.3. tree
    4. 1.4. mkdir
    5. 1.5. rm -r
    6. 1.6. ln
    7. 1.7. pushd
    8. 1.8. popd
    9. 1.9. dirs
  2. 2. Searching for Files
    1. 2.1. locate
    2. 2.2. 通配符Wildcard
    3. 2.3. find
  3. 3. working with files
    1. 3.1. viewing files
    2. 3.2. touch
    3. 3.3. 删除文件
  4. 4. IO
  5. 5. 参考文档
    1. 5.1. man
    2. 5.2. info
    3. 5.3. help
  6. 6. File Operations
    1. 6.1. FileSystems
      1. 6.1.1. Mount Points
      2. 6.1.2. The Network Filesystem
        1. 6.1.2.1. Server
        2. 6.1.2.2. Client
    2. 6.2. FileSystem Architecture
    3. 6.3. Comparing Files and File Types
      1. 6.3.1. diff & patch
      2. 6.3.2. file 查看文件类型
    4. 6.4. Backing Up and Compressing Data
      1. 6.4.1. rsync
      2. 6.4.2. Compressing Data
      3. 6.4.3. tar 打包
      4. 6.4.4. dd Disk-to-Disk Copying
  7. 7. USER ENVIRONMENT
    1. 7.1. 识别用户
    2. 7.2. 添加用户
    3. 7.3. 修改用户密码
    4. 7.4. 添加组
    5. 7.5. 配置文件
    6. 7.6. 环境变量
      1. 7.6.1. 主目录 $HOME
      2. 7.6.2. 路径 $PATH
      3. 7.6.3. 提示语句 PS1
    7. 7.7. 命令历史
    8. 7.8. 命令别名
    9. 7.9. 文件权限
  8. 8. su 切换用户,不提倡使用
  9. 9. sudo 临时使用root权限
  10. 10. 关机,重启
    1. 10.1. shutdown 关机,重启 (root)
    2. 10.2. halt 关机(root)
    3. 10.3. poweroff 关机(root)
    4. 10.4. init 0 关机(root)
    5. 10.5. 关闭图形界面
  11. 11. 安装应用程序
  12. 12. 程序定位
    1. 12.1. which
    2. 12.2. whereis
  13. 13. 文本操作
    1. 13.1. echo
    2. 13.2. cat
    3. 13.3. nano
    4. 13.4. gedit
    5. 13.5. vim
    6. 13.6. sed 多文本操作
    7. 13.7. awk 提取内容
  14. 14. File Manipulation Utilities
    1. 14.1. sort 排序
    2. 14.2. uniq 去重
    3. 14.3. paste 黏贴
    4. 14.4. join 连接
    5. 14.5. split 分割
  15. 15. 正则表达式
  16. 16. Security
    1. 16.1. last 每个账户上次(包含正在登陆的)登陆的时间
    2. 16.2. root
    3. 16.3. su 与 sudo
    4. 16.4. password 密码
      1. 16.4.1. chage 用来规划用户密码,比如密码过期时间,多久要修改密码等
      2. 16.4.2. PAM (Pluggable Authentication Modules) 用来增强passwd命令
    5. 16.5. boot loader