Removing a pattern from file names - bash

I have some files named as below:
e2ed150l145l245St01-Oxs_MinDriver-Magnetization-00-0002105.omf
e2ed150l145l250St01-Oxs_MinDriver-Magnetization-00-0002167.omf
e2ed150l145l255St01-Oxs_MinDriver-Magnetization-00-0001519.omf
e2ed150l145l260St01-Oxs_MinDriver-Magnetization-00-0001841.omf
e2ed150l145l265St01-Oxs_MinDriver-Magnetization-00-0002730.omf
e2ed150l145l270St01-Oxs_MinDriver-Magnetization-00-0002788.omf
e2ed150l145l275St01-Oxs_MinDriver-Magnetization-00-0001616.omf
e2ed150l150l250St01-Oxs_MinDriver-Magnetization-00-0002386.omf
e2ed150l150l2100St01-Oxs_MinDriver-Magnetization-00-0015577.omf
I want to rename them to:
e2ed150l145l245St01.omf
e2ed150l145l250St01.omf
e2ed150l145l255St01.omf
e2ed150l145l260St01.omf
e2ed150l145l265St01.omf
e2ed150l145l270St01.omf
e2ed150l145l275St01.omf
e2ed150l150l250St01.omf
e2ed150l150l2100St01.omf
Following some of the similar forums I used several commands including:
rename 's/-Oxs_MinDriver-Magnetization-00-[0-9]*//g' e2e*.omf -v
But it did not work. Please advise.

You can also use mv as well:
for i in e2e*.omf; do
mv $i ${i%%-*}.omf
done
or
for i in e2e*.omf; do
mv $i ${i//-*/}.omf
done
To collapse to a single line:
for i in e2e*.omf; do mv $i ${i%%-*}.omf; done
If the destination already exists use mv -f

Related

changing file names according to parent directory name

Hello I have several directories of this tendency:
cluster1_bin.101_res/
cluster1_bin.102_res/
cluster1_bin.103_res/
cluster1_bin.104_res/
in each of these direcories I have several files with ".gbk" suffix like the following ones:
cluster1_bin.101_res/k141_734976.region001.gbk
cluster1_bin.101_res/k141_753945.region001.gbk
cluster1_bin.101_res/k141_743978.region001.gbk
and I need to replace everything that is before .region001.gbk for it's parent directory name
this is the desired output:
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
Is there a way I can do that?
Thanks for your time.
With a for loop and Parameter Expansion and mv to rename the files.
#!/usr/bin/env bash
for file in cluster1_bin.[0-9]*_res/*.gbk; do
path_name=${file%/*}
file_name=${file##*/}
region_with_extension=${file_name#*.}
echo mv -v "$file" "$path_name/$path_name.$region_with_extension" || exit
done
With your given input/data the output is:
mv -v cluster1_bin.101_res/k141_734976.region001.gbk cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
mv -v cluster1_bin.102_res/k141_753945.region001.gbk cluster1_bin.102_res/cluster1_bin.102_res.region001.gbk
mv -v cluster1_bin.103_res/k141_743978.region001.gbk cluster1_bin.103_res/cluster1_bin.103_res.region001.gbk
Remove the echo If you think the output is correct so mv can rename the files.

Batch renaming files in MAC OSX

I have several thousand files with name like this:
PIN_PMN_PT_010_02_00331_0004_018edf
and need to rename them all something like this:
PIN_PMN_PT_010_02_00331_0004_018.edf
I have used simple mv scripts like this:
for f in *; do echo mv "$f" "`echo $f | tr 'edf' '.edf'`"; done
For some reason it creates names like this:
PIN_PMN_PT_010_02_00331_0004_018.ed
They are missing the last f. I am running the script using echo to dry run. Any ideas please?
Using MACBook Pro running Mohave 10.14.6 and Bash.
Use shell Parameter Expansion!
Simply under any shell:
For the test, echo to show what's will go:
for file in *edf ;do echo mv "$file" "${file%edf}.edf" ;done
Then, for doing the job:
for file in *edf ;do mv "$file" "${file%edf}.edf" ;done
(This must work same on MacOs, than under Linux.)
... And to prevent renaming of already correctly named files:
for file in *edf ;do test -f "${file##*.edf}" && mv "$file" "${file%edf}.edf" ;done
Syntax ${file##*.edf} will replace any string, terminated by .edf, by an empty string. So test -f "" will fail.
... Still: I don't have any Mac for doing the test, but as this is POSIX Standard, this must work on any shell. (Let my know, please comment!)
More infos?
Have a look at man sh or man bash and search for Parameter Expansion
man -P"less +'/Parameter Expansion'" bash
Using sed, this should work
for f in *; do echo mv "$f" "`echo $f | sed 's/.\{3\}$//`.edf"; done
You are just removing the last 3 characters of a string, and adding your file extension.
Personally, I find the rename command invaluable for this sort of thing:
rename 's/edf$/.edf/' *edf
If you want to do a dry-run, you can do:
rename --dry-run 's/edf$/.edf/' *edf
Sample Output
'PIN_PMN_PT_010_02_00331_0004_018edf' would be renamed to 'PIN_PMN_PT_010_02_00331_0004_018.edf'
The benefits of using rename are:
it can do a dry-run to test before you run for real
it will create all necessary directories with the -p option
it will not clobber (overwrite) files without warning
you have the full power of Perl available to you and can make your renaming as sophisticated as you wish.
As helpfully suggested by F. Hauri in the comments, you may have some files that have already had the dot inserted before the extension in your directory. To protect against insertion of a second dot, you could either be more specific in the files you select for renaming and only rename those ending in a digit followed by edf:
rename 's/edf$/.edf/' *[0-9]edf
Or, as F.Hauri suggested:
rename 's/([^.])edf$/$1.edf/' *edf
Note that you can install on macOS with homebrew:
brew install rename

A simple mv command in a BASH script

The aim of my script:
look at all the files in a directory ($Home/Music/TEST) and its sub-directories (they are music files)
find out what music genre each file belongs to
if the genre is Heavy, then move the file to another directory ($Home/Music/Output)
This is what I have:
#!/bin/bash
cd Music/TEST
for files in *
do
if [ -f "$files" ];then
# use mminfo to get the track info
genre=`mminfo "$files"|grep genre|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
if [ $genre = Heavy ] ;then
mv "$files" "~/Music/Output/$files"
fi
fi
done
Please tell me how to write the mv command. Everything I have tried has failed. I get errors like this:
mv: cannot move ‘3rd Eye Landslide.mp3’ to ‘/Music/Output/3rd Eye Landslide.mp3’: No such file or directory
Please don't think I wrote that mminfo line - that's just copied from good old Google search. It's way beyond me.
Your second argument to mv appears to be "~/Music/Output/$files"
If the ~ is meant to signify your home directory, you should use $HOME instead, like:
mv "$files" "$HOME/Music/Output/$files"
~ does not expand to $HOME when quoted.
By the look of it the problem occurs when you move the file to its destination.Please check that /Music/Output/ exits from your current directory.Alternatively use the absolute path to make it safe. Also it's a good idea not use space in the file-name.Hope this will helps.:)
Put this command before mv command should fix your problem.
mkdir -p ~/Music/Output

Bash: Creating subdirectories reading from a file

I have a file that contains some keywords and I intend to create subdirectories into the same directory of the same keyword using a bash script. Here is the code I am using but it doesn't seem to be working.
I don't know where I have gone wrong. Help me out
for i in `cat file.txt`
do
# if [[ ! -e $path/$i ]]; then
echo "creating" $i "directory"
mkdir $path/$i
# fi
grep $i file >> $path/$i/output.txt
done
echo "created the files in "$path/$TEMP/output.txt
You've gone wrong here, and you've gone wrong here.
while read i
do
echo "Creating $i directory"
mkdir "$path/$i"
grep "$i" file >> "$path/$i"/output.txt
done < file.txt
echo "created the files in $path/$TEMP/output.txt"
78mkdir will refuse to create a directory, if parts of it do not exist.
e.g. if there is no /foo/bar directory, then mkdir /foo/bar/baz will fail.
you can relax this a bit by using the -p flag, which will create parent directories if necessary (in the example, it might create /foo and /foo/bar).
you should also use quotes, in case your paths contain blanks.
mkdir -p "${path}/${i}"
finally, make sure that you are actually allowed to create directories in $path

Bash shell: how to add a name

I am trying to rename some zip files in bash with an _orig but I seem to be missing something. Any suggestions??
My goal:
move files to an orig directory
rename original files with a "_orig" in the name
The code Ive tried to write:
mv -v $PICKUP/*.zip $ORIGINALS
for origfile in $(ls $ORIGINALS/*.zip);do
echo "Adding _orig to zip file"
echo
added=$(basename $origfile '_orig').zip
mv -v $ORIGINALS/$origfile.zip $ORIGINALS/$added.zip
done
Sorry still kinda new at this.
Using (p)rename :
cd <ZIP DIR>
mkdir -p orig
rename 's#(.*?)\.zip#orig/$1_orig.zip#' *.zip
rename is http://search.cpan.org/~pederst/rename/ (default on many distros)
Thanks to never use
for i in $(ls $ORIGINALS/*.zip);do
but use globs instead :
for i in $ORIGINALS/*.zip;do
See http://porkmail.org/era/unix/award.html#ls.
I know you've got a solution already, but just for posterity, this simplified version of your own shell script should also work for the case you seem to be describing:
mkdir -p "$ORIGINALS"
for file in "$PICKUP"/*.zip; do
mv -v "$file" "$ORIGINALS/${file%.zip}_orig.zip"
done
This makes use of "Parameter Expansion" in bash (you can look that up in bash's man page). The initial mkdir -p simply insures that the target directory exists. The quotes around $PICKUP and $ORIGINALS are intended to make it safe to include special characters like spaces and newlines in the directory names.
While prename is a powerful solution to many problems, it's certainly not the only hammer in the toolbox.

Resources