Simple Bash Script: Change names of files to mimic directories - bash

I have 312 directories labeled,
Ion_0001- Ion_0312.
In each directory I have a file light.out. I'd like to change the file names in each directory to, for example:
Ion_0001.out
I believe I also need to substitute the / so that my output DOESNT look this this:
Ion_0001/.out
Can any one help me out with a simple script??
This is what I've tried:
#!/bin/bash
for dir in */
do
cd $dir
for filename in *.out; do
mv $filename ${filename//$dir.out}
done
cd ..
done
Thanks!

Not a free coding service, but it's simple enough to not make it worth arguing about...
Assuming this file structure:
Ion_0001/
Ion_0001/light.out
Ion_0002/
Ion_0002/light.out
...
Run this code in a script or just at the command line:
for i in Ion_0*
do
mv "${i}/light.out" "${i}/${i}.out"
done
Resulting in this structure:
Ion_0001/
Ion_0001/Ion_0001.out
Ion_0002/
Ion_0002/Ion_0002.out
...
Is that what you were looking for?

for dir in Ion*/; do
mv "${dir}light.out" "${dir}${dir%/}.out"
done
The trailing slash in the Ion*/ pattern limits the results to directories only, but the slash will be present in the variable's value.

Related

Entering a directory whose name matches a substring

I want to enter several directories in a for loop. I do not have the complete name of the directories, only a part of it.
I would like to do something like what you would write on the terminal, something like cd *IncompleteDirName*
This is a MVE of the loop: IncompleteDirName's are obtained from the file IncompleteDirNames.
cont=1
sum=1
while read anotherFILE; do
IncompleteDirName=$(sed "${cont}q;d" IncompleteDirNames)
cd *"${IncompleteDirName}"*
# Do stuff
cd ..
cont=$((cont + sum))
done <anotherFILE
This is not working, I don't know if this has to do with wildcard not expanding or with variable not working properly.
It is throwing me this error message:
*: No such file or directory
I suppose this means asterisk is not working as intended. It is not entering the directories, and there is a directory that matches every pattern. Anyway, no directory is being entered.
This is how IncompleteDirNames file looks like:
Alicante
Almeria
Andalucia
Avila
It is a column of names.
These are the directory names corresponding to the IncompleteDirNames above:
aa4fas_Alicante_com
mun_Almeria
comunidadde_Andalucia
ciuAvila
Try this code -
cont=1
sum=1
while read FILE; do
IncompleteDirName=$(sed "${cont}q;d" FILE)
cd *"${IncompleteDirName}"*
# Do stuff
cd ..
cont=$((cont + sum))
done <IncompleteDirNames
You can do the following:
#!/bin/bash
while read -r line; do
cd /absolute/path/to/*$line*
# do stuff
done < filename
This will enter each directory whose name partially matches a line in filename, and "does stuff".

Delimit the file name while moving to another directory in Shell

Im trying to move multiple files from one directory to another directory.
File name is with sequence and will be varying.
Example:
/global/userhome/usrsats/---------directory which has file names as below:
fl_cl_filename1
fl_cl_filename2
fl_cl_filename3
...
...
Now when moved to another directory, i need to get only the file name and delimit the fl_cl part.
Please help
Assuming you're using bash, I would do this with the remove the matching prefix pattern facility like this (with DEST_DIR set to the destination directory):
cd /global/userhome/usrsats
for f in *; do mv $f ${DEST_DIR}/${f#fl_cl_}; done

Append part of folder name to all .gz within

I have a folder of data folders with the following structure:
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data2.gz
sampleName2-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz
I want to modify all the data.gz within each sample folder by appending the sample name but not the random numbers to get:
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName1_data1.gz
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName1_data2.gz
sampleName2-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName2_data1.gz
It seems like this should be a simple mv for loop but I haven't been able to figure out how to pull part of a folder name using basename.
for i in */Data/Intensities/BaseCalls/*.gz; do mv $i "fastq""/"${i%%-*}"."`basename $i`; done
I couldn't figure out how to make the files stay in their original folder but for my purposes it works to have all the files go to a new folder ("fastq")
I suppose the "sampleName" part doesn't include dashes. In that case, use the standard pattern removal expansion: %%. That is, suppose your full path (relative to directory root) is stored in $path, just do ${path%%-*} to extract the "sampleName" part. Search for %% in the Bash Reference Manual for more details. As a simple example:
> path=sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz
> echo ${path%%-*}
sampleName1
Otherwise, you could also use more advanced substring extraction based on regex. See BashFAQ/100 or Manipulating Strings from the TLDP Advanced Bash Scripting Guide.
Update. Here's the full command to perform the job described, and it is entirely native to the shell:
for file in */Data/Intensities/BaseCalls/*.gz; do
mv "$file" "${file%/*}/${file%%-*}_${file##*/}"
done

