Looking for an Applescript to find files and move them to different folder - find

I'm trying to find a script to find and move files to a different folder.
I've got a folder with hundreds pictures like this:
PA-600-01.jpg, PA-600-02.jpg, PA-600-03.jpg, PA-600-04.jpg, PA-601-01.jpg, PA-601-02.jpg, PA-601-03.jpg, PA-602-01.jpg, PA-602-02.jpg, PA-602-03.jpg, PA-602-04.jpg, PA-602-05.jpg
I want to move all the pictures with PA-600 (so PA-600-01.jpg, PA-600-02.jpg, PA-600-03.jpg and PA-600-04.jpg) on a folder (new or already existing, the easier...) named PA-600, move all the pictures with PA-601 (PA-601-01.jpg, PA-601-02.jpg and PA-601-03.jpg) on a folder named PA-601, move all the pictures with PA-602 (PA-602-01.jpg, PA-602-02.jpg, PA-602-03.jpg, PA-602-04.jpg and PA-602-05.jpg) on a folder named PA-602... until PA-699
I tried to move a file but not a group of files:
tell application "Finder" make new folder at alias "Macintosh HD:Users:AirYoSo:Desktop:600-699" with properties {name:"PA-600"} copy file "Macintosh HD:Users:AirYoSo:Desktop:600-699:PA-600-01.jpg" to folder "Macintosh HD:Users:AirYoSo:Desktop:600-699:PA-600" end tell

