Delete files using shell script with some additional parameters ( windows ) - image

I have a dataset contains ( .csv files and images ) but I have some images are not annotated, that means the number of images < number of .csv files.
I have placed all images to the folder that contains the .csv files.
I would like to know if is it possible to delete additional images ( images without annotations, I mean images without a correspondent .csv files ) with a shell script or command line?

I solved my problem by this command:
for f in .jpg; do [[ -f ${f%.}.csv ]] || echo "$f"; done
The Source of the solution:
linux- Script to compare and delete the files

Related

Dynamically execute functions in different folders using bash

I have two folders: control and patients both with several folders inside, belonging to one individual each.
I want to do two things:
Create inside the folder for each individual a new folder called cortical_maks and inside that one, three more, called accumebens, putamen, caudate
Inside each individual folder, there are images in img format I want to convert to nii.gz using the funtion fslchfiletype.
This is what I have so far:
DIR="/media/Roy"; cd "$DIR/Analysis"
for group in Controls Patients; do
for case in $group/*; do
[ -d $case ] || continue #if its not a folder
mkdir $DIR/Analysis/$case/Cortical_masks && cd $_
mkdir accumbens putamen caudate
for file in $DIR/Analysis/$case/ROIS2/rs_roi/*.img; do
fslchfiletype NIFTI_GZ "$file"
done;
done;
done;
There are two problems with this code.
The second time you run it, the folder cortical masks is created in the main folders, that is controls and patients, that is, outside the folder it´s supposed to work.
Also, it just converts img to niig.gz for one folder at a time. First time you execute the script, converts imgs for the folder belonging to the first individual, etc
Question 01 :
The second time you run it, the folder cortical masks is created in the main folders, that is controls and patients. I want it in a way that doesn´t create new folders if there is already one with the same name.
I suggest you to use an if loop to verify if the folder cortical_mask exist before to run your code :
#check if the folder exist, if yes = true so we add ! caracter to have the opposite
if [ ! -d "$DIR/Analysis/$case/Cortical_masks" ]
then
#Your code
fi
Question 02 :
Also, it just converts img to niig.gz for one folder at a time. First time you execute the script, converts imgs for the folder belonging to the first individual, etc
If you want to do two actions in the same time, why do you not create two bash scripts and execute them simulteanously ? Or, you can automate them by creating a process in your OS which will execute scripts for you.

cat multiple files in multiple folders via a loop

I have a set of files which have fastq.gz inputs (R3003_C0UH3ACXX_ATTACTCG_L005_R1_001.fastq.gz ) In these files the RXXXX numbers are not sequential. However I got all the folder names whch begin with R in a txt file , one in each line. Each folder has different number of *_R1 and *_R2 files. I would like to merge them and I have used cat *_R1.fastq.gz and *_R2.fastq.gz indiviually to do this .But I have 500 samples and it is impossible to run them all like this by going into each individual folder and doing cat. How do I get to open each folder in the txt file, run cat to get the final RXXX.R1.fastq.gz and RXXXX.R2.fastq.gz for each folder with a loop?
My file name is R3.txt which lists the RXXX files and I tried
for i in cat R3 ; do cat $i*R1*.fastq.gz >$i\.R1.fastq.gz; done
But it doesn't do what I hoping

How to batch "touch" files in subfolder with data from parent folder

I have a folder named "Video" on my Desktop and inside that folder are a bunch of .MTS video files. (00000.MTS, 00001.MTS, 00002.MTS etc...)
There is also a subfolder "H.265" (Video > H.265) which contains files (00000.mp4, 00001.mp4, 00002.mp4 etc...) which were made by converting the files from the parent folder. The problem is they don't contain the correct date created metadata.
I want to use the command
touch -r 00000.MTS 00000.mp4
touch -r 00001.MTS 00001.mp4
...
touch -r 000mn.MTS 000mn.mp4
to copy the date from the MTS to the mp4 file.
Is there a way I could write a for loop or something to do this for every single file?
I am using MacOS Mojave.
Something like this
cd ~/Desktop/Video
for v in *.mp4
do
vn=`basename $v .mp4`
touch -r "H.265/$vn.MTS" $v
done

How to rename photos using information from csv file

I am new to unix and could really use your help.
I want to rename a lot of photographs so they correspond with codes of items that are on the picture. I have a .csv file that has the original .jpg name and then the codes I want the photos to be renamed to, following in consecutive columns. For example:
IMG_1234.JPG,AB001,AB003,AB004
IMG_1345.JPG,AB011,AB012,AB013,AB014,AB015
IMG_1456.JPG,AB112
IMG_1678.JPG,AB125,AB126
So I want IMG_1234.JPG copied 3 times and renamed to AB001, AB003, and AB004 etc.
I know I need a script and that I can copy and rename files, but I can't figure out how to make a script run through the csv file and copy & rename the .jpg to the names following until an empty cell and then move on to the next row and copy & rename that .jpg etc etc.
I hope my question is clear and I apologize for my limited knowledge.
Thanks in advance!
edit: The image names have directories (with spaces) in front of them as the photographs are in different folders. For example:
./Photos sorted/Samples1-100/IMG_1134.JPG
This should do what you want. The filename of the csv file is given as parameter to the script. You might adjust the paths inside the copy command, currently everything must be in the same directory. If you are using this on a mac or linux, you can also use "ln -s" instead if "cp" to create a symbolic link to the original file to save disk space.
CSVFILE=$1
cat $CSVFILE |\
while read LINE; do
SPLIT=`echo $LINE | tr "," " "`
FIRST=0
for NAME in $SPLIT; do
if [ $FIRST -eq 0 ]; then
SRCNAME=$NAME
else
DSTNAME=$NAME
cp ${SRCNAME}.jpg ${DSTNAME}.jpg
fi
((FIRST++))
done
done

shell script to create libre office Impress with images

I've some image files generated by an analysis. Every time I do analysis, the file names are same. And I've to create a presentation, and I'm using Libre Office Impress.
Let us say, I've three images image1.png, image2.png, and image3.png, and I should put these images say in page no 3, page no 5, and page no 8
Now, I'm inserting images manually. I know very basic shell scripting. So I was wondering what would be the bash shell script to automatically create an libre office impress file with images automatically inserted at pages mentioned as above.
If the main part of the .odp file doesn't change, it is possible to make a template in flat openDocument format, where the names of the 3 images are updated by script.
A flat document is a uncompressed xml document that can be open with a text editor to change some part manually with caution. Usually the name of such a document is rather with the .fodp You must save this template in flat format, with links to the images rather than incorporate them.
So.
Let us say :
the template is at /path/to/the/template.fodp beside the images im1.png, img2.png, img3.png of the example.
the name of the first image is image1.png, the second image2.png, and the third : image3.png.
the images to be imported are in the same directory /path/to/the/document where will be the final document.
Let us write a script insertImages.sh
myTpl="$1" # will contains '/path/to/the/template.fodp'
myDir="$2" # will contains '/path/to/the/document'
img1="$3" # will contains the name of the first image in myDir
img2="$4" # will contains the name of the second image in myDir
img3="$5" # will contains the name of the third image in myDir
[[ -f "$1" ]] && cp "$1" "$2/document.fodp" || exit 1 # checks if the template exists and copy it
[[ -f "$3" ]] && sed "$2/document.fodp" "s/img1.png/$3/" || exit 1 # overwrite the name of the first image
[[ -f "$4" ]] && sed "$2/document.fodp" "s/img2.png/$4/" || exit 1 # overwrite the name of the second image
[[ -f "$5" ]] && sed "$2/document.fodp" "s/img3.png/$5/" || exit 1 # overwrite the name of the third image
This script should be invoked this way :
insertImages.sh "/path/to/the/template.fodp" "/path/to/the/document" "image1.png" "image2.png" "image3.png"
I'm not a big programmer. So there is probably some mistakes in those lines. But the principle is there.
They are some constaints :
a flat document
relative pathes to the images.

Resources