how to rename two files to different name automatically in bash - bash

I have a problem that I want to know how to do it automatically in bash:
I have 12 folders(0,1,2,3..11), each contain two files like shown below, for example, in folder 1, it contains:
4DNFI6E4RZ9H.fastq.gz
4DNFIIN1NES7.fastq.gz
and none of the files in the folders share the same name.
I want to create a script that can loop into each of the folders, and rename the files according to the name of the folders, for example for files in folder 1, I want the two files to be renamed to:
1_R1.fastq.gz
1_R2.fastq.gz
for files in folder 2, I want the two files to be renamed to:
2_R1.fastq.gz
2_R2.fastq.gz
...
So how to do it? thanks a lot!

Like this :
#!/bin/bash
for dir in */; do
c=0
for file in $dir/*; do
mv "$file" "${file%/}_R$((++c)).fastq.gz"
done
done
ls *

Related

How do I copy files listed in a .txt into a new folder?

I have a .txt list of filenames that I would like to copy into a new folder. The filenames take the form: 0-01-199898988999.mp4. I can use the list of file names to copy files into a new folder using the following code.
cp `cat list.txt` new_folder/
However, this only works when the files I want to copy, the .txt file, and the new folder are all in the same directory, and all of my files to copy are organized into subfolders within the directory.
FolderA
->list.txt
->new_folder
->SubA
-->0-01-199898988999.mp4
-->0-02-199898988999.mp4
-->0-03-199898988999.mp4
->SubB
-->0-04-199898988999.mp4
-->0-05-199898988999.mp4
-->0-06-199898988999.mp4
-->0-06-199898988999.mp4
How can I modify the code to search through the subdirectories of folder A?
I've tried using --parents, but it hasn't worked.
cp -v --parents `cat list.txt` /new_folder
I've been searching around for answers, but most versions of this problem involve a list with the unique directory names for each file (which I don't have), copying all the files within the subdirectories, or searching by extension instead of file name.

"for loop" to run rsync on relevant folders only

I have a folder contains around 650 folders (source), I generated a list of the relevant folders i want (final.txt).
i am trying to use a "for loop" to copy only the relevant sub-folders to a new location (target).
i keep getting the original content of the "source" copied to the "target".
i run:
for var in `cat final.txt` ; do rsync -ah $var source/ target/ ; done
I tried different syntax but can't seem to get what I need.
what am i doing wrong?
I expect to copy only the folders which name is in the final.txt list copied to the target (all "names" in the file are a single word, matching to some of the folder names for exactly)
ok after messing around I should have ran this (it works)
for var in `cat final.txt` ; do rsync -ah source/$var target/ ; done

Bash, move and rename files to base parent directory name

I have a bunch of base-parent-folders with different names.
In each of these base-parent-folders i have a folder with same same name(result).
In each of the result folders, i have a result file (data.txt), also named the same in each base-parent-folder.
I need to move all the data.txt files to a new folder (newfolder), and rename them to the base-parent-folder name.
for name in ./*/*/data.txt; do
mv "$name" "../newfolder/$(basename -- "$(dirname -- "$name")").txt";
done
This will move the file but rename the data.txt files to result.txt, and not the unique base-parent-folder name.
Help is much appreciated :D
What i have:
data\data1\result\data.txt
data\data2\result\data.txt
data\data3\result\data.txt
data\data4\result\data.txt
data\data5\result\data.txt
What i want:
data\newfolder\data1.txt
data\newfolder\data2.txt
data\newfolder\data3.txt
data\newfolder\data4.txt
data\newfolder\data5.txt
for f in */*/data.txt;
do mv "$f" "./newfolder/${f%/*/*}.txt";
done
Worked for me. The ./ part did not work. It wanted to add a additional folder called . when moving the file.
Thanks for the help

Mac OS - Batch Rename All Files in Folder but Disregard All SubFolders

I have a bunch of folders that I would like to rename all the files contained within minus any subdfolders.
For example lets say I have two parent folders:
ParentFolder1 - [PF1]
ParentFolder2 - [PF2]
Each parent folder has various amounts of subfolders:
SubParentFolder1_1
SubParentFolder1_2
SubParentFolder2_1
Inside the ParentFolder and each SubParentFolder there can be files such as .mp3, .txt. etc. or more subfolders.
How would I go about renaming all and any files in this manner:
example.mp3 -> example - [PF1]
example.txt -> example - [PF2]
example.docx -> example - [PF2]
Appreciate any input!
This is a way to list files (not folders) in a range of directories and then do something with them... Specifics of renaming are up to you.
for FOLD in Parent*;
do for FILE in $(ls -p $FOLD | grep -v "/");
do echo "$FOLD/$FILE" "$FOLD/${FILE%.*}";
done; done;
Explanation:
For each folder (FOLD) in directories matching the wildcard Parent*,
list the contents, adding / to the end of directory names.
Do inverse grep on that list, leaving just the file names.
Loop through each FILE and echo out the original folder+file, followed by the folder and file with the suffix removed by patten matching.
Once you test this, you can replace echo with mv to do the actual renaming... (I've put these on separate lines to make them more readable, but would usually run this as one long command.

copying files into named directories in bash

I have a directory structure like so
/dir01/dir02/files
I want to copy the first file in dir02 onto a separate drive and into a directory with the same name as dir01
I wrote the following script
while [ "${*}" != "" ] ; do
INPUT="${1}"
FOLDER="${INPUT}"/*DPX
TARGET_FOLDER="/Users/user/Desktop/folder"/$(basename "${INPUT}")
for file in "${FOLDER}"; do
echo cp "$file" "${TARGET_FOLDER}"
break 1
done
done
Here INPUT is dir01 , FOLDER is dir02 and TARGET_FOLDER is the new directory with the same name as dir02 I want the file to copy to.
When I run the script it looks for a folder named *DPX in the INPUT path, which doesn't exist. There are many folders in the INPUT directory named *DPX and I want it to pull the first file from all of them.
Try replacing your for with:
for file in "$INPUT"/*DPX/*
Notes:
Your version is looking for a file called *DPX because ${FOLDER} is quoted on the for line.
for f in "$dir" will execute the for loop once, with f=$dir. To look for files under $dir, you need another /*.
Also, you want a shift before the last done.

Resources