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.
Related
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
--Bash 4.1.17 (running with Cygwin)
Hello, I am trying to pass the date into the --suffix option on the move (mv) command. I am able to pass in a simple string (like my name) but unable to pass in the date. If you run the script below you will see that the mv command with the suffix="$var" works but suffix="$now" does not.
#!/bin/bash
dir="your directory goes here"
now="$(date "+%m/%d/%y")"
var="_CARL!!!"
echo "$now"
echo "$var"
cd "$dir"
touch test.txt
# error if already exists
mkdir ./stack_question
touch ./stack_question/test.txt
mv -b --suffix="$var" test.txt ./stack_question/
The idea is that if test.txt already exists when trying to move the file, the file will have a suffix appended to it. So if you run this script with:
--suffix="$var"
you will see that the stack_question directory contains two files:
test.txt & test.txt_CARL!!!
But, if you run this script with:
--suffix="$now"
you will see that in the stack_question directory only contains:
test.txt
Any help on this would be greatly appreciated!
It is because you have embedded / in your date format try
now="$(date +%m_%d_%y)"
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
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
#!/bin/bash
for i in /home/xxx/sge_jobs_output/split_rCEU_results/*.rCEU.bed
do
intersectBed -a /home/xxx/sge_jobs_output/split_rCEU_results/$i.rCEU.bed -b /home/xxx/sge_jobs_output/split_NA12878_results/$i.NA12878.bed -f 0.90 -r > $i.overlap_90.bed
done
However I got the errors like:
Error: can't determine file type of '/home/xug/sge_jobs_output/split_NA12878_results//home/xug/sge_jobs_output/split_rCEU_results/chr4.rCEU.bed.NA12878.bed': No such file or directory
Seems the computer mixes the two .bed files together, and I don't know why.
thx
Your i has the format /home/xxx/sge_jobs_output/split_rCEU_results/whatever.rCEU.bed, and you insert it to the file name, which leads to the duplication. It's probably simplest to switch to the directory and use basename, like this:
pushd /home/xxx/sge_jobs_output/split_rCEU_results
for i in *.rCEU.bed
do
intersectBed -a $i -b ../../sge_jobs_output/split_NA12878_results/`basename $i .rCEU.bed`.NA12878.bed -f 0.90 -r > `basename $i .NA12878.bed`.overlap_90.bed
done
popd
Notice the use of basename, with which you can replace the extension of a file: If you have a file called filename.foo.bar, basename filename.foo.bar .foo.bar returns just filename.