Hi there,
I would write a bash script to format then create a range of folder (folder name is number) to a usb stick.
for numbers in $(seq -w 001 999)
do
pause "Press any key to start"
mkfs.vfat -F32 /dev/sdc1
mount /dev/sdc1 /media/usb
mkdir /media/$numbers
umount /dev/sdc1
done
But its not effectively because I only can create one folder in one time.
and sometimes write fail because usb is not ready
If I plug 10 usb stick in hub, how can I modify script to create folder on multiple disk in same time?
thanks~
I think there is something wrong with your script!
If you are mounting your device to: /media/usb, why you are creating the directories like this: "mkdir /media/$numbers", it should be: "mkdir /media/usb/$numbers". Also, if I understood what you want, why you are formatting your usb every loop?
Try this:
checkRetCode ()
{
lstRetCode=${?}
lineNo="${1}"
if [ ${lstRetCode} -ne 0 ]
then
echo "Last command returned and invalid code: [${lstRetCode}]. Line number: [${lineNo}]."
exit 1
fi
}
processUsbDev ()
{
usbDevName="${1}"
baseMountPoint="${2}"
numDirs="${3:-999}"
numRetries="${4:-3}"
usbDev="/dev/${usbDevName}"
echo "Formatting usb disk: [${usbDev}]"
mkfs.vfat -F32 "${usbDev}"
checkRetCode "${LINENO}"
mountPoint="${baseMountPoint}/${usbDevName}"
if [ ! -d "${mountPoint}" ]
then
mkdir "${mountPoint}"
checkRetCode "${LINENO}"
fi
echo "Mounting disk: [${usbDev}] under: [${mountPoint}]"
mount ${usbDev} ${mountPoint}
checkRetCode "${LINENO}"
countDirs=1
echo "Creating directories under: [${mountPoint}] - [${countDirs}][${numDirs}]..."
while [ ${countDirs} -le ${numDirs} ]
do
countDirStr=`echo "${countDirs}" | awk '{printf("%03d\n", $0)}'`
createDir="${mountPoint}/${countDirStr}"
countRetries=0
while true
do
mkdir "${createDir}"
retCode=${?}
if [ ${retCode} -ne 0 ]
then
if [ ${countRetries} -ge ${countRetries} ]
then
echo "Failed to create directory: [${createDir}]!"
break
fi
else
break
fi
countRetries=`expr ${countRetries} + 1`
done
countDirs=`expr ${countDirs} + 1`
done
umount ${usbDev}
checkRetCode "${LINENO}"
}
echo "Press any key to start!"
read
processUsbDev "sdc1" "/media/usb" "999" "3"
I was unable to test it, but should work!
1 Like
Hi felipe,
thanks for your script, it's easy to understand.
But after run this script, folders were only created on a specify usbdevname,
My idea is to create one folder on each one USB stick.
For example, I plug 4 usb on my pc, when I press any key, folder was created to each USB and name will be 001(usb1) 002(usb2) 003(usb3) 004(usb4).
On next action, script will keep mkdir until the end of number.
Is that possible?
So, based on what you want, the 005 will be back to usb1?
001(usb1) 002(usb2) 003(usb3) 004(usb4)
005(usb1) 006(usb2) 007(usb3) 008(usb4)
...
Is that correct?
yes this is what I need, four USB work on the same time will be better, or one by one also good for me.
Try this:
checkRetCode ()
{
lstRetCode=${?}
lineNo="${1}"
if [ ${lstRetCode} -ne 0 ]
then
echo "Last command returned and invalid code: [${lstRetCode}]. Line number: [${lineNo}]."
exit 1
fi
}
processUsbDev ()
{
usbDevNamesCSV="${1}"
baseMountPoint="${2}"
numDirs="${3:-999}"
numRetries="${4:-3}"
usbDevBase="/dev"
OIFS="${IFS}"
IFS=","
for usbDevName in ${usbDevNamesCSV}
do
usbDev="${usbDevBase}/${usbDevName}"
echo "Formatting usb disk: [${usbDev}]"
mkfs.vfat -F32 "${usbDev}"
checkRetCode "${LINENO}"
mountPoint="${baseMountPoint}/${usbDevName}"
if [ ! -d "${mountPoint}" ]
then
mkdir "${mountPoint}"
checkRetCode "${LINENO}"
fi
echo "Mounting disk: [${usbDev}] under: [${mountPoint}]"
mount ${usbDev} ${mountPoint}
checkRetCode "${LINENO}"
done
IFS="${OIFS}"
countDirs=1
countDevs=1
numOfDevs=`echo "${usbDevNamesCSV}" | awk -F"," '{print NF}'`
while [ ${countDirs} -le ${numDirs} ]
do
[ ${countDevs} -gt ${numOfDevs} ] && countDevs=1
countDirStr=`echo "${countDirs}" | awk '{printf("%03d\n", $0)}'`
usbDevName=`echo "${usbDevNamesCSV}" | awk -F"," -v usbDev="${countDevs}" '{print $usbDev}'`
mountPoint="${baseMountPoint}/${usbDevName}"
createDir="${mountPoint}/${countDirStr}"
echo "Creating directory under: [${mountPoint}] - [${countDirStr}][${usbDevName}]..."
countRetries=0
while true
do
mkdir "${createDir}"
retCode=${?}
if [ ${retCode} -ne 0 ]
then
if [ ${countRetries} -ge ${countRetries} ]
then
echo "Failed to create directory: [${createDir}]!"
break
fi
else
break
fi
countRetries=`expr ${countRetries} + 1`
done
countDevs=`expr ${countDevs} + 1`
countDirs=`expr ${countDirs} + 1`
done
OIFS="${IFS}"
IFS=","
for usbDevName in ${usbDevNamesCSV}
do
usbDev="${usbDevBase}/${usbDevName}"
umount ${usbDev}
if [ ${?} -ne 0 ]
then
echo "Failed to umount: [${usbDev}]."
fi
done
IFS="${OIFS}"
}
echo "Press any key to start!"
read
processUsbDev "sdc1,sdc2,sdc3,sdc4" "/media/usb" "999" "3"
And a sample output:
Press any key to start!
Formatting usb disk: [/dev/sdc1]
Mounting disk: [/dev/sdc1] under: [/media/usb/sdc1]
Formatting usb disk: [/dev/sdc2]
Mounting disk: [/dev/sdc2] under: [/media/usb/sdc2]
Formatting usb disk: [/dev/sdc3]
Mounting disk: [/dev/sdc3] under: [/media/usb/sdc3]
Formatting usb disk: [/dev/sdc4]
Mounting disk: [/dev/sdc4] under: [/media/usb/sdc4]
Creating directory under: [/media/usb/sdc1] - [001][sdc1]...
Creating directory under: [/media/usb/sdc2] - [002][sdc2]...
Creating directory under: [/media/usb/sdc3] - [003][sdc3]...
Creating directory under: [/media/usb/sdc4] - [004][sdc4]...
Creating directory under: [/media/usb/sdc1] - [005][sdc1]...
Creating directory under: [/media/usb/sdc2] - [006][sdc2]...
Creating directory under: [/media/usb/sdc3] - [007][sdc3]...
Creating directory under: [/media/usb/sdc4] - [008][sdc4]...
Creating directory under: [/media/usb/sdc1] - [009][sdc1]...
Creating directory under: [/media/usb/sdc2] - [010][sdc2]...
Creating directory under: [/media/usb/sdc3] - [011][sdc3]...
Creating directory under: [/media/usb/sdc4] - [012][sdc4]...
Creating directory under: [/media/usb/sdc1] - [013][sdc1]...
Creating directory under: [/media/usb/sdc2] - [014][sdc2]...
Creating directory under: [/media/usb/sdc3] - [015][sdc3]...
Creating directory under: [/media/usb/sdc4] - [016][sdc4]...
Creating directory under: [/media/usb/sdc1] - [017][sdc1]...
Creating directory under: [/media/usb/sdc2] - [018][sdc2]...
Creating directory under: [/media/usb/sdc3] - [019][sdc3]...
Creating directory under: [/media/usb/sdc4] - [020][sdc4]...
If you want to know all devices that are USB, check this link: http://stackoverflow.com/questions/69254/how-do-i-figure-out-which-dev-is-a-usb-device
I hope it helps!
GREAT felipe ,
it works for me.
could you please help to insert a pause command?
I need to plug another four usb stick ~ :o
Press any key to start!
Formatting usb disk: [/dev/sdc1]
Mounting disk: [/dev/sdc1] under: [/media/usb/sdc1]
Formatting usb disk: [/dev/sdc2]
Mounting disk: [/dev/sdc2] under: [/media/usb/sdc2]
Formatting usb disk: [/dev/sdc3]
Mounting disk: [/dev/sdc3] under: [/media/usb/sdc3]
Formatting usb disk: [/dev/sdc4]
Mounting disk: [/dev/sdc4] under: [/media/usb/sdc4]
Creating directory under: [/media/usb/sdc1] - [001][sdc1]...
Creating directory under: [/media/usb/sdc2] - [002][sdc2]...
Creating directory under: [/media/usb/sdc3] - [003][sdc3]...
Creating directory under: [/media/usb/sdc4] - [004][sdc4]...
[NEED A PAUSE for replace new usb]
Creating directory under: [/media/usb/sdc1] - [005][sdc1]...
Creating directory under: [/media/usb/sdc2] - [006][sdc2]...
Creating directory under: [/media/usb/sdc3] - [007][sdc3]...
Creating directory under: [/media/usb/sdc4] - [008][sdc4]...
printf "Switch USB sticks and hit ENTER: "
read REPLY
Check below how to:
# Replace this line:
[ ${countDevs} -gt ${numOfDevs} ] && countDevs=1
# By
if [ ${countDevs} -gt ${numOfDevs} ]
then
countDevs=1
echo "Please, replace usb devices!"
read
fi