Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a count variable in a file name

I have a quick question. I just wanted to know if it was valid format (using bash shell scripting) to have a counter for a loop in a file name. I am thinking something along the lines of:

for((i=1; i <=12; i++))
do
  STUFF
  make a file(i).txt
like image 963
Stephopolis Avatar asked Jun 22 '12 18:06

Stephopolis


People also ask

How do I count the number of characters in a file name?

type the following command dir /s /b > output. csv file in excel. use the =LEN() function in excel to count the number of characters per row as listed in the output.

How do I count files using grep?

If you want to count only files and NOT include symbolic links (just an example of what else you could do), you could use ls -l | grep -v ^l | wc -l (that's an "L" not a "1" this time, we want a "long" listing here). grep checks for any line beginning with "l" (indicating a link), and discards that line (-v).


1 Answers

Here's a quick demonstration. The touch command updates the last-modified time on the file, or creates it if it doesn't exist.

for ((i=1; i<=12; i++)); do
   filename="file$i.txt"
   touch "$filename"
done

You may want to add leading zeroes to the cases where $i is only one digit:

for ((i=1; i<=12; i++)); do
   filename="$(printf "file%02d.txt" "$i")"
   touch "$filename"
done

This will result in file01.txt, file02.txt, and so on, instead of file1.txt, file2.txt.

like image 59
Michael Hoffman Avatar answered Oct 28 '22 12:10

Michael Hoffman