Linux.com

Home Linux Community Forums Software Command Line trouble with name increment

trouble with name increment

Link to this post 16 Jan 12

Ok thank you again.
For my problem instead of using an index, I'll use a date based extension to the name like this :

ffmpeg -f alsa -ac 1 -i hw:1,0 -acodec flac -f x11grab -s $LARGEUR"x"$HAUTEUR -i :0.0+$DECALAGE_FENETRE -r 15 -vcodec libx264 -vpre lossless_ultrafast  -y video`date +%Y`.`date +%m`.`date +%H`.`date +%M`.`date +%S`.mkv &

Link to this post 16 Jan 12

The choice to use a date based assignment is better for organization and it should lead to no duplication, but it would not hurt to build error handling for duplicate names as a cautionary tool. Do you know how to use bash to check for existing files?

Link to this post 17 Jan 12

I read a bit about it but I cannot pretend I master it.
I am very interrested in learing more as I am a beginner with bash scipting

A few examples would be welcome.

Link to this post 17 Jan 12

Then here is a good example of filename error handling and implementing a formatted sequence number to force creation of a new valid name.

#!/bin/bash

filedir="${HOME}/testscripts/emptydir/"
filename="testfile"
fileext=".txt"

# format the date as year-month-date so it can be easily sorted
dt=$(date +"%y-%m-%d")

# loop through from 1 to 999
# adding each three digit number to the filename
# until a number that does not exist if found,
# then make the file with the available name

# use the seq command to increment a number, it is being formatted
# as a 3 digit number from 1 to 999
for seqnum in $(seq -f "%03g" 1 999)
do
# set a filename including the directory, base name,
# date, sequence number and the extension
fullfile="${filedir}${filename}-${dt}-${seqnum}${fileext}"

# test the see if the file exist, first test to see
# if it does not exist
#
# the ! means NOT
# the -f tests to see if the file exists
# so "! -f" means check to see if the file does not exist
#
if [ ! -f ${fullfile} ]
then
# since the file does not exist, make it
touch ${fullfile}
echo " ${fullfile} has been created"

# exit the loop since the file has been created
break
fi
done

Link to this post 17 Jan 12

Thank you for this. I think I understand. I just have a little problem understanding the letter g in %03g (I am not a computer scientist even if already wrote programms in Java and C++). . I think I have to read the c printf function quietly.

Link to this post 18 Jan 12

Honestly it does not match the proper printf floating point syntax, I have been using it for a while after I found it on the net. I am assuming that the synax is stating to have leading zeros while forcing whole numbers to be three characters long.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board