1

I'm trying to write a bash script that will look for all .txt files in a folder, grab the basename and then relocate the file. However, I'm having an issue where any time I try to combine the filename with a string, it outputs the "*.txt" wildcard from the for loop instead of the actual file it found. If I simply echo the variable, it outputs the expected filename.

I've included a simplified script below and the output it produces. I'm new to BASH and I assume this has something to do with the way it handles variables. I've been searching for an explanation for a few hours, but I haven't really come up with anything. Can anyone explain why the value changes when concatenated?

Here is the example code:

TEMP_PATH=C/Test

for testFile in $TEMP_PATH/*.txt; do
    testFileName=$(basename "$testFile")
    echo $testFileName
    echo "FileName:$testFileName"
done

Here is the example output from each echo

TestOne.txt
FileName:*.txt

Here are the files in the directory. TestingToo.sh is the script shown above

TestingToo.sh  TestOne.txt

Any help would be appreciated! Thank you.

7
  • I am unable to reproduce your results. When I run your script in described environment I got TestOne.txt FileName:TestOne.txt as expected Commented May 11, 2017 at 21:16
  • Hmm. I just tried copying my script exactly as I have it written here and ran it again. It still produces the same output for me. It would be strange, but I wonder if it's related to me running this on a windows system. I've tried with both git bash, and the windows 10 developer bash. Both produce the same results... Commented May 11, 2017 at 21:24
  • Are you sure you have some txt files under C/Test? If not, the string $TEMP_PATH/*.txt will not expand into list of files under C/Test. It will be taken "as is", so you will end up with variable testFile set to C/Test/*.txt. Next command will set testFileName to *.txt. Then, in the first echo command bash will substitute *.txt into set of matching files, that is: TestOne.txt in your current directory. But in the second echo comand there will be no file matching FileName:*.txt pattern. So bash will leave it as-is. Commented May 11, 2017 at 21:34
  • Yes, both the script and TestOne.txt are in C:\Test. The 3rd code block in the main post is output from an ls command while in /mnt/c/Test. So it seems to recognize that C:\Test and C/Test are the same location. I also tried explicitly using /mnt/c/Test for TEMP_PATH and it produces the same output. Commented May 11, 2017 at 21:49
  • What output is produced by echo C/Test/*.txt command? Commented May 11, 2017 at 21:51

1 Answer 1

1

Try this

#!/usr/bin/env bash

TEMP_PATH=~/temp
TARGET_PATH=~/temp2

filenames=`find "$TEMP_PATH" -name "*.txt" `

if [ -z "$filenames" ] ; then echo "files not found" ; exit ; fi

for testFile in $filenames; do
    testFileName=$(basename "$testFile")
    echo $testFileName
    mv "${testFile}" "${TARGET_PATH}/${testFileName}"
done
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.