Bash, move and rename files to base parent directory name - bash

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

Related

How can I delete all files in all subdirectories with a certain name?

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.
This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.
I will need to remove this file from 260 subdirectories under the L001 directory. My directory structure is like this:
L001
C5.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
C6.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg). So maybe there is a way to provide a list of the file names I need to delete.
How can I delete these files?
find . -name "s_1_1102_c.jpg" -exec rm -f {} \;
Note: This will find and delete the file in any subdirectory of the current one. So you could execute it in L001 or wherever else you want to do this.
for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
echo rm L001/*/"$i";
done
If output looks fine, remove echo.
The final method I used to delete my files was given by #Peter - Reinstate Monica
for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done
I was concerned that my file list would be too long but it worked in this situation.

how to rename two files to different name automatically in 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 *

moving files with specific extension to directory

I am trying to move all files with a .multianno.txt from one directory to another. I thought the command below would work but maybe the * is causing problems. Thank you :).
mv /home/cmccabe/Desktop/NGS/annovar/*.multianno.txt /home/cmccabe/Desktop/NGS/API/2-12-2015/annovar/
mv: cannot stat ‘/home/cmccabe/Desktop/NGS/annovar/*.multianno.txt’: No such file or directory
directory (/home/cmccabe/Desktop/NGS/annovar)
TSVC_variants_IonXpress_001_variant_strandbias_readcount.vcf.hg19_multianno.txt
TSVC_variants_IonXpress_002_variant_strandbias_readcount.vcf.hg19_multianno.txt
TSVC_variants_IonXpress_003_variant_strandbias_readcount.vcf.hg19_multianno.txt
Your filenames end in _multianno.txt, not .multianno.txt, so use
mv /home/cmccabe/Desktop/NGS/annovar/*_multianno.txt /home/cmccabe/Desktop/NGS/API/2-12-2015/annovar/

Delimit the file name while moving to another directory in Shell

Im trying to move multiple files from one directory to another directory.
File name is with sequence and will be varying.
Example:
/global/userhome/usrsats/---------directory which has file names as below:
fl_cl_filename1
fl_cl_filename2
fl_cl_filename3
...
...
Now when moved to another directory, i need to get only the file name and delimit the fl_cl part.
Please help
Assuming you're using bash, I would do this with the remove the matching prefix pattern facility like this (with DEST_DIR set to the destination directory):
cd /global/userhome/usrsats
for f in *; do mv $f ${DEST_DIR}/${f#fl_cl_}; done

batch rename files and folders at once

I got help regarding the following question:
batch rename files with ids intact
It's a great example of how to rename specific files in a group, but I am wondering if there is a similar script I could use to do the following:
I have a group of nested folders and files within a root directory that contain [myprefix_foldername] and [myprefix_filename.ext]
I would like to rename all of the folders and files to [foldername] and [filename.ext]
Can I use a similar methodology to what is found in the post above?
Thanks!
jml
Yes, quite easily, with find.
find rootDir -name "myprefix_*"
This will give you a list of all files and folders in rootDir that start with myprefix_. From there, it's a short jump to a batch rename:
find rootDir -name "myprefix_*" | while read f
do
echo "Moving $f to ${f/myprefix_/}"
mv "$f" "${f/myprefix_/}"
done
EDIT: IFS added per http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
EDIT 2: IFS removed in favor of while read.
EDIT 3: As bos points out, you may need to change while read f to while read -d $'\n' f if your version of Bash still doesn't like it.

Resources