How to loop a function in a sub-directory - linux

I cant figure out how to loop a function to combine Fastq files in a sub-directory
The directory is organized as
TJU1/Files/Actual files
TJU2/Files/Actual files
...
I can run this code on the sub-directories individually but I cant figure out how to loop it for the "files" sub-directory in each folder
cat *R1* > R1.fastq.gz ; cat *R2* > R2.fastq.gz
I am new to the BASH command structure so I apologize if this is a basic question.
Would appreciate any help
Thank You

Maybe you need something like this:
for folder in $(ls -d ROOT_PATH/*/)
do
cd $folder && cat.......
done

Related

looping over subdirectories and files in the sub directories in linux

I am new to the site and to Linux.
I am trying to write a command line code in Linux to loop over sub directories of a given directory then over the files in these sub-directories. perform a certain function then output the result to new sub-directories with the same name in a different directory.
Here is my code
for dirname in $(ls);
do
cd $dirname
for file in $(ls)
do
mkdir -p ~/path/to/new directory/$dirname/$filname
zcat $file| ~/path/to/ptogram stdin --outdir ~/path/to/new directory/$dirname/$file
done
cd ..
done
although the code is working , i want to know if there is a simpler way.. I have a feeling my code is not clean maybe because I am not familiar with all the syntax in Linux
Thank you!

Go to bottom most directory?

I'm working with a directory with a lot of nested folders like /path/to/project/users/me/tutorial
I found a neat way to navigate up the folders here:
https://superuser.com/questions/449687/using-cd-to-go-up-multiple-directory-levels
But I'm wondering how to go down them. This seems significantly more difficult, but a couple things about the directory structure help. Each directory only has another directory in it, or maybe a directory and a README.
The directory I'm looking for looks more like a traditional project and might have random directories and files in it (more than any of the other higher directories certainly).
Right now I'm working on a solution using uh.. recursive bash functions cd'ing into the only directory underneath until there are either 0 or 2+ directories to loop through. This doesn't work yet..
Am I overcomplicating this? I feel like there could be some sweet solution using find. Ideally I want to be able to type something like:
down path
where path is a top-level folder. And that will take me down to the bottom folder tutorial.
There is an environment variable named CDPATH. This variable is used by cd in the same manner that executables use PATH when searching for pathname.
For example, if you have the following directories:
/path/to/project/users/me
/path/to/project/users/me/tutorial
/path/to/project/users/him
/path/to/project/users/him/test
/path/to/project/users/her
/path/to/project/users/her/uat
/path/to/project/users/her/dev
/path/to/application
/path/to/application/conf
/path/to/application/bin
/path/to/application/share
export CDPATH=/path/to/project/users/me:/path/to/project/users/him:/path/to/project/users/her:/path/to/application
A simple command such as cd tutorial will search the above paths for tutorial.
Let's pretend /path/to/application has directories underneath namely, conf, bin, share. A simple cd conf will send you to /path/to/application/conf as long as none of the paths before it have conf directory. This behavior is similar to executables in PATH. The first occurrence always gets chosen
My attempt - this actually works now! I'm still afraid it could easily go infinite with symbolic links or some such.
Also, I have to run this like
. down
from within the first empty folder.
#!/bin/bash
function GoDownOnce {
Dirs=$(find ./ -maxdepth 1 -mindepth 1 -type d)
NumDirs=$(echo $Dirs | wc -w)
echo $Dirs
echo $NumDirs
if [ "$NumDirs" = "1" ]; then
cd $Dirs
GoDownOnce
fi
}
GoDownOnce
A friend also suggested this sweet one liner:
cd $(find . -type d -name tutorial)
Admittedly this isn't quite what I asked, but it gets the job done pretty well.

Need help for shell script to copy multiple dir and packing into tar

Im amateur in script shell so i need help for shell cripting for my issue :
i have 2 directory inside this path /home/backup :
CSC
DFG
and each folder (CSC & DFG) have a these dir inside : weekly and monthly
and i want to copy all files and packing into 1 file .tar inside weekly dir > into monthly dir
so my question :
hows the script for this my issue?
Thanks
I'm not sure I understand your question, but if you want to create a tar file, here is the command (with verbose):
tar -cvf backup.tar /home/backup/*
and then if you need to compress it, which I assume you want to do:
gzip backup.tar

shell script to increment file names when a directory contents changes (centos)

I have a folder containing 100 pictures from a webcam. When the webcam sends a new picture, I want this one to replace number 0 and have all the other jpg's move up one number. I've set up a script where inotify monitors a directory. When a new file is put into this directory the script renumbers all the files in the picture directory, renames the new uploaded picture and puts it in the folder with the rest.
This script 'sort of' works. 'Sort of', because sometimes it does what it's supposed to do and sometimes it complains about missing files:
mv: cannot stat `webcam1.jpg': No such file or directory
Sometimes it complains about only one file, sometimes 4 or 5. Of course I made sure all 100 files were there, properly named before the script was run. After the script is run, the files it complains about are indeed missing.
This is the script, in the version I tested the full paths to the directories are used of course.
#!/bin/bash
dir1= /foo # directory to be watched
while inotifywait -qqre modify "$dir1"; do
cd /f002 #directory where the images are
for i in {99..1}
do
j=$(($i+1))
f1a=".jpg"
f1="webcam$i$f1a"
f2="test"
f2="webcam$j$f1a"
mv $f1 $f2
done
rm webcam100.jpg
mv dir1/*.jpg /f002/webcam0.jpg
done
I also need to implement some error checking, but for now I don't understand why it is missing files that are there.
You are executing the following mv commands:
mv webcam99.jpg webcam100.jpg
...
mv webcam1.jpg webcam2.jpg
The mv webcam0.jpg to webcam1.jpg is missing. With the first change to "$dir" you have the following files in /foo2:
webcam99.jp
...
webcam2.jpg
webcam0.jpg
With subsequent "$dir" change you will have the following:
webcam99.jp
...
webcam3.jpg
webcam0.jpg
In other words -- you are forgetting to move webcam0.jpg to webcam1.jpg. I would modify your script like this:
rm webcam99.jpg
for i in {98..0}
do
j=$(($i+1))
f1a=".jpg"
f1="webcam$i$f1a"
f2="test"
f2="webcam$j$f1a"
mv $f1 $f2
done
mv dir1/*.jpg /f002/webcam0.jpg

Move all files in specified folder up one directory

I have a program that extracted files to a series of sub-folders each with a "header" sub-folder. For example:
/share/Videos/Godfather.Part.1
/share/Videos/Godfather.Part.1/<packagename>
/share/Videos/Godfather.Part.1/<packagename>/Godfather.avi
/share/Videos/Godfather.Part.2
/share/Videos/Godfather.Part.2/<packagename>
/share/Videos/Godfather.Part.2/<packagename>/Godfather2.avi
I'd like to take the files in the specified folder <packagename> and move them up one directory so that the file structure looks like this:
/share/Videos/Godfather.Part.1
/share/Videos/Godfather.Part.1/Godfather.avi
/share/Videos/Godfather.Part.2
/share/Videos/Godfather.Part.2/Godfather2.avi
How can I accomplish this task in bash command line? Mind you this is an example using 2 folders, I have 100's like this.
Share and enjoy.
for i in `find . -name "*avi"`
do
dest=`dirname $i`
mv $i $dest/..
done

Resources