Rename files with loop variable name

I am trying to download a bunch of files and rename them as I go. the download portion works correctly, but i can't get it to rename the files. This is the script I'm using:

I am trying to download a bunch of files and rename them as I go. the download portion works correctly, but i can't get it to rename the files. This is the script I'm using:

COUNTER=0
for (( i = 696; i <= 773; i++ ))
  do
  fastq-dump --split-3 --accession SRR546$i
mv SRR546"$i"_1 ./"$COUNTER"mVD_SRR546$i_1
mv SRR546"$i"_2 ./"$COUNTER"mVD_SRR546$i_2
gzip *.fastq

COUNTER=$[COUNTER + 1]
done

This will correctly download the file SRR546696, SRR546697 etc. but does nothing to rename them.

I also tried using:

rename 's/SRR/"$COUNTER"mVD_SRR/' *.fastq

but this also did nothing.

Bash: Array empty outside of the for loop

I'm creating a script to rotate my backups, one important part of this script is to find the files based on certain criteria and then move them or erase them.

I'm creating a script to rotate my backups, one important part of this script is to find the files based on certain criteria and then move them or erase them.

I've got the loop going, but the Array (path_array), can't be used outside the loop. I've read several posts about this, but not sure how the answers apply to my specific case. See code below.

Thanks!!!

#!/bin/bash
anos=(2016 2017 2018 2019)
meses=(02)

meses=(01 02 03 04 05 06 07 08 09 10 11 12)
anos=(2018)

source="/volume1/NetBackup/Servers/MIA/"
destination="/volume1/NetBackup/Servers/MIA/_Archive"

######## Pasar los Files del Primer dia del Año a Archiving

for i in ${anos[@]}; do
for j in ${meses[@]}; do
month_start=$(date +$i-$j-01)
month_finish=$(date +$i-$j-02)
# echo $month_start
# echo $month_finish
path_array=(find $source -type f -not -path "*/_Archive/*" -newermt $month_start ! -newermt $month_finish | cut -sd / -f 6-)
# echo $path_array

    # echo Archivos año: $i mes: $j

    #printf '%s\n' "${path_array[@]}"
done

done

printf '%s\n' "${path_array[@]}"


Linux Tutorial - Shell Bash Commands from the Beginning with Examples

Linux Tutorial - Shell Bash Commands from the Beginning with Examples

Linux Tutorial - Shell Bash Commands from the Beginning with Examples. Learn BASH command interpreter explained step by step and with examples. Learn what Linux is and how the command interpreter fits into the operating system. Get and install Linux. Understand the structure and syntax of Linux commands. Learn to manage the elements of the filesystem: files and directories. Learn to install software from official servers or from any compatible file.

Linux Tutorial - Shell Bash Commands from the Beginning with Examples

Learn BASH command interpreter explained step by step and with examples.

In this course you will learn to use a command interpreter in Linux. I will teach you all the necessary concepts from the beginning and step by step, so you will have a good base on which to expand your skills with this powerful way of using an Operating System. Whether you are initiated in its use or if you have never seen it, you will take advantage of the explanations and exercises that I am going to teach you.

In the first topic I will explain what Linux is and how the command interpreter fits into the operating system. You will also see how to install it using a virtual machine so you can do all the practices and experiments you want without fear that your computer will suffer some damage.

In the second block you will learn to manage the elements of the filesystem: files and directories. You will understand how they are organized and you can move about the directory structure quickly and effectively. You will also learn how to create, change or delete both files and folders.

Like Linux in a multi-user system, in the third topic you will know how users, groups and permissions are organized.

Finally you will learn to install software from official servers or from any compatible file. You will hallucinate with the amount of free programs that you can enjoy in a few seconds.

Do not hesitate, take the course and lose your fear of the console mode, your computer skills will grow exponentially!

What you'll learn

  • 🐧 Get and install Linux
  • 🏼 Understand the structure and syntax of Linux commands
  • ❓Get quick help from any system command
  • 📂 Manage your files and folders quickly and effectively
  • 🛠 Use all the tools offered by the command interpreter to be more efficient using Linux
  • 💡Understand and manage concepts such as file system paths, both relative and abusive
  • 👨‍💻Create users and assign them permissions and passwords
  • 💿 Install and configure the large amount of free software offered by Linux
  • 🕸 Modify system repositories
  • *️⃣ Use wildcards and file system shortcuts
  • 💻 Know the information offered by the system prompt

Loop better: A deeper look at iteration in Python

Loop better: A deeper look at iteration in Python

Let's take a look at how that works. Using For Loops Better in Python

When we come to perform some actions over a sequence in Python, we use the for loop most of the time. The sequence is a general term, which can refer to a list, a tuple, a dictionary, a set, or a string, all of which can be used in a for loop in Python. Here’s an example of how we use the for loop, usually.

>>> students = ["John", "Mike", "Sandra", "Jennifer"]
>>> for student in students:
...     print(student)
... 
John
Mike
Sandra
Jennifer

Beyond this simple use case, there are several built-in functions that you can use together with the iteration of a sequence using the for loop. When used properly, these functions can make your code much cleaner.

1. items() and values()

