Sponsored Content
Top Forums Shell Programming and Scripting Bash to trim folder and files within a path that share a common file extension Post 303032422 by cmccabe on Sunday 17th of March 2019 09:01:01 AM
Old 03-17-2019
I am using ubuntu 14.04 as my os.
Each .bam and .bam.bai is inside each R_2019 directory but it looks like the files can not be found. Thank you Smilie.

set -xv
Code:
ROOTDIR=/home/cmccabe/rename
ROOTDIR=/home/cmccabe/rename
+ ROOTDIR=/home/cmccabe/rename
cmccabe@Satellite-M645:~$ cd $ROOTDIR || exit 1
cd $ROOTDIR || exit 1
+ cd /home/cmccabe/rename
cmccabe@Satellite-M645:~/rename$ # matching name "R_2019*" to operate on desired directory names, expand this to be precise.
# matching name "R_2019*" to operate on desired directory names, expand this to be precise.
cmccabe@Satellite-M645:~/rename$ MDIR="R_2019*"
MDIR="R_2019*"
+ MDIR='R_2019*'
cmccabe@Satellite-M645:~/rename$ find . -mindepth 1 -maxdepth 1 -type d -name "${MDIR}" | while read RDIR
find . -mindepth 1 -maxdepth 1 -type d -name "${MDIR}" | while read RDIR
> do
do
>   TRIMSTR="${RDIR%%-v5.6*}"
  TRIMSTR="${RDIR%%-v5.6*}"
>      for FLN in $RDIR/*.bam $RDIR/*.bam.bai
     for FLN in $RDIR/*.bam $RDIR/*.bam.bai
>      do
     do
> FLNSUB="_${RDIR/\.\//}"
FLNSUB="_${RDIR/\.\//}"
>     "mv ${FLN} ${FLN/$FLNSUB}"
    "mv ${FLN} ${FLN/$FLNSUB}"
>      done
     done
> # Now we shall rename the folder, after files inside have been renamed.
# Now we shall rename the folder, after files inside have been renamed.
>     "mv $RDIR ${TRIMSTR}"      
    "mv $RDIR ${TRIMSTR}"      
> done
done
+ read RDIR
+ find . -mindepth 1 -maxdepth 1 -type d -name 'R_2019*'
+ TRIMSTR=./R_2019_01_30_14_24_53_user_S5-0271-95
+ for FLN in '$RDIR/*.bam' '$RDIR/*.bam.bai'
+ FLNSUB=_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
+ 'mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx.bam'
bash: mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx.bam: No such file or directory
+ for FLN in '$RDIR/*.bam' '$RDIR/*.bam.bai'
+ FLNSUB=_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
+ 'mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx.bam'
bash: mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx.bam: No such file or directory
+ for FLN in '$RDIR/*.bam' '$RDIR/*.bam.bai'
+ FLNSUB=_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
+ 'mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx.bam.bai'
bash: mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx.bam.bai: No such file or directory
+ for FLN in '$RDIR/*.bam' '$RDIR/*.bam.bai'
+ FLNSUB=_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
+ 'mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx.bam.bai'
bash: mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx.bam.bai: No such file or directory
+ 'mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ./R_2019_01_30_14_24_53_user_S5-0271-95'
bash: mv ./R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions ./R_2019_01_30_14_24_53_user_S5-0271-95: No such file or directory
+ read RDIR

--- Post updated at 08:01 AM ---

@Don Cragun I just tried the scrip by @Peasent and posted the results. The files could not be found to trim. I am using ubuntu 14.04 currently and may be migrating to centos 7 in the near future.

I do have backups of the data and removed the echo as the output looked correct and since Ihave backups I performed the mv. As I look back it was not correct but the .bam files were trimmed as expected it was the .bam.bai that were not. Thank you Smilie.

Code:
find /home/cmccabe/rename/ \( -type d -o -name '*.bam*' \) -exec ls -ld {} +

drwxrwxr-x 3 cmccabe cmccabe 4096 Mar 17 07:28 /home/cmccabe/rename/
drwxrwxr-x 2 cmccabe cmccabe 4096 Mar 17 07:34 /home/cmccabe/rename/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions
-rw-rw-r-- 1 cmccabe cmccabe    0 Feb 28 14:59 /home/cmccabe/rename/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Feb 28 14:59 /home/cmccabe/rename/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_011_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai
-rw-rw-r-- 1 cmccabe cmccabe    0 Feb 28 14:59 /home/cmccabe/rename/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam
-rw-rw-r-- 1 cmccabe cmccabe    0 Feb 28 14:59 /home/cmccabe/rename/R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions/xxx_013_xx00_xxx_R_2019_01_30_14_24_53_user_S5-0271-95-v5.6_Oncomine_Childhood_Cancer_Research_DNA_and_Fusions.bam.bai


Last edited by cmccabe; 03-17-2019 at 10:04 AM.. Reason: added comments
 

10 More Discussions You Might Find Interesting

1. AIX

trim file name extension????

Dear Chaps, What will I do if, I am not sure about the length of the file name, but only one thing that I want to remove only the last extension. e.g. abcdXXXXXX.pqrXXXXX.asc (X is any character) I want to trim only .asc (or,watever) so that resultant file name would be like... (1 Reply)
Discussion started by: vishal_ranjan
1 Replies

2. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

3. Shell Programming and Scripting

command to list files with path & date in a folder

Hi, I need command to display files with full path and date of files where are generated at every 5hrs in a folder. eg: /u01/app/test/orjthsd_1_1 Sun May 10 19:03:26 2009 /u01/app/test/weoiusd_1_1 Sun May 10 21:00:26 2009 thanks saha (3 Replies)
Discussion started by: saha
3 Replies

4. Shell Programming and Scripting

move set of files to the target path with different extension

I have the following files in the dir /home/krishna/datatemp abc.xml cde.xml asfd.txt asdf_20120101-1.xml asdf_20120101-2.xml asdf_20120101-3.xml asdf_20120101-4.xml Now I need to move the files having the pattern asdf_20120101-*.xml to the dir /home/krishna/dataout with the extn as... (1 Reply)
Discussion started by: kmanivan82
1 Replies

5. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

6. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: cmccabe
1 Replies

7. Linux

How to print full path name along with file extension?

Hi I have a requirement like this: /abc/a/x.txt /abc/a/y.txt /abc/b/x.gz /abc/b/y.txt I need output like this: /abc/a:*.txt /abc/b:*.txt /abc/b:*.gz I have tried find /abc -type f -name "*.*" ||awk -F . '{print $NF}' it is print only extensions without path name. Please... (5 Replies)
Discussion started by: lijjumathew
5 Replies

8. Shell Programming and Scripting

Find common lines with one file and with all of the files in another folder

Hi! I would like to comm -12 with one file and with all of the files in another folder that has a 100 files or more (that file is not in that folder) to find common text lines. I would like to have each case that they have common lines to be written to a different output file and the names of the... (6 Replies)
Discussion started by: Eve
6 Replies

9. Shell Programming and Scripting

Find common lines between all of the files in one folder

Could it be possible to find common lines between all of the files in one folder? Just like comm -12 . So all of the files two at a time. I would like all of the outcomes to be written to a different files, and the file names could be simply numbers - 1 , 2 , 3 etc. All of the file names contain... (19 Replies)
Discussion started by: Eve
19 Replies

10. 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
All times are GMT -4. The time now is 08:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy