实验概述
实验背景
Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和Unix的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的Unix工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。**
实验介绍
本实验基于科研助手环境,主要介绍Linux的基本命令,帮助大家了解并掌握基本的Linux命令。
实验环境
1、进入科研助手控制台-应用商城页面,选择【类型】为“教学与实践”,“Linux系统教学与实践"镜像,点击“使用此镜像”
2、进入开发机购买页面,资源可以选择CPU,CPU: 4核 内存: 16GB即可满足教学环境。框架版本为预置的jupyter-mysqltraining。
3、购买完成后,回到开发机列表,点击【打开】进入开发机。
4、跳转至jupyter环境,点击【终端】可直接进入实验环境。
前置说明
实验手册的文件名以及目录仅供参考,大家可设置为自己便于操作的文件名或者目录,或者加上唯一标识,比如名字缩写。
命令实操
查看文件夹
以当前所在目录为/home为例:
步骤1 查看当前文件夹信息
输入:
ls
输出:
batchcom dataset-bc-pub
使用ls命令,查看当前目录下的所有文件及文件夹。
输入命令ls --help 查看ls命令详情。
输入:
ls --help
输出:
Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file ... ... GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'ls invocation'
ls的命令参数有很多,本实验将一些经常使用的参数进行一一列举,帮助大家去了解ls命令。
步骤2 列表方式查看当前文件夹信息
输入:
ls -l
输出:
total 8
-rw-r--r-- 1 batchcom batchcom 7637 Apr 1 10:44 Mysql教学.ipynb
drwxr-x--- 1 batchcom batchcom 70 Apr 1 10:02 batchcom
drwxrwxrwx 8 root root 168 Mar 15 22:46 dataset-bc-pub
ls -l 按照列表的形式,输出当前目录的所有信息。
一般关注文件的类型、文件的权限、文件所属的用户及用户组、文件的创建时间和文件的名称,对于文件的硬链接数本实验暂不关注。
这里的信息可以分为8个部分。
参数选项 描述 备注 - d 描述当前文件类型 rw-rw-r-- 描述当前文件的权限,三个为一组 第一组对应的为该文件用户的权限 第二组为文件所属用户组的权限 第三组为其他用户组的权限 r:可读 w:可写 x:可执行 -:无权限 1 1 8 batchcom 当前文件的所属的用户 文件属于batchcom用户创建 batchcom 当前文件所属的用户组 文件归属于batchcom用户组 7637 70 168 Apr 1 10:44 文件的创建时间 Mysql教学.ipynb 文件的名称 ls -l 在Linux使用中也会简写为 ll 。
步骤3 查看文件夹中所有信息
输入:
ls -a
输出:
. .. .ipynb_checkpoints Mysql教学.ipynb batchcom dataset-bc-pub
ls -a 把当前目录的所有信息都列举出来,同时包含隐藏文件。
在Linux系统中 . 开头的文件一般是隐藏文件。
. 表示当前文件夹。
.. 表示父目录。
步骤4 按文件大小查看文件夹信息
输入:
ls -lS
输出:
total 56
-rw-r--r-- 1 batchcom batchcom 55611 Apr 1 10:53 Mysql教学.ipynb
drwxrwxrwx 8 root root 168 Mar 15 22:46 dataset-bc-pub
drwxr-x--- 1 batchcom batchcom 70 Apr 1 10:02 batchcom
参数S使得ls的输出项按照文件大小从大到小排序。
-lS ls命令支持多个参数项拼凑在一起生效。这里表示ls的输出项按照文件大小从大到小以文件列表的形式展示输出项。也可以分开来生效,即 ls -l -S。
步骤5 列表方式查看其他目录
输入:
ls -l /
输出:
total 4
lrwxrwxrwx 1 root root 7 Jan 26 10:05 bin -> usr/bin
drwxr-xr-x 2 root root 6 Apr 18 2022 boot
drwxr-xr-x 5 root root 440 Apr 1 10:50 dev
drwxr-xr-x 1 root root 4096 Apr 1 10:52 etc
drwxrwxrwx 1 root root 47 Apr 1 10:53 home
lrwxrwxrwx 1 root root 7 Jan 26 10:05 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Jan 26 10:05 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Jan 26 10:05 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Jan 26 10:05 libx32 -> usr/libx32
drwxr-xr-x 2 root root 6 Jan 26 10:05 media
drwxr-xr-x 2 root root 6 Jan 26 10:05 mnt
drwxr-xr-x 1 root root 35 Apr 1 09:59 opt
dr-xr-xr-x 1420 root root 0 Apr 1 10:50 proc
drwx------ 2 root root 37 Jan 26 10:13 root
drwxr-xr-x 1 root root 20 Apr 1 10:51 run
lrwxrwxrwx 1 root root 8 Jan 26 10:05 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Jan 26 10:05 srv
dr-xr-xr-x 13 root root 0 Mar 17 00:26 sys
drwxrwxrwt 1 root root 28 Apr 1 10:52 tmp
drwxr-xr-x 1 root root 53 Jan 26 10:05 usr
drwxr-xr-x 1 root root 41 Jan 26 10:13 var
ls 一般默认是查看当前目录下的文件信息,也支持查看其他文件夹的信息。在命令最后紧跟一个绝对路径或者相对路径,可以查看输入的path下的目录信息。
查看当前目录的绝对路径
步骤1 查看当前目录的绝对路径
输入:
pwd
输出:
/home
表示当前路径的绝对路径。
在Linux操作中,需要通过pwd命令查看当前路径的绝对路径,避免因为手写错误或者记忆错误造成文件路径写错,减少可预见的错误操作。
在Linux操作过程中,Shell客户端界面只会显示当前父目录的名称,而不会显示全部的目录地址。
切换目录
步骤1 切换至根目录
输入:
cd /
输入:
ls
输出:
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
通过Shell的客户端界面可以看到,当前目录从~切换到了/,也就是根目录下,同时查看根目录下的文件信息,进行核实。
~ 波浪线代表的目录地址为当前用户的主目录。
输入:
cd ~
输入:
pwd
输出:
/home/batchcom
输入:
ls
输出:
log
可以看到通过切换路径为~,可以直接切换到当前用户的用户主目录。
创建目录
步骤1 创建新目录
输入:
cd /home
mkdir ./mkdir_test
输入:
ll
输出:
total 56
-rw-r--r-- 1 batchcom batchcom 55611 Apr 1 10:53 Mysql教学.ipynb
drwxr-x--- 1 batchcom batchcom 70 Apr 1 10:56 batchcom
drwxrwxrwx 8 root root 168 Mar 15 22:46 dataset-bc-pub
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 10:58 mkdir_test
在用户目录下创建一个文件夹,命名为mkdir_test。
mkdir 创建目录默认在当前目录下创建指定的文件夹,也可以紧跟对应的路径。
输入:
mkdir --help
步骤2 创建多级目录
输入:
mkdir -p /home/dir1/dir2/dir3
输入:
ls -R /home/dir1
输出:
/home/dir1:
dir2
/home/dir1/dir2:
dir3
/home/dir1/dir2/dir3:
mkdir默认只能在目录下创建子目录,无法在不存在的子目录下创建孙子目录。因此通过-p参数,递归地创建目录,避免因为目录不存在而造成目录创建失败。
如果不加-p参数。
输入:
mkdir ~/dir11/dir2/dir3
输出:
mkdir: cannot create directory ‘/home/batchcom/dir11/dir2/dir3’: No such file or directory
用户无法创建目录dir3,因为不存在目录/root/dir11/dir2。
删除目录
步骤1 删除一个目录
输入:
rmdir /home/dir1
输出:
rmdir: failed to remove '/home/dir1': Directory not empty
rmdir在删除目录的时候需要目录内为空,即不存在文件和文件夹。
可以通过rmdir --help查看命令使用。
输入:
rmdir --help
步骤2 删除多级目录
如果需要删除多个文件夹,需要添加-rf参数。
输入:
rm -rf /home/dir1
输入:
ll
输出:
total 24
drwxrwxrwx 1 root root 109 Apr 1 13:00 ./
drwxr-xr-x 1 root root 51 Apr 1 11:46 ../
drwxr-xr-x 1 batchcom batchcom 42 Apr 1 10:44 .ipynb_checkpoints/
-rw-r--r-- 1 batchcom batchcom 16749 Apr 1 11:50 Mysql教学.ipynb
drwxr-x--- 1 batchcom batchcom 109 Apr 1 12:57 batchcom/
drwxrwxrwx 5 root root 71 Mar 26 15:42 dataset-assist-0/
drwxrwxrwx 4 root root 4096 Feb 26 16:59 dataset-bc-pub/
drwxrwxrwx 3 root root 83 Mar 20 18:10 dataset-fs-0/
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test/
创建文件
步骤1 创建空白文件
输入:
cd ~
输入:
touch file1
输入:
ls
输出:
file1 log mkdir_test
创建一个名叫file1的空白文件。
输入:
touch --help
步骤2 创建指定时间戳的文件
输入:
touch -t 202504011230 file2
输入:
ll
输出:
total 20
drwxr-x--- 1 batchcom batchcom 135 Apr 1 13:02 ./
drwxrwxrwx 1 root root 109 Apr 1 13:00 ../
-rw------- 1 batchcom batchcom 1203 Apr 1 11:48 .bash_history
-rw-r--r-- 1 batchcom batchcom 220 Jan 7 2022 .bash_logout
-rw-r--r-- 1 batchcom batchcom 4240 Mar 11 15:02 .bashrc
drwxr-xr-x 3 batchcom batchcom 17 Apr 1 10:02 .cache/
drwxr-xr-x 1 batchcom batchcom 29 Apr 1 10:44 .ipython/
drwxr-xr-x 1 batchcom batchcom 33 Apr 1 10:44 .jupyter/
drwxr-xr-x 1 batchcom batchcom 19 Apr 1 09:59 .local/
-rw-r--r-- 1 batchcom batchcom 807 Jan 7 2022 .profile
drwxr-xr-x 1 batchcom batchcom 29 Apr 1 09:59 .supervisor-pid/
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 12:30 file2
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log/
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test/
使用-t参数创建了一个指定的时间戳202504011230的文件file2。
-t参数后面紧跟的时间戳格式为[[CC]YY]MMDDhhmm[.ss]。
步骤3 更新文件时间戳
输入:
touch -r file1 file2
输入:
ls -l
输出:
total 20
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file2
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log/
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test/
使用-r参数,指定file1文件的时间戳作为file2文件的时间戳。使得file2文件的时间戳得到了更新。
步骤4 创建多个文件
输入:
touch file3 file4
输入:
ls
输出:
file1 file2 file3 file4 log mkdir_test
touch命令后紧跟多个文件名称,会自动创建多个文件。
文件复制
步骤1 文件复制
输入:
cp file1 file1_cp
输入:
ls
输出:
file1 file1_cp file2 file3 file4 log mkdir_test
通过cp命令复制file1文件,并命名为file1_cp。这里是重新创建了一个文件。
可以通过输入cp --help查看使用规则:
cp --help
cp的命令虽然参数很多,但常用的参数比较少,一般都是直接复制文件即可。cp后面紧跟的就是两个文件的路径。
步骤2 保留文件属性复制
使用-a参数复制文件保留文件属性
输入:
cp -a file1 file1_cp_a
输入:
ls -l
输出:
total 20
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file2
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file3
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file4
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log/
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test/
file1_cp_a文件的所有属性与file1保持一致。
步骤3 文件夹复制
创建一个新的文件夹dir1之后使用-r参数复制文件夹。
输入:
mkdir dir1
输入:
cp -r dir1 dir1_cp
输入:
ls -l
输出:
total 0
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:06 dir1
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:07 dir1_cp
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file2
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file3
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file4
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test
通过-r参数可以复制文件夹,也可以添加-a参数来复制文件夹的所有属性。
输入:
cp -a -r dir1 dir1_cp_a
输入:
ls -l
输出:
total 0
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:06 dir1
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:07 dir1_cp
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:06 dir1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file2
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file3
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file4
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test
文件移动
文件移动命令mv,通过mv移动文件,类似于Windows的剪切,同时做剪切过程中也可以进行文件的重命名。
可以使用mv -help查看使用规则,输入:
mv --help
步骤1 文件移动
将文件file1移动至文件夹dir1中。
输入:
mv file1 dir1/
输入:
ll dir1/
输出:
total 0
drwxr-xr-x 2 batchcom batchcom 19 Apr 1 13:09 ./
drwxr-x--- 1 batchcom batchcom 226 Apr 1 13:09 ../
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
mv的第一个参数为对应的文件路径,第二个参数为目标路径。如果第二个参数为目录路径,那么移动之后的文件名称保持一致;如果第二个参数为文件路径,那么移动之后的文件名称会自动重命名。
输入:
mv file1_cp dir1/file1_cp_mv
输入:
ll dir1
输出:
total 0 drwxr-xr-x 2 batchcom batchcom 38 Apr 1 13:10 ./ drwxr-x--- 1 batchcom batchcom 210 Apr 1 13:10 ../ -rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1 -rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp_mv
移动完成之后原来的文件就会消失。
输入:
ll
输出:
total 0
drwxr-xr-x 2 batchcom batchcom 38 Apr 1 13:10 dir1
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:07 dir1_cp
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:06 dir1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file2
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file3
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file4
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test
步骤2 文件夹移动
将文件夹dir1_cp移动至dir1。
输入:
mv dir1_cp dir1
输入:
ll dir1
输出:
total 0
drwxr-xr-x 3 batchcom batchcom 53 Apr 1 13:11 ./
drwxr-x--- 1 batchcom batchcom 195 Apr 1 13:11 ../
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:07 dir1_cp/
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp_mv
文件夹的移动与文件移动是一样的。是否会自动重命名看第二个参数是什么路径。如果是目录路径则不重命名,如果不是则会自动重命名。
步骤3 移动文件夹内某些文件
移动dir1中的file开头的文件至文件夹dir1_cp_a中。
输入:
mv dir1/file* dir1_cp_a
输入:
ll dir1
输出:
total 0
drwxr-xr-x 3 batchcom batchcom 21 Apr 1 13:11 ./
drwxr-x--- 1 batchcom batchcom 195 Apr 1 13:11 ../
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 13:07 dir1_cp/
输入:
ll dir1_cp_a
输出:
total 0
drwxr-xr-x 2 batchcom batchcom 38 Apr 1 13:11 ./
drwxr-x--- 1 batchcom batchcom 195 Apr 1 13:11 ../
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file1_cp_mv
通过name*的方式去匹配文件夹中以name为开头的文件。
文件删除
通过命令rm做文件删除操作。
可以通过rm --help查看使用规则,输入:
rm --help
步骤1 删除文件
删除文件file2
输入:
rm file2
输入:
ls -l
输出:
total 0
drwxr-xr-x 3 batchcom batchcom 21 Apr 1 13:11 dir1
drwxr-xr-x 2 batchcom batchcom 38 Apr 1 13:11 dir1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:01 file1_cp_a
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file3
-rw-r--r-- 1 batchcom batchcom 0 Apr 1 13:04 file4
drwxr-xr-x 1 batchcom batchcom 24 Apr 1 09:59 log
drwxr-xr-x 2 batchcom batchcom 6 Apr 1 12:57 mkdir_test
rm命令紧跟文件的路径,表示删除对应该路径下的文件。
步骤2 删除多个文件
删除文件file3 file4
输入:
rm file3 file4
输入:
ls
输出:
dir1 dir1_cp_a file1_cp_a log mkdir_test
rm 后面紧跟多个文件可以删除多个文件。如果文件有规律,也可以使用通配符*的方式来匹配。
rm file*
步骤3 删除文件夹
删除文件夹dir1。
输入:
rm -r dir1
输入:
ls
输出:
dir1_cp_a file1_cp_a log mkdir_test
rm默认只能删除文件,通过参数-r可以删除文件夹。
rm 删除操作之后文件就丢失了,因此在操作过程中做删除操作的时候需要多多注意待删除的目标文件或者文件夹的路径是否正确。
rm -r也支持删除多个文件夹。需要在后面紧跟多个文件夹,也可以使用通配符来匹配。
文件查看
文件查看的命令主要介绍三个,一个是cat,一个是head,一个是tail。
cat命令主要是查看文件的所有内容;head用作查看文件的头部内容;tail用作查看文件的末尾内容。
步骤1 cat查看文件
查看系统环境变量文件/etc/profile。
输入:
cat /etc/profile
输出:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "$(id -u)" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
cat命令紧跟需要查看的文件路径,这个路径可以是绝对路径也可以是相对路径。
cat 详细参数:
输入:
cat --help
步骤2 head查看文件
查看环境变量文件/etc/profile前5行的信息。
输入:
head -5 /etc/profile
输出:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
通过-5的方式来指定需要查看多少行的数据(不指定参数默认查看前10行的数据)。
head命令参数详情:
输入:
head --help
步骤3 tail查看文件
查看环境变量文件/etc/profile末尾5行的信息。
输入:
tail -5 /etc/profile
输出:
. $i
fi
done
unset i
fi
通过-5的方式来指定需要查看末尾多少行的数据(不指定参数默认查看末尾10行的数据)。
tail命令参数详情:
输入:
tail --help
tail 还有一个比较常用的操作是使用参数-f,可以实时监控查看的文件,文件一旦新增了内容就会将新增的内容显示出来。
实验小结
本次实验通过敲击一些常用的Linux命令来辅助学习Linux的基础操作,主要有浏览文件、创建文件、文件操作,为后续的Linux高阶语法奠定基础。