Copy files from multiple folders to another multiple folders Linux - linux

I have a huge list of files from different folder and i need to copy them to another folder.
I have already created those folders which I will copy.
I have tried the code:
for file in $(cat /home/pdf/report/folder/files.txt | sed $'s/\r//'); do cp "$file" /home/pdf/report/folder/dest.txt; done
Is there any solutions in Linux?

I would do it this way:
while read file
do
cp "$file" /home/pdf/report/folder/
done < /home/pdf/report/folder/files.txt
The important point is you're copying into the folder without specifying the filename, just the folder, meaning the copy will have the same name as the original

Related

Need script to move and rename files without overwriting duplicate filenames

Maybe i'm just going about this wrong and making it harder than it has to be.
This is my problem. I have 2 different scripts that download various picture files. the first downloads from email and the downloaded files go into the /attachments/ directory. The second script copies the contents of google drive, all files and folders get copied into ~/gdrive/ directory. i want to be able to move all picture files from both these folders as well as any subfolders to ~/Pictures/$today and prevent any overwriting in the case of duplicate file names. I don't mind having 2 separate scripts to handle the pictures in the 2 different directories, but I do need it to be able to get all files in subdirectories of the starting point. it also needs to be able to handle a variety of file extensions. my current solution adds a numbered extension such as .~1~ after the files normal extension .jpg, .png, .tiff, etc. I dont lose any files this way but any that wind up with a backup number after the extension are rendered useless to my project. This is what I am currently using
TODAY=$(date +"%m-%d-%Y")
mkdir -p ~/Pictures/$TODAY &&
sudo find /attachments -type f -exec mv --backup=numbered -t ~/Pictures/$TODAY {} +
My result if there are duplicate file names looks like this:
DSC07286.JPG
DSC07286.JPG.~1~
Is there a better approach than what i am doing? Is there a way to dissect the filename parts and reorganize them and do it recursively for all files in the directory? Thanks
Something like this should do it (untested; uses standard lowercase variable names and puts the index just before the extension to not mess with sorting):
for path in ~/Pictures/"$today"/*.JPG
do
index=0
for duplicate_path in "$path".~[0-9]*
do
new_path="${duplicate_path%%.*}${index}.JPG"
echo "$duplicate_path" "$new_path"
((++index))
done
done
When you're confident it's doing the right thing, simply replace echo with mv to actually move the files.
Here is my solution.
#!/bin/bash
TODAY=$(date +"%m-%d-%Y")
NOW=$(date +"%D %T")
sudo mkdir -p /home/pi/Pictures/emailpics/$TODAY &&
sudo find /attachments -type f -exec mv --backup=numbered -t /home/pi/Pictures/emailpics/$TODAY/ {} + &&
for f in /home/pi/Pictures/emailpics/$TODAY/*.~?~
do
fullfilename=$f
filepath=$(dirname "$fullfilename")
filename=$(basename "$fullfilename")
fname="${filename%.*}"
bkpnum="${filename##*.}"
file="${fname%.*}"
ext="${fname##*.}"
sudo mv $f $filepath/$file$bkpnum.$ext
done
Can't say i fully understand all the syntax for the parsing bits, but it works. maybe someone else can explain what is going on.

Create directory based on date/time and copy files to it?

I'm attempting to create a script that will create a folder based on the current time and date. I then need the script to copy the files from a source folder to the newly created folder. I then need it to copy folders from a second source folder to the original source folder, overwriting everything that's in there.
Below is what I've tried, and it's failing in quite an epic fashion.
#!/bin/bash
d="/home/$(date +%d-%m-%y")"
mkdir "$d"
cp /home/test "$d"
cp /home/test2 /home/test
I'm aware that I don't have to define the variable, as the time between copies should be seconds and not lapse a day, but I wanted to make sure and honestly, I'm interested in learning to use variables in scripting.
There is one too many double quote here:
d="/home/$(date +%d-%m-%y")"
Actually no quoting is necessary here at all, write like this:
d=/home/$(date +%d-%m-%y)
In the rest of the script, if you want to copy directories, you will need to use cp -r instead of simply cp.
Finally, note that when you do cp -r dir1 dir2 when dir2 already exists, then dir1 will be copied inside dir2, rather than overwriting its content. That is, it will create dir2/dir1. If dir1 doesn't contain hidden files, then you can write like this to overwrite the content of dir2:
cp -r dir1/* dir2/

Copy a file in all folders of a directory [duplicate]

This question already has answers here:
Copy files from one directory into an existing directory
(9 answers)
Closed 5 years ago.
Given source file path and destination path. How to write a shell script to copy source file in all folders of destination directory?
You don't need a script. Use cp -R for recursive copy:
cp -R source_path dest_path
To do a recursive copy while preserving file attributes like last modified time etc., use the -p option as well:
cp -Rp source_path dest_path
From man cp:
-R
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the
source_file ends in a /, the contents of the directory are copied
rather than the directory itself. This option also causes symbolic
links to be copied, rather than indirected through, and for cp to
create special files rather than copying them as normal files.
Created directories have the same mode as the corresponding source
directory, unmodified by the process' umask.
It sounds like you want to copy a single file into all the subdirectories under the target directory (and into the target directory itself). If that is correct, then:
find $targetdir -type d -exec cp $sourcefile {} \;

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

Copying Moving Deleting Directories in Linux with Array and/or Numerically

I have a large directory of sub-directories that has reached the ext3 limit. I need to copy some directories to an alternate path. I also need to remove some directories completely.
The directories are named numerically 1000,1001,1002,1003. I'd like to be able to copy and delete by number. i.e.
rm -rf (WHERE dirname<12000)
I also have some non-sequential directories such as 45698,59875,897526
I was able to pull these directory names from a database and I have them in an array.
What is the best way to copy and delete these?
I would copy the interesting folders first. You told that you have them in an array, maybe its better to store their names in a file? Then you cold do the following:
xargs -I {} cp -a {} /path/to/backup < list_of_interesting_folders
Then you can remove the remaining folders using brace expansion (with bash):
rm -rf {1000..12000}

Resources