Eliminating subfolders to move all files into one folder - linux

I have a folder that contains 32 folders, each with several image files. I would like to move all of these image files into one main folder. I know how to do that manually, folder by folder. Is there an automated command-line way to do that? I have Crunchbang Waldorf, and usually use PCmanFM as a file manager.

/*/ stands for directories.
mv /path/from/*/*.jpg /path/main/

if all these images have one extension, for instance .jpg:
find /directory/You/Want/To/Search -name "*.jpg" -exec cp -t /destination/directory {} +
Note: just make sure that all these images have one unique name otherwise this command would break
UPDATE:
if you don't know what are the images extensions you could just do that one:
find /directory/You/Want/To/Search -regex ".*\.\(jpg\|gif\|png\|jpeg\)" -exec cp -t /destination/directory {} +

Related

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.
However I also want to move and harvest all of the files who are in sub folders so:
Pictures/1.png
Pictures/yolo/2.png
Pictures/yolo/swag/sand/3.png
Pictures/extra/fire/4.png
I want to move or copy all these files to another folder like results so I get:
results/1.png
results/2.png
results/3.png
results/4.png
Only I have no idea in advance what sub folders will be in the Pictures folder.
How can I accomplish this in bash/shell scripts ?
I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).
You can do it like this:
find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results \;
Another option is to use xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
You can simply copy all files and subdirectories along with their contents using cp's recursive option:
cp -pr <source_path>/* <destination_path>/
But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.
cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

Linux find and copy files with same name to destination folder do not overwrite

I want to find and copy all files with *.jpg in one folder includes its sub folder to another folder
I use
find /tempL/6453/ -name "*.jpg" | xargs -I '{}' cp {} /tempL/;
but it overwrite files with same name
for example in /tempL/6453/, there are test (1).jpg test (2).jpg and folder 1, in /tempL/6453/1/, there are also have files with the same name test (1).jpg test (2).jpg
If I use the above command, there are only two files test (1).jpg test (2).jpg in /tempL/, it can not copy all files to /tempL/.
What I want is to copy all files to /tempL/, when there are same file name, just rename them, how to?
What I want is to copy all files to /tempL/, when there are same file name, just rename them, how to?
1) If you only do not what overwrite cp --backup will give you a backup for existing file, with --suffix option of cp, you can also specify the suffix to be used for backup.
2) --parents option of cpwill keep directory tree, i.e. files in folder 1 will be copy to new created 1 folder.
3) If you want to customize your rename processing, you can not use cp command only. write script for it and call it to process the result of find
Install "GNU parallel" and use:
find /tempL/6453/ -name "*.jpg" | parallel 'cp {} ./dest-dir/`stat -c%i {}`_{/}'
{/} ................. gets filename with no full path
I think the same approach should be possible with xargs, but learning about parallel was amazing for me, it gives us many beautiful solutions.
I recommend using echo before cp in order to test your command

Moving files without overwrite

I'm using the following command to move all files in subfolders to a destination folder, without overwriting files with same name:
find folder-target -type f -exec cp --backup=numbered \{\} folder-final \;
And this is causing the files append ~1~ if the file already exists. The problem is: this is causing the file usuless. I need catch all my pdfs, and i can't open this pdfs if they have this numbers.
Is this fixable? Can't i use a pre-fix?
Thanks.
try cp -n
see man pages here : http://man7.org/linux/man-pages/man1/cp.1.html

Rsync make flat copy

I'm trying to write a script that copy all the files of one dir (with subdirs) to the root of another dir.
So Imagine I have this file structure:
/
pic.JPG
PIC5.JPG
FOLDER
pic2.JPG
pic3.JPG
FOLDER2
pic4.JPG
I want all the .JPG files from that directory and copy them over to another destination. But I don't want the directory structure, just the files.
This is what I've got:
"sudo rsync -aq --include '*/' --include '*.JPG' --exclude '*\' /source/picturesRoot/ /destination/flatView/
But it also copies the directories :(
I found this link on stackoverflow:
rsync : Recursively sync all files while ignoring the directory structure
I looked at the solution and didn't see much difference with my command, apart from the * and . in the path. I tried it but it didn't work.
I hope somebody can help me, thanks.
This answer cannot work for you because your pictures are not at the same level in directories. There is no option in rsync to skip the creation of directory structure. In the link you gave, it's working because the user explicitly select source files with *.
You can try something with find and rsync. Find will find files and rsync copy them.
Here is a solution :
find /source/picturesRoot -type f -name "*.JPG" -exec rsync -a {} /destination/flatView/ \;
Be careful, if two files have the same name just one will be in destination directory.

Bash script to recursively step through folders and delete files

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?
Change directory to the root directory you want (or change . to the directory) and execute:
find . -name "._*" -print0 | xargs -0 rm -rf
xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.
find . -name '._*' -exec rm -Rf {} \;
I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:
http://github.com/houbysoft/short/blob/master/tidy
find /path -name "._*" -exec rm -fr "{}" +;
Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean.
dot_clean -- Merge ._* files with corresponding native files.
For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.
Because dot_clean works recursively by default, use:
dot_clean <directory>
If you want to turn off the recursively merge, use -f for flat merge.
dot_clean -f <directory>
find . -name '.*' -delete
A bit shorter and perform better in case of extremely long list of files.

Resources