Rename specific file extension in directory with match to another file in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename specific file extension in directory with match to another file in bash
# 1  
Old 03-04-2016
Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name the date of the match exists and is where the match in name is located. I am not sure how to do this or if it is possible. I have tried a sed command with no luck, maybe that isn't the best way. Thank you Smilie.

contents of folder /home/cmccabe/Desktop/NGS/API/2-15-2016
Code:
IonXpress_001.bam
IonXpress_002.bam
IonXpress_003.bam
IonXpress_007.bam
file1.gz
file2.gz

name
Code:
2-15-2016
IonXpress_001.bam testname1_12345
IonXpress_002.bam testname2_45678
IonXpress_003.bam testname3_9012
IonXpress_007.bam testname1_12345-
2-19-2016
IonXpress_001.bam testname5_00000
IonXpress_002.bam testname6_11111
IonXpress_003.bam testname7_1213
IonXpress_007.bam testname8_78524

sed tried:
Code:
for f in /home/cmccabe/Desktop/NGS/API/2-12-2016/*.bam ; do
  bname=$(basename $f)
  cmd=$(sed -n "/$f/,/[0-9]{1,2}-[0-9]{1,2}-20[0-9]{2}/{s/\(.*\.bam\) \(.*\)/mv \1 \2/p}" /home/cmccabe/Desktop/NGS/panels/name.txt)
  echo "$cmd"
done
sed: -e expression #1, char 4: extra characters after command


Last edited by cmccabe; 03-04-2016 at 06:38 PM.. Reason: fixed format
# 2  
Old 03-04-2016
try:
Code:
sed -n "/$bname/,/[0-9]{1,2}-[0-9]{1,2}-20[0-9]{2}/{s/\(.*\.bam\) \(.*\)/mv \1 \2/p}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename portion of file using match to another

In the portion of bash below I am using rename to match the $id variable to $file and when a match (there will alwsys be one) is found then the $id is removed from each bam and bam.bai in $file and _test is added to thee file name before the extension. Each of the variables is set correctly but... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Rename text file with a specific pattern in directory

I am trying to rename all text files in a directory that match a pattern. The current command below seems to be using the directory path in the name and since it already exists, will not do the rename. I am not sure what I am missing? Thank you :). Files to rename in... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. Shell Programming and Scripting

How to rename the extension of a file?

Hello, I have multiple files named rscclog_2013-03-25.txt;3 in a directory, where 2013-03-25 is the previous day's date and the number after extension .txt preceded by a ';' is any number which i do not know beforehand. Now, i have to rename all such files as rscclog_2013-03-25.txt thus,... (2 Replies)
Discussion started by: rahulkt1987
2 Replies

7. Shell Programming and Scripting

Rename file extension.

I have a list file that contains names of many files. I am reading one file name at a time using for loop Then I like to create one more list file but with the file extension changed to "ctl". Note: The file name can have any number of dots ".". But the extension after the last dot should be... (4 Replies)
Discussion started by: pinnacle
4 Replies

8. Shell Programming and Scripting

Rename file to uppercase except extension

Hi, I am trying to make all file in the directory uppercase but not their extension (ex: image.jpg becoming IMAGE.jpg) here is code i am trying. $ ls | while read file do name=${file%%.*} newfilename=$(echo $name | tr 'a-z' 'A-Z') mv $file $newfilename done any suggestions of... (4 Replies)
Discussion started by: johninweb
4 Replies

9. Shell Programming and Scripting

rename a file with new extension

Hi guys, i had many files like filename.20110520_20110519_050030 i have to rename the file by removint the last numerics .. i.e filename.dat i tried with cut command and removed the numerics but i'm not able to add .dat to the files. is there any command insted of cut command to... (1 Reply)
Discussion started by: apple2685
1 Replies

10. Shell Programming and Scripting

rename file extension

I am trying for loop to rename file extension from .txt to .html : as below : for i in *.txt; do mv "$i" `basename $i`.html; done ------------------------------------------- But this renames a file file1.txt as file1.txt.html anyone know how get avoid .html added after .txt ? it... (4 Replies)
Discussion started by: sriram003
4 Replies
Login or Register to Ask a Question