How to run a Bash script when starting Windows 10?
I can only find soulations for ubuntu.
Thanks.
#!/bin/bash
# Desktop PATH
Desktop=/c/Users/118883/Desktop
# folder with the script
executeFolder=Clean-up-folder-structure
# mainfolder
sortingFolder=sorting
# inner folders
docFolder=1.word_docs
excelFolder=2.excel_docs
imageFolder=3.images
cd $Desktop
# moves all folders from desktop to a sorting folder
function moveMyFiles(){
find * -mindepth 0 -maxdepth 0 -not -name $sortingFolder -not -name
$executeFolder -exec mv -v -t $sortingFolder {} +
}
# Sort al the .docx files
function sortDocs(){
cd $sortingFolder
if [ -d $docFolder ]
then
find *.docx -mindepth 0 -maxdepth 0 -exec mv -v -t $docFolder {} +
else
mkdir $docFolder
find *.docx -mindepth 0 -maxdepth 0 -exec mv -v -t $docFolder {} +
fi
}
function sortExcel(){
cd $sortingFolder
if [ -d $excelFolder ]
then
find *.xlsx -mindepth 0 -maxdepth 0 -exec mv -v -t $excelFolder {} +
else
mkdir $excelFolder
find *.xlsx -mindepth 0 -maxdepth 0 -exec mv -v -t $excelFolder {} +
fi
}
function sortImages(){
cd $sortingFolder
if [ -d $imageFolder ]
then
find *.PNG -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.JPEG -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.jpeg -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.jpg -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.GIF -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
else
mkdir $imageFolder
find *.PNG -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.JPEG -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.jpeg -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.jpg -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
find *.GIF -mindepth 0 -maxdepth 0 -exec mv -v -t $imageFolder {} +
fi
}
#main execute
if [ -d $sortingFolder ]
then
cd $Desktop
moveMyFiles
sortDocs
sortExcel
sortImages
else
# zo niet maak een folder
cd $Desktop
mkdir $sortingFolder
moveMyFiles
sortDocs
sortExcel
sortImages
fi
When running in Powershell error appear.
syntax errors and parse errors.
like:
At line:1 char:3
+ if [ -d $sortingFolder ]
+ ~
Missing '(' after 'if' in if statement.
At line:1 char:5
+ if [ -d $sortingFolder ]
+ ~
Missing type name after '['.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement
I'd suggest the same as gazzo, but not with powershell, rather with a virtual shell such as git bash. If possible of course.
Due to how our environments set up at work, i use bash scripts through git bash to download git repositories, setup projects dynamically etc. You could set up windows to run git bash exe and pass in your bash script's path to run it.
maybe a bit complicated but it works: you can run the sh script in powershell using
sh yourShScript
you can put this command in a powershell script file (e.g. yourScript.ps1), then put this powershell script in a cmd script that you can then run at startup. Write this in the cmd file:
PowerShell pathToYourScript\yourscript.ps1
in powershell use:
wsl sh
this starts up the wsl subsystem, not just powershell
Related
This works however I would like to do it only if it is the only .jpg for the given directory, the one below will just rename them all to folder.jpg, overwriting the other files:
find . -type f -name '*.jpg' -execdir mv {} 'folder.jpg' \;
I guess find cannot filter by the number of matches, but you can always exec a shell which does more elaborate checks for you:
find . -type f -name '*.jpg' -execdir sh -c '[ $# = 1 ] && mv "$1" folder.jpg' sh {} +
I'm trying to loop in the SOURCE directory, tar each subfolder to DEST directory without success.
I've tested a few variations, but none is acceptable, so I'm looking for some help.
These are the closest ones.
Thanks
Structure:
/srv/source
sub1
sub2
sub3
I would like to have a tar for each sub as this:
/srv/dest
sub1.{DATE}.tar.gz
sub2.{DATE}.tar.gz
sub3.{DATE}.tar.gz
These where the tests I made:
#!/bin/bash
DATE=$(date '+%Y.%m.%d')
SOURCE=/srv/source
DEST=/srv/dest
find ${SOURCE} -maxdepth 1 -mindepth 1 -type d -printf '%f\n' -execdir tar zcvf ${DEST}/{}-${DATE}.tar.gz -C ${DEST}/{} . \;
#!/bin/bash
DATE=$(date '+%Y.%m.%d')
SOURCE=/srv/source
DEST=/srv/dest
for DIR in "$(find ${SOURCE} -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | xargs -0)"
do
tar zcvf ${DEST}/${DIR}-${DATE}.tar.gz -C ${SOURCE}/${DIR} .
done
EDIT:
This is working
for DIR in ${SOURCE}/*
do
[ -d "${DIR}" ] && tar zcf ${DEST}/$(basename "${DIR}")-${DATE}.tar.gz -C ${SOURCE}/$(basename "${DIR}") .
done
How can I use command substitution in find … -exec … to avoid using xargs in the following command?
find -L -- /path/to/directory -mindepth 2 -maxdepth 2 -type d -exec dirname '{}' \; | xargs basename -a
I tried the following using command substitution, but it output . for each result instead of the desired output:
find -L -- /path/to/directory -mindepth 2 -maxdepth 2 -type d -exec basename "$(dirname '{}')" \;
Your first command will return strange results if a path contains whitespace.
Use a small shell script:
find -L -- . -mindepth 2 -maxdepth 2 -type d -exec sh -c 'basename "$(dirname "{}")"' \;
Alternative syntax to pass one path argument to the script:
find -L -- . -mindepth 2 -maxdepth 2 -type d -exec sh -c 'basename "$(dirname "$1")"' sh {} \;
Or pass as many arguments to the script as possible:
find -L -- . -mindepth 2 -maxdepth 2 -type d -exec sh -c '
for path do
basename "$(dirname "$path")"
done
' sh {} +
With GNU utilities it's possible to output NUL-terminated strings with dirname passed to xargs -0. The basename command is not run if there are no arguments (-r):
find -L -- . -mindepth 2 -maxdepth 2 -type d -exec dirname -z {} + | xargs -r0 basename -a
I have a dozen files named
~/DOMAIN1.de/bin/dbdeploy.php
~/DOMAIN2.de/bin/dbdeploy.php
~/DOMAIN3.de/bin/dbdeploy.php
I want to run them all with the same arguments.
My bash script reads:
cd ~
find . -maxdepth 1 -type d -name "*\.de" -exec php56 bin/dbdeploy.php "$1" "$2" \;
However, the path given to exec seems not to be relative to the found subdirectory but rather to my PWD:
$ bash -x ./.dbpush "some argument"
+ cd ~
+ find . -maxdepth 1 -type d -name '*\.de' -exec php56 bin/dbdeploy.php 'some argument' ';'
Could not open input file: bin/dbdeploy.php
Could not open input file: bin/dbdeploy.php
Could not open input file: bin/dbdeploy.php
How can I use the found path in the -exec directive?
Ok, actually I found the answer myself:
The "find"-results are stored in {}, so the line reads
find . -maxdepth 1 -type d -name "*\.de" -exec php56 {}/bin/dbdeploy.php "$1" "$2" \;
Alternativly
find . -type f -wholename "*\.de/bin/dbdeploy.php" -exec php56 {} "$1" "$2" \;
I want to "join" these two tasks:
for dir in /blabla/bleble/*; do (cd "$dir" && mkdir -p Folder1/Folder1a && mkdir -p Folder2); done
and
find -amin -10
How can I do this?
I've tried this, but it doesn't work:
find -amin -2 -exec sh -c '
for dir in /blabla/bleble/*; do (cd "$dir" && mkdir -p Folder1/Folder1a && mkdir -p Folder2);
done' sh {} +
Something like this is might do:
find /a/b/ -mindepth 1 -maxdepth 1 -type d -amin -2 \
-exec sh -c 'for f; do mkdir -p -- "$f/Folder1/Folder1a" "$f/Folder2; done"' "" {} +
Breakdown:
-mindepth 1 -maxdepth 1 will limit result to current directory only.
-type d makes sure you only list directories
-exec foo "" {} + will execute foo with matches as arguments:
foo "" "/a/b/c" "/a/b/john" "a/b/doe"
for f will iterate over all positional arguments ($1, $2, ...)
for f; do
mkdir -p -- "$f/Folder1/Folder1a" "$f/Folder2"
done
Running sh -c 'code' arg1 arg2 will set $0 to arg1, and $1 to arg2, therefore the empty argument: foo "" {} +:
% sh -c 'echo $0' john
john
Assuming there aren't so many folders in /blabla/bleble that you overflow the command line, you can use find to search the target directory. -prune prevents recursing into the directories.
find /blabla/bleble/* -prune -type d -amin -10 -exec mkdir -p {}/Folder1/Folder1a {}/Folder2 \;
If you are using GNU find or another version that supports them, use -mindepth and -maxdepth instead to find the top-level subdirectories, no matter how many there are.
find /blabla/bleble -maxdepth 1 -mindepth 1 -type d -amin -10 -exec mkdir -p {}/Folder1/Folder1a {}/Folder2 \;