Moving files into new subdirectory, except for existing folders? - windows

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

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.

"for loop" to run rsync on relevant folders only

I have a folder contains around 650 folders (source), I generated a list of the relevant folders i want (final.txt).
i am trying to use a "for loop" to copy only the relevant sub-folders to a new location (target).
i keep getting the original content of the "source" copied to the "target".
i run:
for var in `cat final.txt` ; do rsync -ah $var source/ target/ ; done
I tried different syntax but can't seem to get what I need.
what am i doing wrong?
I expect to copy only the folders which name is in the final.txt list copied to the target (all "names" in the file are a single word, matching to some of the folder names for exactly)
ok after messing around I should have ran this (it works)
for var in `cat final.txt` ; do rsync -ah source/$var target/ ; done

Moving or copying files from one directory to multiple directories

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?

Mac OS - Batch Rename All Files in Folder but Disregard All SubFolders

I have a bunch of folders that I would like to rename all the files contained within minus any subdfolders.
For example lets say I have two parent folders:
ParentFolder1 - [PF1]
ParentFolder2 - [PF2]
Each parent folder has various amounts of subfolders:
SubParentFolder1_1
SubParentFolder1_2
SubParentFolder2_1
Inside the ParentFolder and each SubParentFolder there can be files such as .mp3, .txt. etc. or more subfolders.
How would I go about renaming all and any files in this manner:
example.mp3 -> example - [PF1]
example.txt -> example - [PF2]
example.docx -> example - [PF2]
Appreciate any input!
This is a way to list files (not folders) in a range of directories and then do something with them... Specifics of renaming are up to you.
for FOLD in Parent*;
do for FILE in $(ls -p $FOLD | grep -v "/");
do echo "$FOLD/$FILE" "$FOLD/${FILE%.*}";
done; done;
Explanation:
For each folder (FOLD) in directories matching the wildcard Parent*,
list the contents, adding / to the end of directory names.
Do inverse grep on that list, leaving just the file names.
Loop through each FILE and echo out the original folder+file, followed by the folder and file with the suffix removed by patten matching.
Once you test this, you can replace echo with mv to do the actual renaming... (I've put these on separate lines to make them more readable, but would usually run this as one long command.

Transferring files between folders

I need a solution for this script to move files that will drop into the 8953-x folders to a joint folder.
The files that drops to the 8953-x folders will automatically be moved to the joint folder /mnt/FOLDER.
It will move all files besides files containing ! in their filename, for example picture.jpg.!sync.
Files ending with .!sync are in sync between servers, using btsync and not complete, they shall be ignored. When the sync end, the file output will change to picture.jpg, and then I want it to be transferred to the joint folder.
#!/bin/bash
from_folders=(8953-10 8953-11 8953-12 8953-3 8953-4 8953-5 8953-6 8953-7 8953-8 8953-9)
${from_folders[#]}
I believe this should work, but please test on a backup directory.
shopt -s extglob; mv 8953-*/!(\!*) /mnt/FOLDER
This turns on pattern matching and then moves all files in those folders which do not start with exclamation to the destination folder.
If the files all are named .jpg when ready, it is easier:
mv 8953-*/*.jpg /mnt/FOLDER
This is an improved script:
#!/bin/bash
shopt -s extglob
from_folders=(8953-10 8953-11 8953-12 8953-3 8953-4 8953-5 8953-6 8953-7 8953-8 8953-9)
for folder in ${from_folders[#]}
do
echo mv $folder/!(\!*) /mnt/finished_fotograf
done

Resources