Suppose we have a dictionary like the below.

>>> scores = {"John": 94, "Mike": 95, "Sandra": 98, "Jennifer": 95}

When you iterate this dictionary, the regular for loop will only retrieve the keys for the dictionary. In this case, it will only print out the name for each item. It functions the same as iterating the keys of the list.

>>> for score in scores:
...     print(score)
... 
Mike
John
Jennifer
Sandra

Instead, we can use the items() function, which will give us access to both the key and the value of each item.

>>> for score in scores.items():
...     print(score)
... 
('Mike', 95)
('John', 94)
('Jennifer', 95)
('Sandra', 98)

You can take a step further by naming the tuple’s items so that you can access both directly.

>>> for name, score in scores.items():
...     print("Student Name: " + name + ", Score: " + str(score))
... 
Student Name: Mike, Score: 95
Student Name: John, Score: 94
Student Name: Jennifer, Score: 95
Student Name: Sandra, Score: 98

Sometimes, if you’re only interested in the values of the dictionary, you can simply use the values() function.

>>> for score in scores.values():
...     print(score)
... 
95
94
95
98
2. enumerate()

Suppose we have a list like the below.

>>> grades = ["Freshman", "Sophomore", "Junior", "Senior"]

Sometimes, we want to access both the index and the item itself. In this case, you can use the enumerate() function, in which you pass the list as the parameter.

>>> for grade in enumerate(grades):
...     print(grade)
... 
(0, 'Freshman')
(1, 'Sophomore')
(2, 'Junior')
(3, 'Senior')

Similarly, you can access the index and the item separately if you refer to the tuple’s items. In addition, you can specify the enumerate() function’s start index, which is handy in our case.

>>> for year, name in enumerate(grades, start=1):
...     print("Year " + str(year) + ": " + name)
... 
Year 1: Freshman
Year 2: Sophomore
Year 3: Junior
Year 4: Senior
3. reversed()

Suppose we have a list like the below. This list shows the students who have arrived at the classroom in a temporal order.

>>> arrived_students = ["John", "Mike", "Sandra", "Jennifer"]

The teacher then wanted to check their homework, starting with the student who arrived at the latest time. So it can be done like this. We can simply call the reversed() function with the list.

>>> for student in reversed(arrived_students):
...     print(student)
... 
Jennifer
Sandra
Mike
John

One thing to note is that you want to differentiate the reversed() function from the reverse() function, with the latter reversing the order of a given list without returning the list itself. In other words, you can call arrived_students.reverse(), but you can’t iterate it directly by calling for student in arrived_students.reverse().

4. sorted()

Suppose we have a list like the below.

>>> students = ["John", "Mike", "Sandra", "Jennifer"]

The teacher wants to sort the students based on their names and perform some actions (e.g., ask a question). It can be done with the sorted() function by passing in the list of students as the parameter, which will generate the list with their names sorted in the ascending order. You can optionally set a boolean value to the reverse parameter to request the order by ascending or descending, like this: sorted(students, reverse=True).

>>> for student in sorted(students):
...     print(student)
... 
Jennifer
John
Mike
Sandra

What if the list’s items are dictionaries, like below?

>>> students = [{"name": "John", "id": 1}, {"name": "Mike", "id": 4}, {"name": "Sandra", "id": 2}, {"name": "Jennifer", "id": 3}]

Essentially, we’re sorting a list of dictionaries. This can be done by setting the key parameter using lambda in the sorted() function.

>>> for student in sorted(students, key = lambda i: i["id"], reverse=True):
...     print(student)
... 
{'name': 'Mike', 'id': 4}
{'name': 'Jennifer', 'id': 3}
{'name': 'Sandra', 'id': 2}
{'name': 'John', 'id': 1}

In the above example, we request the list be sorted by using the id key of the dictionary in the reverse order, which is clearly reflected by the printout.

If you want to sort with multiple keys, you’ll simply change the key parameter to something like key = lambda i: (i["id"], i["name"]).

5. filter()

Suppose that we still use the list of students below.

>>> students = [{"name": "John", "id": 1}, {"name": "Mike", "id": 4}, {"name": "Sandra", "id": 2}, {"name": "Jennifer", "id": 3}]

We only want to retrieve the information of the students with an even id number. This is we can conveniently get it done using the filter() function with the use of a lambda.

>>> for student in filter(lambda i: i["id"] % 2 == 0, students):
...     print(student)
... 
{'name': 'Mike', 'id': 4}
{'name': 'Sandra', 'id': 2}
6. zip()

Suppose that we have the following two lists.

names = ["John", "Mike", "Sandra", "Jennifer"]
ids = [1, 3, 2, 4]

Using the zip() function, we can conveniently use the information from both lists. Again, as before, you can always directly access the tuple’s items if you assign variable names to them, like for name, id in zip(names, ids).

>>> for student in zip(names, ids):
...     print(student)
... 
('John', 1)
('Mike', 3)
('Sandra', 2)
('Jennifer', 4)
Conclusions

The for loop is used very frequently in our code. By using the above functions in your for loop, you can potentially reduce your code length, making it easier and more fun to read!

Thank you for reading!