Processing of the file names - bash

I have a folder with a files inside with a long names like:
08_29_2017.AT1_dry_apo.3rep.step7_1.xtc
each file begins with a current data in a format like
dd_mm_yyyy.
My bash script loops all the files and defines only the relevant part of the file name in a new variable:
for traj in ${all_xtc}/*.xtc; do
traj_name3=$(basename "$traj")
traj_name="${traj_name3/.xtc/}" # here I remove xtc from the name
# here I need to add something to remove the date from the begining of the file
what should be passed here to remove a date from the beginning of a file name?
Thank you!

You can do what you want in two parameter expansions:
for traj in "$all_xtc"/*.xtc; do
traj=${traj##*/[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]}
traj=${traj%.xtc}
echo "$traj"
done
The first one removes everything up to the last slash (equivalent to basename), followed by the date. The second one removes the .xtc from the end.
The first expansion isn't at all strict about the date, since it would also match 98_76_0000, but it's probably good enough in this case. You can add a . to the end if you want to remove that as well.

Related

bash: assign variable on the fly according to the pattern in file name

My bash script operates with different files located within the same directory and then execute some program for them. Each files match the following pattern where the ${receptor} keyword is always located after the second "_" of the file name. This keyword defines the group of the file.
something_something_${receptor}_repX.pdb
According to the ${receptor}, all files are devided in three groups. So I create a special function which set the name of the prefix and execute all operations in flow only for the files contained this index:
set_receptor () {
#receptor='5r82dim'
#receptor='2zu2dim'
receptor='7dr8dim'
}
set_receptor
for pdb in "${docking_pdb}"/*_${receptor}_*.${lig_ext}; do
pdb_name=$(basename "$pdb" .${lig_ext})
pdb_name="${pdb_name/_rep1}"
echo "Converting ${output}: the system ${pdb_name}
done
This means that each time when I need to switch between different groups I need to uncomment $receptor manually in the set_receptor function. How could I determine $receptor automatically in the workflow in order that my script could be executed only 1 time for any number of groups and determine each group automatically in the begining of FOR loop execution?
If the keyword is always after the second underscore, you can use parameter expansion to extract it:
#!/bin/bash
for pdb in "$docking_pdb"/*_*_*."$lig_ext" ; do
receptor=${pdb##*/} # Remove the path.
receptor=${receptor#*_} # Up to the first underscore.
receptor=${receptor#*_} # Up to the second underscore.
receptor=${receptor%%_*} # Remove everything after the 1st underscore.
...
done

bash script to rearrange multiple file names correctly(go pro)

I have multiple GoPro files in a directory that all need to be renamed and would like a bash script to do it. Basically I want to move the 3rd and 4th characters back to the 12th and 13th spot with dashes around it. The only other GoPro post I found puts it at the end of the file but I really need it in the middle like that. Thank you
Example
Original filename is:
GX010112_1647792633961.MP4
and I need it to look like:
GX0112_164-01-7792633961.MP4
A possible answer with the rename command-line. It allows you to rename multiple files according to PERL regex.
#installation of rename
apt install rename
#rename your files
rename 's/^(.{2})(.{2})(.{8})(.*)/$1$3-$2-$4/' *
REGEX explanation:
The regex is structured as s/ MATCHING PATTERN / REPLACEMENT /
^: set position at the beginning of string.
(.{2}) : Match the first 2 characters and store it in $1.
(.{2}) : Match the following 2 characters (the 3th and 4th) and store it in $2.
(.{8}) : Match the following 8 characters (from the 5th to 12th included) and store it in $3.
(.*) : Match the rest of you string and store it in $4.
Hope it helps.
After many hours I figured it out based on this thread:
Rename several gopro files
for file in GX*; do
file1="${file#*??}"
file2=${file1#*??}
file3=${file1#*??????}
file4=${file1%*$file2}
file5=${file2%*?????????????????}
mv -v "$file" "${file5}${file4}${file3}"
done
I hope it comes in handy for any other GoProers out there!

extract first part of of file name up to a pattern in bash

This seems to be a common sort of problem but I haven't seen an answer that works in bash directly. What I've got so far, using the case where I want to grab the file name up to the first blank or period, where $f is the filename (e.g. f="filename-fileversion filetype.file-subtype"), is
echo ${f#*[. ]}
which does what I want but leaves me with the remainder of the string (filetype.file-subtype) when what I want is the excluded part (filename-fileversion) and
echo ${f%[. ]*}
which gives me the first part of the string but matches to the last occurrence of the pattern (filename-fileversion filetype). Variations moving the asterix before/after the pattern end up returning the entire filename.
Is there a way to do what I want without resorting to an external program?

Replace literal at HTML using Unix shell script

I have a series of scripts declared in an HTML with the following format:
xxx.jfhdskfjhdskjfhdskjfjioe3874.bundle.js
The part between the periods is a dynamic hash, but it will always be an alphanumeric with the same positions. My problem is that I need to dynamically modify that hash, with the new generated files, which are in the same directory as the HTML itself. Is there a clean way to do it in Unix with a script?
You must be more specific: You want to generate new hashes for all scripts in this directory or need just a tool to change one by one basis? where do you get new hashes from? Below I attached simple script to change the part between first and second period sign. Script should be called with old name as first argument and new hash as second argument. It could be compressed to just one line but I used variables for clarity.
#! /bin/sh
OLDNAME=$1
NEWHASH=$2
NEWNAME=$(printf "%s" "$OLDNAME" | sed "s/^\([^\.]*\)\.[^\.]*\.\(.*\)/\1\.$NEWHASH\.\2/")
echo $NEWNAME

Rename files, moving date part of name from beginning to end

I have filenames that come with the date at the beginning of their names, e.g. 20171015….txt.
I currently use a batch file to strip off the first 8 characters and this has worked well. However I'd like to now keep the date but move it to the end of the file name.
With the file name without extension set as a variable, %Variable%:
Ren "%Variable%.ext" "%Variable:~8%%Variable:~,8%.ext"
Or if performed within some sort of loop and delayed expansion enabled, i.e.SetLocal EnableDelayedExpansion:
Ren "!Variable!.ext" "!Variable:~8!!Variable:~,8!.ext"

Resources