Reputation: 1402
I have multiple .pdb files in different folders. How to loop in through entire directory naming all files to one particular name test.pdb?
Sample structure
file1
file1.pdb
xyz.txt
file2
file2.pdb
xyz.txt
file3
file3.pdb
xyz.txt
Desired output structure
file1
test.pdb
xyz.txt
file2
test.pdb
xyz.txt
file3
test.pdb
xyz.txt
The code I am using at the moment :
for d in */ ; do
mv *.pdb test.pdb
done
But it is not working.
Upvotes: 0
Views: 51
Reputation: 14432
Alternative solution, iterating of the pdf files, instead of the folder names.
It's not clear if other pdb file, whose names does not match the folder name should be affected too. The code test for this condition. It can be removed if not needed.
for file in */*.pdb ; do
d=$(dirname $file)
b=$(basename $file .pdb)
[ "$d" = "$b" ] && mv "$d/$b.pdb" "$d/test.pdb"
done
Upvotes: 0
Reputation: 1169
Try this
for d in */; do
if [ -f $d/*.pdb ] ;then
mv $d/*.pdb $d/test.pdb
fi
done
Upvotes: 0
Reputation: 189327
You need to include the directory name. Just *.pdb
will always expand to the files matching the wildcard in the current directory.
for d in ./*/ ; do
mv "$d/"*.pdb "$d/"test.pdb
done
I added the ./
before the wildcard as a precaution too. It helps avoid problems if a file name starts with a dash, which would then get interpreted as an option to mv
without the leading dash.
Many mv
implementations will refuse to move multiple files to the same destination name, so this will probably only work if the wildcard expands to exactly one file in each directory. (Zero matches will work, but emit a noisy warning.)
Upvotes: 1