Move all files in specified folder up one directory - linux

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

Related

Copy files within multiple directories to one directory

We have an Ubuntu Server that is only accessed via terminal, and users transfer files to directories within 1 parent directory (i.e. /storage/DiskA/userA/doc1.doc /storage/DiskA/userB/doc1.doc). I need to copy all the specific files within the user folders to another dir, and I'm trying to specifically target the .doc extension.
I've tried running the following:
cp -R /storage/diskA/*.doc /storage/diskB/monthly_report/
However, it keeps telling me there is no such file/dir.
I want to be able to just pull the .doc files from all the user dirs and transfer to that dir, /storage/monthly_report/.
I know this is an easy task, but apparently, I'm just daft enough to not be able to figure this out. Any assistance would be wonderful.
EDIT: I updated the original to show that I have 2 Disks. Moving from Disk A to Disk B.
I would go for find -exec for such a task, something like:
find /storage/DiskA -name "*.doc" -exec cp {} /storage/DiskB/monthly_report/ \;
That should do the trick.
Use
rsync -zarv --include="*/" --include="*.doc" --exclude="*" /storage/diskA/*.doc /storage/diskB/monthly_report/

How to loop a function in a sub-directory

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

copy/move same name multiple files in different folder

I have multiple files in different folders with the same name and extension. For example: There are 460 folders and each folder has one file with the name of snps.vcf. I want to copy/move these files to one folder and later on, I will do some analysis that I need to do.
I have tried:
find -type f -name "*.vcf" -exec cp {} /home/AWAN/try';'
but this code overwrites the files and only one file remains there in the end.
I have tried rename but I don't know how to select multiple files by find command then rename. Even with the mmv I couldn't find the possible solution.
You need to write an external script and pass it to -exec.
Your script may use mktemp to generate a random file name. Example:
mktemp /your/directory/try-XXX
The XXX part will be replaced by mktemp with a different value for each call.

How to move a certain pattern of subfolders to another folder, keeping the structure using bash?

I have a set of folders like:
/path/to/group1/folder/number123
/path/to/group1/folder/number456
/path/to/group2/folder/number123
/path/to/group2/folder/number456
/path/to/group3/folder/number123
/path/to/group3/folder/number456
And I want to move folders that match /path/to/group*/folder/number123
to the base folder /path/toOther/ so that after the move it looks like:
/path/to/group1/folder/number456
/path/to/group2/folder/number456
/path/to/group3/folder/number456
/path/toOther/group1/folder/number123
/path/toOther/group2/folder/number123
/path/toOther/group3/folder/number123
Is there a way to do this with a move command and wild cards, or will it require more than a 1-liner?
If you don't mind writing a few lines:
cd /path/to
for f in group*/folder/number123
do
d="/path/toOther/${f%/*}"
mkdir -p "$d"
mv "$f" "$d/."
done
Of course you can combine the script into one line. (Or the bash will do this when you recall it with the Up key.)

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.

Resources