I am composing a script to process 20 files. All of them located in different directories. I have partial file name.
File1 directory is /data/data1directory/Sample_File1/logs/File1_Data_time.err
File2 directory is /data/data2directory/Sample_File2/logs/File2_Data_time.err
.....
My script looks like this. (runrunrun.sh)
#!/bin/bash
INPUT=$1
mv /data/*/Sample_*/logs/*_Data_time.err /data/*/Sample_*/logs/*_Data_time_orig.err
cp /data/*/Sample_*/scripts/*.sh /data/*/Sample_*/scripts/*_orig.sh
sh /data/*/Sample_*/scripts/*_orig.sh
When running it, I tried.
./runrunrun.sh File1
. runrunrun.sh File1
sh runrunrun.sh File1
mv: cannot move /data/data1directory/Sample_File1/logs/File1_Data_time.err /data/*/Sample_*/logs/*_Data_time_orig.err
: No such file or directory
cp also got similar feedback
Am I doing it correct?
Thanks!
Let's talk about how wildcards work for a minute.
cp *.txt foo
doesn't actually invoke cp
with an argument *.txt
, if any files matching that glob exist. Instead, it runs something like this:
cp a.txt b.txt c.txt foo
Similarly, something like
mv *.txt *.old
...can't possibly know what to do, because when it's invoked, what it sees is:
mv a.txt b.txt c.txt *.old
or, worse, if you already have a file named z.old
, it'll see:
mv a.txt b.txt c.txt z.old
Thus, you need to use different tools. Consider:
# REPLACES: mv /data/*/Sample_*/logs/*_Data_time.err /data/*/Sample_*/logs/*_Data_time_orig.err
for f in /data/*/Sample_*/logs/*_Data_time.err; do
mv "$f" "${f%_Data_time.err}_Data_time_orig.err"
done
# REPLACES: cp /data/*/Sample_*/scripts/*.sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*.sh; do
cp "$f" "${f%.sh}_orig.sh"
done
# REPLACES: sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*_orig.sh; do
if [[ -e "$f" ]]; then
# honor the script's shebang and let it choose an interpreter to use
"$f"
else
# script is not executable, assume POSIX sh (not bash, ksh, etc)
sh "$f"
fi
done
This uses a parameter expansion to strip off the tail end of the old name before adding the new name.