moving files with specific extension to directory - bash

I am trying to move all files with a .multianno.txt from one directory to another. I thought the command below would work but maybe the * is causing problems. Thank you :).
mv /home/cmccabe/Desktop/NGS/annovar/*.multianno.txt /home/cmccabe/Desktop/NGS/API/2-12-2015/annovar/
mv: cannot stat ‘/home/cmccabe/Desktop/NGS/annovar/*.multianno.txt’: No such file or directory
directory (/home/cmccabe/Desktop/NGS/annovar)
TSVC_variants_IonXpress_001_variant_strandbias_readcount.vcf.hg19_multianno.txt
TSVC_variants_IonXpress_002_variant_strandbias_readcount.vcf.hg19_multianno.txt
TSVC_variants_IonXpress_003_variant_strandbias_readcount.vcf.hg19_multianno.txt

Your filenames end in _multianno.txt, not .multianno.txt, so use
mv /home/cmccabe/Desktop/NGS/annovar/*_multianno.txt /home/cmccabe/Desktop/NGS/API/2-12-2015/annovar/

Related

How can I delete all files in all subdirectories with a certain name?

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.
This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.
I will need to remove this file from 260 subdirectories under the L001 directory. My directory structure is like this:
L001
C5.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
C6.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg). So maybe there is a way to provide a list of the file names I need to delete.
How can I delete these files?
find . -name "s_1_1102_c.jpg" -exec rm -f {} \;
Note: This will find and delete the file in any subdirectory of the current one. So you could execute it in L001 or wherever else you want to do this.
for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
echo rm L001/*/"$i";
done
If output looks fine, remove echo.
The final method I used to delete my files was given by #Peter - Reinstate Monica
for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done
I was concerned that my file list would be too long but it worked in this situation.

Bash, move and rename files to base parent directory name

I have a bunch of base-parent-folders with different names.
In each of these base-parent-folders i have a folder with same same name(result).
In each of the result folders, i have a result file (data.txt), also named the same in each base-parent-folder.
I need to move all the data.txt files to a new folder (newfolder), and rename them to the base-parent-folder name.
for name in ./*/*/data.txt; do
mv "$name" "../newfolder/$(basename -- "$(dirname -- "$name")").txt";
done
This will move the file but rename the data.txt files to result.txt, and not the unique base-parent-folder name.
Help is much appreciated :D
What i have:
data\data1\result\data.txt
data\data2\result\data.txt
data\data3\result\data.txt
data\data4\result\data.txt
data\data5\result\data.txt
What i want:
data\newfolder\data1.txt
data\newfolder\data2.txt
data\newfolder\data3.txt
data\newfolder\data4.txt
data\newfolder\data5.txt
for f in */*/data.txt;
do mv "$f" "./newfolder/${f%/*/*}.txt";
done
Worked for me. The ./ part did not work. It wanted to add a additional folder called . when moving the file.
Thanks for the help

How to move and rename each file in folder to top folder? [linux shell] [bash]

I'm trying to create a simple script in linux shell that in a folderX goes through each of its folders, renames the files that finds there, and moves them to the root (folderX) folder....
I'm guessing that the logic is somewhat like this:
-0> //in folderX
-0>for each folder == $folderY //just the last part, what comes after ~/folderX/[this]
-1> cd ./$folderY
-1> for each fileInY == $fileInY //just the last part, what comes after ~/folderX/folderY/[this.something]
-2> rename fileInY = "$folderY - $fileInY"
-1> move * to folderX
-1> cd..
(but suggestions and different methods of approach are appreciated)
Thanks in advance!
-Gabix
folder="folderX"
find "$folder" -type f -exec cp '{}' '{}'.bak \; -exec mv '{}'.bak "$folder" \;
Set the root folder/directory in a variable folder and then use this to run find and then exec. Find files in the directory structure and first copy the name of the file to the name of the file followed by ".bak" and then move the file from the directory to the root folder/directory

rsync with folder and file name pattern matching to copy files

Right now I'm successfully running:
rsync -uvma --include="*/" --include="*.css" --exclude="*" $spec_dir $css_spec_dir
In a shell script which copies all of the files in the source directory, that are .css files, into a target directory.
I want to do the same for HTML files, but only where they are in a subfolder with the name 'template'.
So I'm in directory ~/foo, and I want to rsync where the --include="*/" only matches on subfolders with the name 'template'. So ~/foo/bar/template/baz/somefile.html would match, and so would ~foo/bar/baz/qux/template/someotherfile.html, but NOT ~/foo/bar/thirdfile.html
Although it looks a little bit strange, this works for me:
rsync -uvma --include="*/" --include="*/template/*/*.html" --include="*/template/*.html" --include="template/*.html" --include="template/*/*.html" --exclude="*" $spec_dir $html_spec_dir
This one works for me:
rsync -umva --include="**/templates/**/*.html" --exclude="*.html" source/ target
Were you looking for **? Here you have to be careful about choosing your exclude pattern, * won't work as it matches directories on the way. If rsync finds foo/templates/some.html, it will first copy foo, then foo/templates and then foo/templates/some.html, but before it gets there * already matched foo and nothing gets copied.
Here's what worked:
rsync -uvma --include="*/" --include="templates/**.html" --exclude="*" $html_all_dir $html_dir
My guess is, your format and mine probably accomplish the same thing. I know I tried about 20 different patterns before this one, and this is the only one that worked properly. I don't think I tried your format though :)

Transferring files between folders

I need a solution for this script to move files that will drop into the 8953-x folders to a joint folder.
The files that drops to the 8953-x folders will automatically be moved to the joint folder /mnt/FOLDER.
It will move all files besides files containing ! in their filename, for example picture.jpg.!sync.
Files ending with .!sync are in sync between servers, using btsync and not complete, they shall be ignored. When the sync end, the file output will change to picture.jpg, and then I want it to be transferred to the joint folder.
#!/bin/bash
from_folders=(8953-10 8953-11 8953-12 8953-3 8953-4 8953-5 8953-6 8953-7 8953-8 8953-9)
${from_folders[#]}
I believe this should work, but please test on a backup directory.
shopt -s extglob; mv 8953-*/!(\!*) /mnt/FOLDER
This turns on pattern matching and then moves all files in those folders which do not start with exclamation to the destination folder.
If the files all are named .jpg when ready, it is easier:
mv 8953-*/*.jpg /mnt/FOLDER
This is an improved script:
#!/bin/bash
shopt -s extglob
from_folders=(8953-10 8953-11 8953-12 8953-3 8953-4 8953-5 8953-6 8953-7 8953-8 8953-9)
for folder in ${from_folders[#]}
do
echo mv $folder/!(\!*) /mnt/finished_fotograf
done

Resources