gpt4 book ai didi

linux - 如何在没有文件全名的情况下检查文件是否存在

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:29 24 4
gpt4 key购买 nike

我正在尝试制作一个 linux 脚本来更新一些列表,其中带有时间戳

我正在努力的部分是:

for dir in $(ls)
do
echo $dir
done

我得到:

noreply-unsch-simco 2014-03-17-14:20:41.txt

noreply-unsch-simcoe 2014-03-17-14:20:41.txt

noreply-unsch-sudbury-nbay 2014-03-17-14:20:41.txt

noreply-unsch-test 2014-03-17-14:20:41.txt

noreply-unsch-thunderbay 2014-03-17-14:20:41.txt

noreply-unsch-toronto 2014-03-17-14:20:41.txt

noreply-unsch-york 2014-03-17-14:20:41.txt

我想要做的是删除旧文件(如果它们存在),因为每次我的脚本运行时,它都会创建一个带有新时间戳的新文件。

所以基本上我想知道如何仅使用名称的开头部分(时间戳之前的所有内容)搜索文件。

我是新手。请帮助我

编辑:好的,所以我查看了评论,但出现错误。这是我的完整代码。

#!/bin/bash
cd /var/lib/mailman/lists/
variable=$(date +"%Y-%m-%d-%H:%M:%S")

for dir in $(ls)
do
echo $dir
rm /root/Desktop/Dan/Lists/$dir-*
echo echo | cat | list_members $dir/ > /root/Desktop/Dan/Lists/$dir" "$variable.txt
done

我遇到了找不到目录错误。

编辑我只需要去掉'-'

 rm /root/Desktop/Dan/Lists/$dir*

谢谢!

最佳答案

如果你想删除所有以特定扩展名开头的文件,你可以这样做:

rm noreply-unsch-*

这将删除所有匹配该模式的文件。如果你想做的不仅仅是删除 ;您可以将循环更改为:

for filename in noreply-unsch-*.txt; do
#something
rm $filename
done

关于linux - 如何在没有文件全名的情况下检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com