学习 SHELL

  1. 编写一个命令,它可以递归地查找文件夹中所有的HTML文件,并将它们压缩成zip文件。注意,即使文件名中包含空格,您的命令也应该能够正确执行
1
find . -type f -name "*.html" | xargs -d '\n' -I{} zip html_files.zip {}
  1. 编写两个bash函数 marcopolo 执行下面的操作。 每当你执行 marco 时,当前的工作目录应当以某种形式保存,当执行 polo 时,无论现在处在什么目录下,都应当 cd 回到当时执行 marco 的目录。 为了方便debug,你可以把代码写在单独的文件 marco.sh 中,并通过 source marco.sh命令,(重新)加载函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

# 保存当前工作目录
marco() {
export MARCO_DIR=$(pwd)
}

# 返回到 marco 保存的目录
polo() {
cd $MARCO_DIR
}
# 1.首先运行source marco.sh,然后在当前目录运行marco
# 2.cd 到任意目录,运行polo,就会返回到当时执行 `marco` 的目录

image-20230902212716936

  1. 编写一个命令或脚本递归的查找文件夹中最近使用的文件。更通用的做法,你可以按照最近的使用时间列出文件吗?
1
2
3
4
5
#!/bin/bash

type=$1 # m-modified时间 a-访问时间

find . -type f -print0 | xargs -0 stat -f '%${type}%t%S${type} %N' | sort -nr