Transferring files between folders - bash

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

Related

"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 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

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.

get list of files in a folder BUT in a predetermined order

I am using the below code to extract the names of files in a folder and then performing an operation on those files.
for library in lib/*
do
echo ${library##*/}
cp -v ${library##*/} /opt/java/deploy/
done
}
The above yields an output of :
camel-amqp-2.16.0.jar
netty-all.jar
proton-j.jar
qpid-jms-client.jar
I then need to copy the jar files one by one in the application deployment folder.
Right now I have 4 jar files but in the future the # of files will increase.
The above works all fine. But the dilemma for me is that I have to make sure that qpid-jms-client.jar & camel-amqp-2.16.0.jar are the last to load.
Basically I need to sort the output so that the qpid & camel jar files are at the bottom of the list and are hence the last ones to be loaded.
Using sort does not help here since sorting alphabetically wont be of much use.
Is there something else I can try out ?
Using extglob you can do this:
shopt -s extglob nullglob
for lib in !(#(qpid-jms-client|camel-amqp-*)).jar qpid-jms-client.jar camel-amqp-*.jar; do
echo "${lib##*/}"
cp -v "${lib##*/}" /opt/java/deploy/
done
How it works:
extglob pattern !(#(qpid-jms-client|camel-amqp-*)).jar matches all *.jar files except qpid-jms-client.jar & camel-amqp-2.16.0.jar files.
Those 2 files are listed in the end to make sure these 2 jar files are always matched in the end

Adding a status (file integrity)check to a cbr cbz converting bash script

First post, so Hi! Let me start by saying I'm a total noob regarding programming. I understand very basic stuff, but when it comes to checking exit codes or what the adequate term is, I'm at a loss. Apparently my searchfoo is really weak in this area, I guess it's a question of terminology.
Thanks in advance for taking your time to reading this/answering my question!
Description: I found a script that converts/repack .cbr files to .cbz files. These files are basically your average rar and zip files, however renamed to another extension as they are used for (comic)book applications such as comicrack, qcomicbook and what not. Surprisingly enough there no cbr -> cbz converters out there. The advantages of .cbz is besides escaping the proprietary rar file format, that one can store the metadata from Comic Vine with e. g comictagger.
Issue: Sometimes the repackaging of the files doesn't end well and would hopefully be alleviated by a integrity check & another go. I modified said script slightly to use p7zip as it can both pack/unpack 7z, zip-files and some others, i. e great for options. p7zip can test the archive by:
7z t comicfile.cbz tmpworkingdir
I guess it's a matter of using if & else here(?) to check the integrity and then give it another go, if there are any error.
Question/tl;dr: What would be the "best"/adequate approach to add a integrity file check to the script below?
#!/bin/bash
#Source: http://comicrack.cyolito.com/forum/13-scripts/30013-cbr3cbz-rar-to-zip-conversion-for-linux
echo "Converting CBRs to CBZs"
# Set the "field separator" to something other than spaces/newlines" so that spaces
# in the file names don't mess things up. I'm using the pipe symbol ("|") as it is very
# unlikely to appear in a file name.
IFS="|"
# Set working directory where to create the temp dir. The user you are using must have permission
# to write into this directory.
# For performance reasons I'm using ram disk (/dev/shm/) in Ubuntu server.
WORKDIR="/dev/shm/"
# Set name for the temp dir. This directory will be created under WORDDIR
TEMPDIR="cbr2cbz"
# The script should be invoked as "cbr2cbz {directory}", where "{directory}" is the
# top-level directory to be searched. Just to be paranoid, if no directory is specified,
# then default to the current working directory ("."). Let's put the name of the
# directory into a shell variable called SOURCEDIR.
# Note: "$1" = "The first command line argument"
if test -z "$1"; then
SOURCEDIR=`pwd`
else
SOURCEDIR="$1"
fi
echo "Working from directory $SOURCEDIR"
# We need an empty directory to work in, so we'll create a temp directory here
cd "$WORKDIR"
mkdir "$TEMPDIR"
# and step into it
cd "$TEMPDIR"
# Now, execute a loop, based on a "find" command in the specified directory. The
# "-printf "$p|" will cause the file names to be separated by the pipe symbol, rather than
# the default newline. Note the backtics ("`") (the key above the tab key on US
# keyboards).
for CBRFILE in `find "$SOURCEDIR" -name "*.cbr" -printf "%p|while read line; do
# Now for the actual work. First, extract the base file name (without the extension)
# using the "basename" command. Warning: more backtics.
BASENAME=`basename $CBRFILE ".cbr"`
# And the directory path for that file, so we know where to put the finished ".cbz"
# file.
DIRNAME=`dirname $CBRFILE`
# Now, build the "new" file name,
NEWNAME="$BASENAME.cbz"
# We use RAR file's name to create folder for unpacked files
echo "Processing $CBRFILE"
mkdir "$BASENAME"
# and unpack the rar file into it
7z x "$CBRFILE" -O"$BASENAME"
cd "$BASENAME"
# Lets ensure the permissions allow us to pack everything
sudo chmod 777 -R ./*
# Put all the extracted files into new ".cbz" file
7z a -tzip -mx=9 "$NEWNAME" *
# And move it to the directory where we found the original ".cbr" file
mv "$NEWNAME" $DIRNAME/"$NEWNAME"
# Finally, "cd" back to the original working directory, and delete the temp directory
# created earlier.
cd ..
rm -r "$BASENAME"
# Delete the RAR file also
rm "$CBRFILE"
done
# At the end we cleanup by removing the temp folder from ram disk
cd ..
echo "Conversion Done"
rm -r "$TEMPDIR"
Oh the humanity, not posting more than two links before 10 reputation and I linked the crap out of OP.. [edit]ah.. mh-mmm.. there we go..
[edit 2] I removed unrar as an dependency and use p7zip instead, as it can extract rar-files.
You will need two checks:
7z t will test the integrity of the archive
You should also test the integrity of all the image files in the archive. You can use at tools like ImageMagick for this.
A simple test would be identify file but that might read only the header. I'd use convert file -resize 5x5 png:- > /dev/null
This scales the image down to 5x5 pixels, converts it to PNG and then pipes the result to /dev/null (discarding it). For the scaling, the whole image has to be read. If this command fails with an error, something is wrong with the image file.

Resources