Try:
set myFolder to (choose folder)
set pFolder to POSIX path of myFolder
set folderNames to paragraphs of (do shell script "find " & quoted form of pFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | grep -Eo PA-[0-9]{3} | uniq")
repeat with aFolder in folderNames
(do shell script "mkdir -p " & quoted form of (pFolder & aFolder))
tell application "System Events" to move (every file of myFolder whose name begins with aFolder) to (pFolder & aFolder)
end repeat
EDIT
If you want to hard wire the path to the folder you can use:
set myFolder to "Macintosh HD:Users:YoSo:Desktop:test"
set pFolder to myFolder's POSIX path & "/"
set folderNames to paragraphs of (do shell script "find " & quoted form of pFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | grep -Eo PA-[0-9]{3} | uniq")
repeat with aFolder in folderNames
(do shell script "mkdir -p " & quoted form of (pFolder & aFolder))
tell application "System Events" to move (every file of folder myFolder whose name begins with aFolder) to (pFolder & aFolder)
end repeat

No idea how to do it in applescript, but this is quite trivial to do in bash, which you have installed on your Mac:
#!/bin/bash
for (( c=600; c<=699; c++ ))
do
echo "Processing PA-$c"
mkdir -p PA-$c
mv PA-$c-*.jpg PA-$c/
done
Save this to a file, for example script.sh, copy the file to the directory with your jpg files, and run it like this, in Terminal (replace /Users/lionel/files with the real path to your files):
$ cd /Users/lionel/files
$ bash script.sh

Related

access failed error - no such file while trying to move files

I am trying to move all the *.csv files to another folder on server but every time i get access failed error , I am able to get all the files to local server using mget but mv fails everytime , i can see the file on the server and got full permissions on the files, sh script is not working with wild characters. struck here with the simple command .
Download to local directory
localDir="/home/toor/UCDownloads/"
[ ! -d $localDir ] && mkdir -p $localDir
#sftp in the file directory to be downloaded
remoteDir="/share/CACHEDEV1_DATA/Lanein1/Unicard/"
#The file to be downloaded is fileName
lftp -u ${sftp_user},${password} sftp://${host}:${port}<<EOF
PS4='$LINENO: '
set xfer:log true
set xfer:log-file "$logfileUCARC"
set xfer:clobber true
set xfer:auto-rename true
debug 9
cd ${remoteDir}
lcd ${localDir}
#mget *.CSV
ls -l
mv "/share/CACHEDEV1_DATA/Lanein1/Unicard/"*.csv "/share/CACHEDEV1_DATA/Lanein1/Unicard/Archives/"
#rm /share/CACHEDEV1_DATA/Lanein1/Unicard/!(*.pdf)
bye
EOF
This is not a shell or Bash problem. It is a LFTP problem.
From the manual of LFTP:
mv file1 file2
Rename file1 to file2. No wildcard expansion is performed.
LFTP just does not support what you asking for. It will treat *.csv as a part of the file name.
See here for an alternative.

Move files and copy subtree

source="/somedir/dir-a"
dest="/somedir2/dir-z"
I need to find all files recursively within the $source directory which contain the string 720p and move them to $dest
Just 2 things to take care of -
For all such files which are to be moved , first create that file's outer 2 directories in $dest and then move this matched file inside that
i have to do this for lakhs of files so a bit of parallelization would be helpful
Example
For a file like - "$source/dir-b/dir-c/file-720p.mp4" , it should do as follows :
mkdir -p "$dest/dir-b/dir-c"
mv "$source/dir-b/dir-c/file-720p.mp4" "$dest/dir-b/dir-c/file-720p.mp4"
You're looking for something like this:
src=foo
dst=bar
export dst
find "${src}" -name '*720p*' -type f -exec sh -c '
for p; do
np=${dst}${p#"${p%/*/*/*}"}
echo mkdir -p "${np%/*}" &&
echo mv "$p" "$np"
done' sh {} +
This can be parallelized using GNU find's -print0 primary in conjunction with GNU xargs, but I don't think that'd make much of a difference performance-wise, as this is rather an IO-intensive task.
Remove echos if the output is satisfactory.

sh Script with copy and removing a part from filename

So i use a script that copy's specific files to specific folders based on there filenames. I would like to extend the script so that after the copy progress a part from the filename is removed. Here is an example The filename looks likes this Image_000058_19_12_2019_1920x1080.jpg and i like to remove the resolution (_1920x1080) part from it. Is there a way to add it to my Script (see below) Thanks to #fedxc for the script.
cd "$HOME/Downloads"
# for filename in *; do
find . -type f | while IFS= read filename; do # Look for files in all ~/Download sub-dirs
case "${filename,,*}" in # this syntax emits the value in lowercase: ${var,,*} (bash version 4)
*.part) : ;; # Excludes *.part files from being moved
move.sh) : ;;
# *test*) mv "$filename" "$HOME/TVshows/Glee/" ;; # Using move there is no need to {&& rm "$filename"}
*test*) scp "$filename" "imac#imac.local:/users/imac/Desktop/" && rm "$filename" ;;
*american*dad*) scp "$filename" "imac#imac.local:/users/imac/Movies/Series/American\ Dad/" && rm "$filename" ;;
*) echo "Don't know where to put $filename" ;;
esac
done```
I use variable operation for bash. Example:
export filename='Image_000058_19_12_2019_1920x1080.jpg' <----Setting name of filename
echo ${filename/_1920x1080/} <--Operation with bash variable.
Image_000058_19_12_2019.jpg <--Result of echo
Consult this page for more: Bash Guide

Split all of the files in a directory by 1000 lines and process them through a perl script

I used this line to try and split all of the files in a directory into smaller files and put them in a new directory.
cd /path/to/files/ && find . -maxdepth 1 -type f -exec split -l 1000 '{}' "path/to/files/1/prefix {}" \;
The result was 'no file found', so how do I make this work so that I split all of the files in a directory into smaller 1000-line files and place them in a new directory?
Later on...
I tried many variations and this is not working. I read another article that split cannot operate on multiple files. Do I need to make a shell script, or how do I do this?
I had a bright idea to use a loop. So, I researched the 'for' loop and the following worked:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
.txt. is in all of the files in the working directory. the 'echo' command was optional. for the 'split' command, instead of naming one file, I replaced that with $f as defined by the 'for' line.
The only thing I would like to have been able to do is move all of these to another directory in the command.
Right now, I am stuck on the find command for moving all matching files. This is what I have done so far that is not working:
find . -type f -name '1split.*' -exec mv {} new/directory/{} \;
I get the error ' not a directory ' ; or I tried:
find . -type f -name '1split.*' -exec mv * . 1/ \;
and I get ' no such file or directory '
Any ideas?
I found that this command moved ALL of the files to the new directory instead of the ones specifically meeting the criteria '1split.*'
So, the answers to my questions are:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
and
mv *searchcriteria /new/directory/path/
I did not need a find command for this after all. So, combining both of these would have done the trick:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
mv *searchcriteria /new/directory/path/ | echo "done."
---later on...
I found that this basically took 1 file and processed it.
I fixed that with a small shell script:
#!/bin/sh
for f in /file/path/*searchcriteria ; ## this was 'split.*' in my case
do echo "Processing $f in /file/path/..." ;
perl script.pl --script=options $f > output.file ;
done ;
echo "done."

bash script problem, find , mv tilde files created by gedit

im using linux with gedit which has the wonderful habit of creating a temp file with a tilde at the end for every file I edit.
im trying to move all of these files at once to a different folder using the following:
find . -iname “*.php~” -exec mv {} /mydir \;
However, its now giving me syntax errors, as if it were searching through each file and trying to move the piece of text. I just want to move all of the files ending in .php~ to another directory. Any idea how I do that?
Cheers Ke
Try this one-liner:
for D in `find . -iname "*.php~"`; do mv ${D} /mydir; done
For future reference, if you go into Edit > Preferences > Editor Tab, there is checkbox for "Create a backup copy of files before saving" That is the guy responsible for creating the tilde version.
GNU find
find . -iname "*.php~" -exec mv "{}" /mydir +;
or
for file in *.php~
do
echo mv "$file" /mydir
done