时间|2022年6月27日
了解Linux系统,熟悉Linux常用命令
Linux 文件系统是树状结构,系统中每个分区都是一个文件系统,都有自己的目录层。Linux 会将这些分属不同分区的、单独的文件系统按照树状的方式形成一个系统的总目录层次结构。目录提供了一个管理文件方便而有效的途径,最上层是根目录,其它所有目录都是从根目录出发而生成的,如下图所示。
/root
:根目录,该目录下一般只存放目录,不存放文件。
/bin
:存放可执行二进制文件的目录,如常用命令 ls
、mv
、cat
等。
/home
:为用户设置的目录,如用户 user 的主目录就是/home/user
/dev
:系统硬件设备目录(device)。Linux系统所有的硬件都通过文件表示,访问该目录下某个文件,相当于访问某个设备,比如/dev/cdrom
是光驱。
/etc
:存放系统配置文件的目录,比如:/ect/passwd
用于存储用户信息的文件。
/lib
:存放根文件系统程序运行所需要的共享库及内核模块。共享库又叫作动态链接共享库,作用类似Windows里的.dll文件,存放了根文件系统程序运行所需的共享文件。
/sbin
:放置系统管理员使用的可执行命令, 如 fdisk
、shutdown
等。与/bin 不同的是,这几个目录是给系统管理员 root 使用的命令,一般用户只能"查看"而不能设置和使用。
/tmp
:临时文件目录,用于存放各种临时文件,是公用的临时文件存储点,系统重启后不会被保存。
/usr
:应用程序存放目录(unix system resource),/usr/bin
存放应用程序,/usr/share
存放共享数据,/usr/lib
存放一些函数库文件等。
/var
:存放运行时需要改变数据的文件, 如随时更改的日志文件/var/log
。
/mnt
:系统管理员安装临时文件系统的安装点,系统提供这个目录是让用户临时挂载其他的文件系统。
/boot
:放置 Linux 系统启动时用到的一些文件,如Linux 的内核文件、引导程序等。
/opt
:给主机额外安装软件所摆放的目录。
/proc
:内存映射目录,该目录可以查看系统的相关信息。操作系统运行时,进程信息及内核信息(比如cpu、硬盘分区、内存信息等)存放在这里。
/bin
/sbin
/usr/bin
/usr/sbin
pwd #print work/current directory
ls #list files
cd [directory_path] #enter directory_path
cd .. #back to previous directory
cd ~ #back to home directory
cd / #back to root directory
exit #exit bash
mkdir [new_directory] #create new_folder in work directory
rm -ri [directory_name] #remove directory with interaction
rm -rf [directory_name] #remove directory without interaction
rmdir [empty_directory] #remove empty_directory(not recommended)
touch new.txt #create new.txt in work directory
echo "hello" > new.txt #create new.txt with content "hello"
echo "world" >> new.txt #append "world" to new.txt
cat test.txt #show the content of test.txt
cat > test.txt #create test.txt with new_content
new_content
⌃+D
cat >> test.txt #append new content to test.txt
new_content
⌃+D
vi test.txt #open "vi" to modify test.txt
vim test.txt #open "vim" to modify test.txt
ls → list directory contents
ls
ls -l #以长格式列出文件的详细信息
ls -a #列出目录下的所有文件,包括以.开头的隐藏文件。
ls -s #在每个文件名后输出该文件的大小。
cat → concatenate and print files
cat file_name.txt #显示输出文档的内容
cat -n file_name.txt #显示输出文档时,对输出文档的行数进行编号,从1开始
cat -b file_name.txt #显示输出文档时,对输出文档的非空行数进行编号,从1开始
cat -s file_name.txt #显示输出文档时,将输出文档的多行空格显示为一行空格
cat -e file_name.txt #显示输出文档时,在每行结束处显示$
cat >new_test.txt #创建空白的new_test.txt文件,并且写入下面内容
add new text here.
There are 3 blank lines in this new file.
#输入control+D来结束输入
cat >>new_test.txt #向new_test.txt文件中追加内容
This line is appended to the file by using >>
#输入control+D来结束输入
cat file1.txt file2.txt #将file1与file2内容连接起来并显示(不改变原file1,file2内容)
cat file1.txt file2.txt >file3.txt #创建file3,将file1与file2内容连接起来并且写入file3中
chmod → change file mode bits
ll file1.txt #查看指定文档的权限
chmod u-w file1.txt #删除owner的写入权限
chmod -w file1.txt #删除file1.txt的写入权限
chmod +w file1.txt #添加file1.txt的写入权限
用户类型
who | 用户类型 | 说明 |
---|---|---|
u | owner | 文件所有者 |
g | group | 文件所有者所在组 |
o | other users | 所有其他用户 |
a | all | 所有用户,相当于ugo |
Linux/Unix的文件调用权限分为三级 : 文件所有者(Owner)、用户组(Group)、其它用户(Other Users)
操作符号
Operator | 说明 |
---|---|
+ | 添加指定用户类型的权限 |
- | 删除指定用户类型的权限 |
= | 设置指定用户权限的设置,即将用户类型的所有权限重新设置 |
权限符号
operator | 说明 |
---|---|
r | 读取 |
w | 写入 |
x | 执行 |
cp → copy files or directories
cp file1.txt file2.txt #将file1.txt复制到file2.txt
cp file1.txt test #将file1.txt复制到test文件夹中
mv → move(rename) files
mv file1.txt file2.txt #将当前目录下的文件 file1 更名为 file2
mv file2.txt test #将file2.txt移动至同一目录下的test文件夹中
mv file2.txt ~/Desktop #将file2.txt移动至桌面
mv file1.txt .. #将file1.txt移动至上一级目录中
rm → remove files or directories
rm file1.txt #删除file1.txt
rm -i file1.txt #以交互的方式删除file1.txt
rm -rf folder7 #删除整个folder7目录,无交互
rm -ri folder2 #删除整个folder7目录,有交互
mk → make directories
mkdir folder #在当前工作目录中创建新的目录folder
mkdir folder4 folder5 folder6 #在当前工作目录中创建新的目录folder
mkdir -p folder7/test #在当前工作目录中创建带有子目录的目录
rmdir → remove empty directories
rmdir folder6 folder5 folder4 #删除空目录folder6/5/4
注意点:rmdir命令仅支持删除空目录,若选择的目录是非空目录,则会报错。
cd test #切换至指定目录
cd .. #返回上一级目录
cd ~ #进入用户主目录
cd / #进入根目录
pwd → print name of current/working directory
pwd #显示当前工作目录的绝对路径
date → print or set the system date and time
date #显示当前日期信息
echo → display a line of text
echo "hello\tworld" #显示字符串,一般起到提示的作用
echo -e "hello\tworld" #显示字符串,转义字符生效
echo -n "hello\tworld" #显示字符串,结尾不换行
echo -e "hello\tworld" >file1.txt #将"hello world"覆盖至file1.txt中
echo -e "hello\tworld" >>file1.txt #将"hello world"追加至file1.txt中
echo -e "\033[33mhello\tworld\033[0m" #显示彩色字体
#彩色字体参数表
\033[30m 黑色字 \033[0m
\033[31m 红色字 \033[0m
\033[32m 绿色字 \033[0m
\033[33m 黄色字 \033[0m
\033[34m 蓝色字 \033[0m
\033[35m 紫色字 \033[0m
\033[36m 天蓝字 \033[0m
\033[37m 白色字 \033[0m
\033[40;37m 黑底白字 \033[0m
\033[41;37m 红底白字 \033[0m
\033[42;37m 绿底白字 \033[0m
\033[43;37m 黄底白字 \033[0m
\033[44;37m 蓝底白字 \033[0m
\033[45;37m 紫底白字 \033[0m
\033[46;37m 天蓝底白字 \033[0m
\033[47;30m 白底黑字 \033[0m
cal → calendar
cal #显示当前月份日历
cal 10 2022 #显示指定月份日历
cal -j #显示自1月1日起的总天数
cal -y #显示当前年历
clear → clear the terminal screen
clear #使终端显示页向后翻一页,实现清屏
ps → report a snapshot of the current processes
ps -l #较长、较详细的将属于本次登入用户的进程列出来
各个表头的含义为:
ps -aux #显示后台运行的进程
各个表头的含义为:
ps -u #显示属于自己的进程
ps -ef | grep java #查看指定进程,如关于java的进程
kill → send a signal to a process
kill -l #显示所有信号名称
只有第9种信号 “强制退出” (SIGKILL)才可以无条件终止进程,其他信号进程都有权利忽略。
#先筛选出相关进程
> ps -ef | grep java
root 16934 1 0 Feb25 ? 00:18:13 java -jar demo.jar
#彻底杀死该进程
> kill -9 16934
who → show who is logged on
who #显示当前登录系统的用户
who --version #查看当前版本
whoami #显示当前登录的用户名
reboot → halt, power-off or reboot the machine
reboot #重新启动计算机,它的使用权限是系统管理者。
passwd → change user password
passwd #修改当前用户登陆密码
passwd -d parallels #删除用户parallels的密码
su → run a command with substitute user and group ID
>whoami #显示当前用户
ricky
>pwd #显示当前目录
/home/ricky
>su root #切换到root用户
密码:
>whoami #显示当前用户
root
man → an interface to the system reference manuals
man ls #显示ls命令的使用指南
使用下面的命令显示有关你计算机系统信息:uname(显示操作系统的名称), uname –n(显示系统域名),uname –p(显示系统的CPU名称)
PS1为shell命令行界面的主提示符。
本实验用到的命令还有:date、cal、pwd、write、uptime、man等。
date
a) 给出你显示以上年份年历的命令
cal 4
cal 52
cal 1752
cal 1952
cal 2005
cal 2006
b) 1752年有几天,为什么?
1752年由于某些历史原因,在9月份少了11天,全年仅有355天。
pwd
alias
uptime
Command | Short Description | Use Example |
---|---|---|
touch | update the access and modification times of each FILE to the current time | touch [file] |
cp | copy files and directories | cp [file] [dir] |
mv | move (rename) files | mv [file] [dir] |
rm | remove files or directories | rm -rf [dir] |
mkdir | make directories | mkdir [dir] |
rmdir | remove empty directories | rmdir [dir] |
ls | list directory contents | ls |
cd | change directory | cd [dir] |
pwd | print name of current/working directory | pwd |
open | open, openat, creat - open and possibly create a file | int fd=open(“./file”,O_RDONLY) |
read | read from a file descriptor | read -p “prompt” [input] |
write | send a message to another user | n=write(fp, p1+len, 3) |
close | close a file descriptor | int close(int fd) |
pipe | create pipe | int pipe(int pipefd[2]); #fd[0]为读端,fd[1] 为写端 |
socket | create an endpoint for communication | int socket(int domain,int type,int protocol) |
mkfifo | make FIFOs (named pipes) | int mkfifo(const char* pathname, mode_t mode) |
system | execute a shell command | int system(const char *command) |
printf | format and print data | printf [format] [arguments] |