Reputation: 265
I have 224 pdf files and I'd like to prefix the files with a number and _
Example: stackoverflow_nov_2014.pdf
File accounts.csv contains: 2567,stackoverflow
So the goal is to take 2567 and prefix it to the pdf file with an underscore: 2567_stackoverflow_nov_2014.pdf
I think I would want to use read -r in a while loop as explained here: https://unix.stackexchange.com/questions/147569/rename-a-batch-of-files-after-reading-from-a-source-file
But when attempting this as it's written, the shell gives usage of mv command and nothing changes with the files.
Edit: Adding sample data from sources file (accounts.csv)
11,My_Golf_Shop
2567,stackoverflow
11122,Test_Store
By the way, the sources file (accounts.csv) isn't in the same order as the files in the directory as accounts.csv so somehow there would need to be matching with file name and the accounts.csv that occurs.
Upvotes: 0
Views: 77
Reputation: 1627
Below is the script that should work under the assumption: 1. All the files are under the same folder 2. For a particular prefix, this script will only rename the first found file.
#!/bin/bash
while read -r line; do
num=`echo $line |cut -f 1 -d ","`
prefix=`echo $line |cut -f 2 -d ","`
if [ -n "$num" -a -n "$prefix" ]; then
full_file=$(basename `eval find . -name '$prefix\*' -print -quit` 2>/dev/null )
mv $full_file ${num}_$full_file 2>/dev/null
fi
done < accounts.csv
Upvotes: 1