Moving or copying files from one directory to multiple directories - shell

I have a directory /input/source that contains files like:
$ ls -1 /input/source
FileName_1.txt
FileName_2.txt
FileName_3.txt
FileName_4.txt
I also have a file named /tmp/temp_path.txt that contains the following paths (or directories) information like:
$ cat /tmp/temp_path.txt
/output/dest/temp_1
/output/dest/temp_2
/output/dest/temp_3
/output/dest/temp_4
I am trying to build a shell script which will read the path info from /tmp/temp_path.txt and then move the files from the source folder to the respective directories as follows
/output/dest/temp_1/FileName_1.txt
/output/dest/temp_2/FileName_2.txt
/output/dest/temp_3/FileName_3.txt
/output/dest/temp_4/FileName_4.txt
Also, wanted to do some checks and balances before moving the files to destination path like:
if count of dest path from file(/tmp/temp_path.txt) != count of input files from source folder
then move randomly
else
move sequentially
How can I do this?

Related

How do I copy files listed in a .txt into a new folder?

I have a .txt list of filenames that I would like to copy into a new folder. The filenames take the form: 0-01-199898988999.mp4. I can use the list of file names to copy files into a new folder using the following code.
cp `cat list.txt` new_folder/
However, this only works when the files I want to copy, the .txt file, and the new folder are all in the same directory, and all of my files to copy are organized into subfolders within the directory.
FolderA
->list.txt
->new_folder
->SubA
-->0-01-199898988999.mp4
-->0-02-199898988999.mp4
-->0-03-199898988999.mp4
->SubB
-->0-04-199898988999.mp4
-->0-05-199898988999.mp4
-->0-06-199898988999.mp4
-->0-06-199898988999.mp4
How can I modify the code to search through the subdirectories of folder A?
I've tried using --parents, but it hasn't worked.
cp -v --parents `cat list.txt` /new_folder
I've been searching around for answers, but most versions of this problem involve a list with the unique directory names for each file (which I don't have), copying all the files within the subdirectories, or searching by extension instead of file name.

Finding and copying files in subfolders to a unique folder giving them their path names

I need to do a bash script in order to copy all files which are in various subfolders and sub-subfolders of a main directory to a unique another folder while giving them a new name indicating their source.
Source paths and files are like:
MainDir/SubFolderX/SubSubFolderXY/fileZ.dat
MainDir/UnknowName../AnotherUnknowName../AnyFile.anyExtension
... Etc
Destination folder with renamed files would be:
NewDir/SubFolderX_SubSubFolderXY_fileZ.dat
NewDir/UnknowName.._AnotherUnknowName.._AnyFile.anyExtension
Etc
What I know is that there is only 2 levels but the names of the subfolders, the subsubfolders, the files, and their extensions could be anything.

Moving files into new subdirectory, except for existing folders?

A Redditor had written me a great script that allowed me to move all the files in a series of folders into a new subdirectory in all those folders called New. However, I also have pre-existing folders (namely just 1 called "Journals") that have had their files moved into a subdirectory called New, as well.
How would I modify the following script (on Windows) to not touch any folders within the folders, or perhaps not touch any folder called Journals?
For example, the current directory looks like:
Parent/Folder name/tons of files/
Parent/Folder name/Journals/tons of files
(folder name = random string of alphanumeric numbers in the thousands). Each folder has a ton of files, and a folder called Journals.
I would like:
Parent/randomstring folder/New/tons of files/
Parent/randomstring folder/Journals/tons of files
The code they wrote for me:
# Run from search root
cd "O:\..."
# Change this to taste
export NEWDIR=New
find . | egrep '(mp4$|jpg$|png$)' |
while read FILE
do
BASEDIR=$(dirname "$FILE")
mkdir "$BASEDIR/$NEWDIR" > /dev/null 2>&1
mv "$FILE" "$BASEDIR/$NEWDIR"
done
This code would do the following:
Parent/randomstring folder/New/tons of files/
Parent/randomstring folder/Journals/new/tons of files

How to copy files in a directory that are not listed in a text file

I have files in a directory, with a pattern in the filename ("IMP-"). I need to copy the files from the directory A to the directory B. But I also keep the files in the directory A. So in order to copy only the new files in directory B, I need, first to list each time I do a copy, the filenames in a text file (list.txt), and then to copy only the files that aren't listed in the text file.
exemple
Directory A (/home/ftp/recep/)
files, for example can be :
/home/recep/IMP-avis2018.txt
/home/recep/IMP-avis2018.pdf
/home/recep/IMP-avis2017.jpg
/home/recep/IMP-avis2017.pdf
Directory B (/home/ftp/transfert/)
In need to copy all files with IMP* to directory B (/home/ftp/transfert/).
And when a new file is receive in drectory A, I need this file, and only this file, to be copied in directory B (where files only stay 2 hours max)
I tought maybe I could do something with rsync, but I could'n find an adequate option.
So maybe it could be a bash script.
Actions would be :
have a simple basic text file containing already proceed files (for example liste.txt)
find files in directory A containing pattern IMP
for each of these files, read the liste.txt file and if the file is not listed in liste.txt, copy it to the directory B
You could try the option -n. The man page says:
-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)
So
cp -n A/* B/
should copy all files from A to B, except those that are already in B.
Another way would be rsync:
rsync -vu A/* B/
This syncs the files from A to B and prints the file that were actually copied.

Unix/Mac OS X: Use file list to copy files and folder keeping the directory structure

I have a plain text file containing names of hundreds of files with path relative to a home directory (can be made absolute path, if needed) in various sub-directories. The home directory contains multiple directories, and thousands of files. I need to create another directory copying the files in the list, while maintaining their directory structure in the destination.
Example:
Source folder:
/home/a/
file1.jpg
file2.jpg
file3.jpg
/home/b/
file4.jpg
file5.jpg
file6.jpg
File List: (plain text, in /home/)
./a/file2.jpg
./b/file5.jpg
Expected Result:
/home/dest/a/
file2.jpg
/home/dest/b/
file5.jpg
Tried cp with various modifications from various questions on stackoverflow, but got a flat folder structure in the result every time.
Using bash on OS X Terminal
Please tell how this can be done.
You can use rsync:
rsync --relative --files-from file-list.txt /home /home/dest

Resources