The parent directory has 5 sub folders, each subfolder has .mp4s, .txt, and other file extensions, how to be in the parents folder and enter a terminal command to only pull all *.mp4s into another specified folder in Bash.
find /path/to/src -type f -name “*.mp4” | xargs -iF mv F /path/to/dst
I stand in the specified parent directory and move the files to the other specified folder that I assume is ../other-spec-dir ( a folder that is not in the search path of find)
find . -type f -name "*.mp4s" -exec mv {} ../other-spec-dir \;
Note that if there are files with identical name only the last one will survive.
Related
I have this directory structure. How to list only files in all F40 sub sub folders in bash? Thanks
The find command, combined with ls, can do it. For instance like this:
find DIR -type d -name F40 -exec ls {} \;
As the GNU find man page says, the find command is used for file search in a directory hierarchy.
In this case find searches the DIR folder for folders (-type d) explicitly named F40, and then it executes the ls (list directory) command to show what's inside.
You can your xargs with find command
find DIR -type d -name F40 | xargs ls
I have a directory that has many folders in it that have sub folders again and then there are some files. Can I write a bash script that copies all the files from the given directory into one single folder? So that I do not have to navigate through ever single folder and copy the content to another folder.
In the topmost dir under which you want the files to be copied:
find . -type f -exec cp {} /some/new/location \;
Finds all the normal files and then copies them to /some/new/location
You could use find to list all files inside the folder:
find ./source -type f
and then use the output as argument for cp, it would look like this:
cp $(find ./source -type f) destination
There would be problem if there are some files within the original directory tree with conflicting name. In that case cp would refuse to copy additional files with the same name with error like:
cp: will not overwrite just-created destination/t22' with./source/test/t2/t22'
To make copies of files with same name, you can use backup option, like this:
cp --backup=numbered $(find ./source -type f) destination
If you want to see what is happening use -v (verbose) option
cp -v --backup=numbered $(find ./source -type f) destination
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.
I have a folder called "Music" which is my parent folder. This folder contains alot of .mp3- and .m4a-files, some in the root of the folder and others in subfolders that can have several subfolders.
How do I use terminal on my Mac to recursively move all files from the subfolder, up into the root of my Music folder and replace existing/duplicate files?
Thanks!
have a good backup ;-) Seriously!
cd to your Music folder
type find . -type f -iname '*.mp3' -mindepth 2 -print0 | xargs -0 -I{} mv -n '{}' .
I want to find all *.pdf files recursively in directory /dir
In that dir there are specific files such as /dir/1/2.pdf [modified today], /dir/2/3.pdf [modified today], /dir/4/4.pdf [from yesterday]
IN that case I only want files that were modified today so: 2.pdf and 3.pdf
I also want to move those files to directory called /pdf/
I found that I can find all files in current directory modified today with:
find -maxdepth 1 -type f -mtime -1
How can I find files from today in subdirectories and move them to /pdf/ dir?
Thanks!!!
Adam
Remove -maxdepth 1 from your find command to find files in subdirectories and use -exec to move those to the target directory:
find /dir -type f -mtime -1 -exec mv {} /pdf \;