Mac Terminal command move files from subdirectory to parent - macos

On my Mac I amm trying to move hundreds of files on my NAS drive, from a parent directory with a load of subdirectories (and possibly directories inside them) and put all of the files into one folder.
They don't have the same file extension for all the files.
Is anyone able to help with the terminal command I need to do this? So far I know that find . -type f will list all the files in the directory and subdirectories but Im unsure how to tell it to get them to move them all into another folder.

For anyone else who may have this same issue:
Ive managed to extract just the .jpg's and put them in the parent folder.
find . -type f -iname '*.jpg' -mindepth 2 -print0 | xargs -0 -I{} mv -n '{}' .
Not quite what I wanted - I was hoping to get every single file and put it into a completely different folder if possible but this has got me further than before.

Go inside the source parent directory and use:
find . -type f -exec mv "$PWD"/{} <destination directory> \;
If you want to move all the files to parent directory itself, use it as the destination directory.

Related

Renaming all files within subdirectories

I am trying to rename all the files with certain regex patterns within the same subdirectory. Right now, I am in the current directory (above all the sub directories) and am running the following code:
for d in */ ; do
cp $d*hw4*.pl $dhw4.pl
done
I wish to find all the files with hw4 in them, and rename them to hw4.pl and keep them in the same directory. Could someone assist me with this? I know you can do this with the mv command, but I don't want to make a mistake, so I am using copy for now.
Thank you.
I suggest to use find, like this:
find . -type f -name '*hw4*' -execdir mv {} hw4.pl \;

Find index.html files & Rename with folder name and move file a level up

Little help in solving a workflow with shell script using the find command.
Finding all index.html files in every folder.
We can use this with find command for that.
find ./ -type f -name 'index.html'
Renaming the file index.html with the folder names.
After renaming the files, I wanted to move the files one level up.
I'm stuck at renaming and moving the files one level up.
As I have more than 100k files, Xargs will be handy for this.
Here is code I have so far
find ./ -type f -name 'index.html' | xargs -P 4
Any help in in renaming the index.html files and moving the files one level up?
Instead of xargs you can use find -exec. Inside, you can run a small sh-Script:
find . -mindepth 2 -type f -name 'index.html' -exec sh -c '
d="$(dirname "$1")";
mv "$1" "$d/../$(basename "$d").html";
rmdir "$d";
' find-sh {} \;
Received the help from Askubuntu forum for it :)
Reference : https://askubuntu.com/questions/1236564/find-index-html-files-rename-with-folder-name-and-move-file-a-level-up

combine contents of files from multiple directories into a single text file in Mac OSx

I have a complicated scenario. In my current working directory, I have several subdirectories. Each subdirectory has a number of files, but I'm only interested in one: RAxML_bestTree.best. The file name is the same for each corresponding file in every subdirectory, i.e., they are not unique. Thus, a copy command to a new subdirectory will not work since one RAxML_bestTree.best will be shown and overwritten 514 times.
I need to take the content of each subdirectory's RAxML_bestTree.best and have it placed into a file all_RAxML_bestTrees.txt either in the current working directory or a new subdirectory. I have tried the following, which appears to print the contents to screen but not to file:
find . -type f -name \RAxML_bestTree.best -exec cat {} all_RAxML_bestTrees.txt \;
Nevermind, found my issue:
find . -type f -name \RAxML_bestTree.best -exec cat > all_RAxML_bestTrees.txt \;

Problems using find and cp to copy just .jpg files from a LOT of directories to one new path

I tried the search, but couldn't find the answer to my specific problem.
When I use,
find /recovered_files "*.jpg" -type f -exec cp {} /out \;
to copy all .jpg files from directories within the /recovered_files directory, the /out directory gets filled with every single file (jpg, txt, xml etc etc) from within the source directories.
Can anyone please explain wherein my stupidity lies, pleeeeeease???
Many thanks, Mark.
What you're doing at the moment is equivalent to calling cp /dir/dir/dir/file.jpg /out for each file, which will copy the file into /out. Thus, all of the files are being put into the same directory.
rsync allows filters to select only certain files to be copied. Change from and to to the appropriate directories in the following:
rsync -r from/* to --include=*.jpg --filter='-! */' --prune-empty-dirs
Credit to this post for this solution.
Edit: changed to rsync solution. Original as follows:
find from -name "*.jpg" -type f -exec mkdir -p to/$(dirname {}) \; -exec cp --parents {} to \;
You should replace from and to with the appropriate locations, and this form won't quite work if from begins with /. Just cd to / first if you need to. Also, you'll end up with all the files inside to underneath the entire directory structure of from, but you can just move them back out again.

Find files, rename in place unix bash

This should be relatively trivial but I have been trying for some time without much luck.
I have a directory, with many sub-directories, each with their own structure and files.
I am looking to find all .java files within any directory under the working directory, and rename them to a particular name.
For example, I would like to name all of the java files test.java.
If the directory structure is a follows:
./files/abc/src/abc.java
./files/eee/src/foo.java
./files/roo/src/jam.java
I want to simply rename to:
./files/abc/src/test.java
./files/eee/src/test.java
./files/roo/src/test.java
Part of my problem is that the paths may have spaces in them.
I don't need to worry about renaming classes or anything inside the files, just the file names in place.
If there is more than one .java file in a directory, I don't mind if it is overwritten, or a prompt is given, to choose what to do (either is OK, it is unlikely that there are more than one in each directory.
What I have tried:
I have looked into mv and find; but, when I pipe them together, I seem to be doing it wrong. I want to make sure to keep the files in their current location and rename, and not move.
The GNU version of find has an -execdir action which changes directory to wherever the file is.
find . -name '*.java' -execdir mv {} test.java \;
If your version of find doesn't support -execdir then you can get the job done with:
find . -name '*.java' -exec bash -c 'mv "$1" "${1%/*}"/test.java' -- {} \;
If your find command (like mine) doesn't support -execdir, try the following:
find . -name "*.java" -exec bash -c 'mv "{}" "$(dirname "{}")"/test.java' \;

Resources