Bash-如何在名称不断变化时搜索文件

时间:2016-03-28 20:22:42

标签: bash

我在bash中遇到了我的代码问题。

我希望我的代码检查某个目录中是否存在某个文件,但由于该文件会不断更改其名称,我想搜索他的"扩展名"。

基本上,当我启动我的脚本时,它将检查目录是否存在,如果它执行它将创建它,然后我想检查是否有任何文件以"开头。&#34 ; (点)在该目录中,如果它存在,它将创建一个名为" .200"

的空文件

这是代码。

#!/bin/bash

directory=$HOME/.impressora
mkdir -p $directory
if [ ! -f "$directory/.*" ]
then echo "" > $directory/.200

事情是,在此之后,我添加了一堆代码,这些代码将不断更改该文件的名称,例如,它将来自" .200" ,到" .199",到" .198" ,到" .197"等...

所以在我的"如果"声明我必须搜索目录是否有任何以"开头的文件。" (点)。

我已经使用了命令" shopt -s dotglob"和" shopt -u dotglob" 让他忽略"。"和" .."文件,默认在该目录中的puten。

我尝试通过添加名为" file"的变量来测试它。并尝试使其具有" .200"的扩展值。文件,然后打印它,但存储在该变量中的值是"。*"而不是" 200"我想要...... 这是带变量的代码。

Directory=$HOME/.impressora
file=$HOME/.impressora/.*
echo "$file"
mkdir -p $directory
if [ ! -f "$directory/.*" ]
then echo "" > $directory/.200
fi

我希望你能帮助我,我一直在拼命想解决这个问题...... 谢谢。

4 个答案:

答案 0 :(得分:0)

您可以将'.source.go': 'Main': 'prefix': 'main' 'body': 'main' 命令传递给find,它可以使用正则表达式。此命令将告诉您目录中有多少个文件以句点开头并包含至少一个数字(而不是字母):

grep

然后,您可以检查find testing/ -printf "%f\n" | grep -c '\.[0-9]\+$' 结果,看它是否大于0.

(根据您的“查找”版本,您也可以使用正则表达式directly in the find command。另一个警告:需要转义grep,而不是转义+ ,可能会因您的操作系统而异。)

答案 1 :(得分:0)

您可以在首次创建文件时记录该文件的inode。重命名文件时,inode不会更改:

#!/bin/bash

directory=$HOME/.impressora
if [ ! -d "$directory" ]; then
    # create the directory and create the initial file
    mkdir -p $directory
    touch $directory/.200

    # record the file's inode
    stat --format=%i $directory/.200 > $directory/.inode
fi

# retrieve the file's path using its inode value
filepath=$(find $directory -inum $(cat $directory/.inode))
echo "\$filepath: $filepath"

尝试重命名$directory/.200并再次运行此脚本以验证是否可以使用其inode找到该文件。

答案 2 :(得分:0)

所以我一直试图理解和试验你们给我的解决方案,但是我告诉过你我在shell脚本中真的很棒,所以我无法实现你们给我的解决方案。 因此,让我尝试用简单的人类语言分解我的代码,希望你能帮我把它变成shell语言。我会考虑每一行代码,因为我不希望你理解它的作用,但是因为我认为它会做的可能与它真正做到的xD不同。我不知道我的英语是否正确,但如果我感到困惑,我很抱歉。

所以就这样了。

!#/bin/bash

directory=$HOME/.impressora
# stores the path $HOME/.impressora in the variable directory

cred=0

mkdir -p $directory
# check if the path $HOME/.impressora exists, if it doesen't create it

if [ ! -f "$directory/.*" ]
#now at this part i want the script to create a file called ".200" if 
#there is no other file started by a "." (dot) in that directory
#i cannot put a specific file name because when i run this script for the
#first time on a fresh computer i want it to create a .200 file wich,
#afterwards will be renamed over and over again

then echo "" > $directory/.200
# so if there is no file started by a "." (dot) on that directory,
#create a file called ".200"
fi

creditos=$(echo $directory/.* | cut -c7-)
#now i want to set the variable creditos = to the name of the file in the
#directory we just created but without the "." (dot).
#so the first time this program runs i want creditos to be = to 200
#but then when i change the name of the file to ".199" i want the variable
#creditos to be = to 199 and so on...

if [ $1 = "lp" ]
#now this script is called "project" so if i type "./project lp" 
#in the terminal do...

then
if [ -d $directory ] && [ $creditos > 0 ]
#if the directory exists and the variable "creditos" is grater than 0...

then
    echo "" > $directory/lp-$imp
    #create an empty file called "lp-the value of a varable wich needs to
    #increment by 1 each time i run the command "./project lp"
    #this is another thing i dont know how to do, how do i implement a
    #variable in wich her value is persistent?
    #the objective is, each time i type "./project lp" in the console
    #if the directory exists and the variable creditos is greater than 0
    #create a file called "lp-1", "lp-2" , "lp-3" and so on

    $cred=$creditos
    # on the first usage of the script "$cred" = 200
    ((cred--))

    mv $directory/.$creditos $directory/.$cred
    #this is my way to rename the file, so it would go from ".200"
    #to ".199" and so on
else
    echo "Diretorio inexistente ou creditos insuficientes"
    #if the directory doesent exist or the variable creditos
    #is lower than 200, print that message
fi
fi

抱歉一堆文字,但我没有更好的方法来准确解释我需要什么,我希望你能帮助我! 非常感谢您的时间!

答案 3 :(得分:0)

由于文件没有名称(因为它以点开头),它自动显示在您的目录中的第一个文件就是那个。首先,您需要" ls -a "你的目录。 接下来你需要跳过两个"文件"出现的是:

  

  

...

所以,你做" 头-3 "命令,以便您可以选择文件夹的3个第一行(文件)。 接下来,您需要选择这3个中的最后一个,以便再次进行管道处理并执行" tail -1 "

最后你需要删除.200文件中的点,这样你只能将值保存到var,为此你需要管道" cut -f2 -d"。& #34; "

基本上你需要更换

creditos=$(echo $directory/.* | cut -c7-)

creditos=$(ls -a $directory | head -3 | tail -1 | cut -f2 -d".")