Is there option in cp command to reflect source path as destination path?

If source and destination path are being same, how to skip re-typing it?
For example, I need to take a back-up of a file or rename it:
# taking back-up
$ cp ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img
# renaming it
$ mv ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img
It's all about the curly braces!
$ cp ~/project/uboot/{,ver1-}u-boot.img
or to be more verbose
$ mv ~/project/uboot/{u-boot.img,ver1-u-boot.img}
The shell will reproduce what you have explicitly written in your question, which means you can write out the full path once. Here's a good link for further reading.
How about
( cd ~/project/u-name-it; cp_or_mv file ver1-file )
or
base=base-path cp_or_mv $base/file $base/ver1-file
However this very much looks like a use case for a script or function to me.
As these can become quite sophisticated of course (e.g. increasing the version would be nice) I prefer to give a simple example more as an incentive ;)
# Untested, test well before using; should work in zsh & bash IFIAC
# usage:
# bup directory [files...]
function bup
{
local dir=$1
shift
for file; do
cp $dir/$file $dir/ver1-$file
done
}
HTH
Robert

looping files with bash

I'm not very good in shell scripting and would like to ask you some question about looping of files big dataset: in my example I have alot of files with the common .pdb extension in the work dir. I need to loop all of them and i) to print name (w.o pdb extension) of each looped file and make some operation after this. E.g I need to make new dir for EACH file outside of the workdir with the name of each file and copy this file to that dir. Below you can see example of my code which are not worked- it's didn't show me the name of the file and didn't create folder for each of them. Please correct it and show me where I was wrong
#!/bin/bash
# set the work dir
receptors=./Receptors
for pdb in $receptors
do
filename=$(basename "$pdb")
echo "Processing of $filename file"
cd ..
mkdir ./docking_$filename
done
Many thanks for help,
Gleb
If all your files are contained within the .Repectors folder, you can loop each of them like so:
#!/bin/bash
for pdb in ./Receptors/*.pdb ; do
filename=$(basename "$pdb")
filenamenoextention=${filename/.pdb/}
mkdir "../docking_${filenamenoextention}"
done
Btw:
filenamenoextention=${filename/.pdb/}
Does a search replace in the variable $pdb. The syntax is ${myvariable/FOO/BAR}, and replaces all "FOO" substrings in $myvariable with "BAR". In your case it replaces ".pdb" with nothing, effectively removing it.
Alternatively, and safer (in case $filename contains multiple ".pdb"-substrings) is to remove the last four characters, like so: filenamenoextention=${filename:0:-4}
The syntax here is ${myvariable:s:e} where s and e correspond to numbers for the start and end index (not inclusive). It also let's you use negative numbers, which are offsets from the end. In other words: ${filename:0:-4} says: extract the substring from $filename starting from index 0, until you reach fourth-to-the-last character.
A few problems you have had with your script:
for pdb in ./Receptors loops only "./Receptors", and not each of the files within the folder.
When you change to parent directory (cd ..), you do so for the current shell session. This means that you keep going to the parent directory each time. Instead, you can specify the parent directory in the mkdir call. E.g mkdir ../thedir
You're looping over a one-item list, I think what you wanted to get is the list of the content of ./Receptors:
...
for pdb in $receptors/*
...
to list only file with .pdb extension use $receptors/*.pdb
So instead of just giving the path in for loop, give this:
for pdb in $receptors/*.pdb
To remove the extension :
set the variable ext to the extension you want to remove and using shell expansion operator "%" remove the extension from your filename eg:
ext=.pdb
filename=${filename%${ext}}
You can create the new directory without changing your current directory:
So to create a directory outside your current directory use the following command
mkdir ../docking_$filename
And to copy the file in the new directory use cp command
After correction
Your script should look like:
receptors=./Receptors
ext=.pdb
for pdb in $receptors/*.pdb
do
filename=$(basename "$pdb")
filename=${filename%${ext}}
echo "Processing of $filename file"
mkdir ../docking_$filename
cp $pdb ../docking_$filename
done

Resources