Bash to trim folder and files within a path that share a common file extension
The bash will trim the folder to trim folder. Within each of the folders (there may be more than 1) and the format is always the same, are several .bam and matching .bam.bai files (file structure) and the bashunder that executes and trims the .bam as expected but repeats the.bam.bai extentions after trimming those files. xxxx_0113_xxx_xxx.bam.bai.bam.bai also in the set -xv. I think the .bam extension common to both may be causing the repeat but am not sure. Removing the .bam.bai from the mv did not fix the repeats. The end goal is to trim the folders and the files within each of the folders and I am not sure if the nested loops are the best way (probably not). Thank you .
bash to trim folder
folder in /home/cmccabe/rename
trim folder
file structure in each /home/cmccabe/rename/<folder>
bash to trim files
desired trim files
set -xv w/ echo mv
set -xv w/o echo mv
See if this helps :
Remove the echo infront of mv commands to execute against files, otherwise it will just print on terminal.
Probably could use some more error handling and stuff.
Hope that helps
Regards
Peasant.
These 2 Users Gave Thanks to Peasant For This Post:
The bash will trim the folder to trim folder. Within each of the folders (there may be more than 1) and the format is always the same, are several .bam and matching .bam.bai files (file structure) and the bashunder that executes and trims the .bam as expected but repeats the.bam.bai extentions after trimming those files. xxxx_0113_xxx_xxx.bam.bai.bam.bai also in the set -xv. I think the .bam extension common to both may be causing the repeat but am not sure. Removing the .bam.bai from the mv did not fix the repeats. The end goal is to trim the folders and the files within each of the folders and I am not sure if the nested loops are the best way (probably not). Thank you .
bash to trim folder
folder in /home/cmccabe/rename
trim folder
file structure in each /home/cmccabe/rename/<folder>
bash to trim files
desired trim files
set -xv w/ echo mv
set -xv w/o echo mv
What you have shown us above makes it look like you may have moved a bunch of your *.bam.bai files into *.bam.bai.bam.bai files and possibly moved *.bam files into *.bam.bam files. The purpose of the echo commands was to make sure that the mv commands that would be executed looked good before actually moving files. The fact that the echoset -xv output did not show the same filenames as the mvset -xv output seems to imply that when the echo was removed from the echo mv lines in your script, something else in your script was changed than just removing the echo in front of the two mv commands.
Since the output from the set -xv trace showed that it was going to rename files in the wrong directory, why did you remove the echo and run it again? The purpose of having the echo in there is so that you can verify that the command being echoed is the command that you want the script to actually perform when run a second time with the echos removed.
Please show us the output from the command:
so we can see how things stand now. Please also tell us what operating system you're using. (PLEASE always tell us what operating system and shell you're using when you start a new thread.)
I don't know if you have tried running the code Peasant suggested in post #2 in this thread. I'm afraid the code Peasant suggested might only work with the file hierarchy you described before any files were moved. (Note that I haven't tried to figure out what his code will do if it starts with your modified file hierarchy instead of what may be the current file hierarchy.) Do you have backups so you can restore that original state? If not, I'm hoping that with the output from the find command above we'll be able to find a way to get to where you want to be without losing any data.
This User Gave Thanks to Don Cragun For This Post:
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 .
set -xv --- 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 .
Last edited by cmccabe; 03-17-2019 at 10:04 AM..
Reason: added comments
OK. We're dealing with empty .bam and .bam.bai files, so we won't be destroying your data no matter what we do here.
When I recreated your file hierarchy on my system, change the first line of Peasant's code from:
to:
and ran his code the results looked promising. So, I change the lines:
in his code to:
and reran the script. Afterwards, I ran an ls on the resulting file hierarchy and got:
which looks exactly like what I thought you were trying to produce.
I guess Peasant and I both thought that you knew how to rearrange the quotes on his echo commands to get working mv commands.
If you try his code again with the mv commands changed to match what I have suggested above, do you get what you want?
This User Gave Thanks to Don Cragun For This Post:
Hi Peasant,
Here is a slightly modified version of your script that just uses options and variable expansions defined by the POSIX standards. But, of course, it still depends on us knowing the pathname of a shell that provides those standard variable expansions. (Note that find isn't needed for this; we can get what we need just using shell pathname expansions.)
Hi cmccabe,
If Peasant's script (modified as suggested in post #5 worked for you, the script above should also work and should even run a little bit faster since it doesn't need to invoke find to get the job done.
I hope this helps,
Don
These 2 Users Gave Thanks to Don Cragun For This Post:
Thank you both .... both scripts work great. I guess I don't understand "" and have noticed it makes a difference in the output but have to read more about it. I always thought it was for escaping a whitespace in a filename or variable. Is that not true? Thanks again .
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)