Copy file without file extension to new folder and rename it - shell

I just during the weekend decided to try out zsh and have a bit of fun with it. Unfortunately I'm an incredible newbie to shell scripting in general.
I have this folder with a file, which filename is a hash (4667e85581f80b6936f8811f0a7493c70eae4ee7) without a file-extension.
What I would like to do is copy this file to another folder and rename it to "screensaver.png".
I've tried with the following code:
#!/usr/bin/zsh
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva"
DEST_FOLDER="/Library/Desktop Pictures/Kuvva/$USERNAME/screensaver.png"
for wallpaper in ${KUVVA_CACHE}; do
cp -f ${wallpaper} ${DEST_FOLDER}
done
This returns the following error:
cp: /Users/Morten/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva is a directory (not copied).
And when I try to echo the $wallpaper variable instead of doing "cp" then it just echo's the folder path.
The name of the file changes every 6 hour, which is why I'm doing the for-loop. So I never know what the name of the file will be, but I know that there's always only ONE file in the folder.
Any ideas how I can manage to do this? :)
Thanks a lot!
Morten

It should work with regular filename expansion (globbing).
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva/"
And then copy
cp -f ${KUVVA_CACHE}/* ${DEST_FOLDER}
You can add the script to your crontab so it will be run at a certain interval. Edit it using 'crontab -e' and add
30 */3 * * * /location/of/your/script
This will run it every third hour. First digit is minutes. Star indicates any. Exit the editor by pressing the escape-key, then shift+: and type wq and press enter. These vi-commands.
Don't forget to 'chmod 0755 file-name' the script so it becomes executable.
Here is the script.
#!/bin/zsh
KUVVA_CACHE="$HOME/Library/Containers/com.kuvva.Kuvva-Wallpapers/Data/Library/Application Support/Kuvva"
DEST_FOLDER="/Library/Desktop Pictures/Kuvva/$USERNAME/screensaver.png"
cp "${KUVVA_CACHE}/"* "${DEST_FOLDER}"

Related

Script for copying a folder somewhere using Git Bash

I want to copy a folder ~/Projects/LocalProject onto my server //VM-Server/ServerProject.
I know that I can use GitBash:
cp -r directory-name-1 directory-name-2
But what I'm curious about is, can I create a script to do that by double clicking that script, or adding it as a command to my GitBash, cause I will need that alot?
--Edit--
Tried nothing, as I don't know how to do that. Yes there are hidden files, I don't want them to be copied. There shouldn't be newer files on the destination. I need to manually run it, I thought that's clear as I mentioned the option to have a executable script / or a terminal command.
Option 1: Batch file
You don't even need git-bash; you can make a batch file in any text editor, name it copy to server.bat, and type in cp C:\Users\<Your username>\Projects\LocalProject \\VM-Server\ServerProject.
You can also make a .sh file for use in bash. The command is the same, just make note that Windows uses \, while bash uses / for directory tree
Option 2: Alias
Open your bash_profile file (it's in your git bash install location).
Add a line at the end of the file that says alias copyToServer = 'cp ~/Projects/LocalProject //VM-Server/ServerProject'. Then close git-bash, reopen it and use the command by typing copyToServer as a bash command. (It doesn't need to be named copyToServer)

Running an executable by calling it in a .sh file

I am very new to bash and using .sh files. I am trying to run the program aescrypt by calling it in a .sh file as follows (aescrypt is in the same directory as the .sh file) :
./aescrypt -e -p password file.txt
It throws the following error:
./aescrypt no such file or directory
Am I doing it wrong?
ps- I realy don't want to add it to the PATH variable as I will be using this on more than one computer that resets every day.
The location of the script is irrelevant. The thing that matters is the working directory of the process executing the script. The simplest solution really is to add aescrypt to a standard location like /bin or /usr/bin. If neither of those is acceptable, perhaps /usr/local/bin is an option. Otherwise, just use the full path of aescrypt in your script. Either hard code it, or if it is in the same directory as the script, try:
$(dirname $0)/aescrypt ...
(Note that hardcoding is more reliable, but less flexible. If you move the executable, the script will break. But using dirname will break if the script changes directory during execution.)
how about if you call the program like ./aescrypt.sh, thats the way to call an .sh programm througt the terminal
First off all, you have also to change the permissions of the file to make it executable, the way to make that is to write in the terminal, the command:
sudo chmod 765 aescrypt.sh
For that you have to be located where the file is

Cygwin: cygstart * Not Opening All Files

One thing that I really don't like with Cygwin is when I have to open files one by one. I set up an alias of opening files (instead of cygstart, I use open). However, everytime I want to open up multiple files, say a pdf, it will only open the first file in the directory. Here is what I type:
open *
open *.pdf
cygstart *
cygstart *.pdf
None of these work. However when I do something like mv * or cp * it works. Any help with this would be greatly appreciated! Thanks in advance.
* will expand to all files in the current directory.
For example:
$ ls
1.pdf 2.pdf 3.pdf
In this case * will expand to 1.pdf 2.pdf 3.pdf. If you give this as a parameter to an other command, then it will see 3 separate parameter. Many program can handle this, but apparently open and cygstart can handle only the first parameter.
To solve this problem this script should be added to a directory which is found in your PATH environment variable (e.g: c:\cygwin\bin):
myopen.sh:
for i in "$#"; do
cygstart "$i"
done
After that it can be called like this:
myopen.sh *.pdf

Need bash script to watch Directories and perform actions on new files and modified files

I want to watch mydirectory/
I created inot.sh and ran it in the background as # ./inot.sh &
Here's the first version of the script inot.sh that I tried:
#!/bin/bash
inotifywait -m -e create,modify --exclude '\*.swp?$' . |
while read dir ev file; do
cp "$file" inotfiles/"$file"
done
Note: the exclude pattern is supposed to exclude vim swap files but doesn't seem to be working yet.
If any file is created (or changed), I want the whole file copied to a storage folder. For now I'm focusing on create,modify just to see if I can get anything at all to work correctly.
At this point, FTP uploads seem to work correctly. Using the command line to touch a file copies the empty file. But then using vim to edit it results in a bunch of problems.
I edited an existing file in vim and did not change it. When I entered the file, I get:
cp: cannot stat `.zzzzzoo.txt.swx': No such file or directory
~
~
~
~
~
It looks like the first line is part of the file, but it's not. It's inotify interfering with the vim viewer. Upon exit, everything looks normal.
But sometimes I get that same cp error message on exit from vim.
If I edit the file and exit with write to file, I get:
Upon exit from vim, I get:
~
~
~
~
~
~
"zzzzzoo.txt" cp: cannot stat `4913': No such file or directory
"zzzzzoo.txt" 1L, 11C written
[root#server mydirectory]# cp: cannot stat `.zzzzzoo.txt.swp': No such file or direc
tory
1st part with tildes is from vim.
"somefile.txt" cp: cannot stat `4913': No such file or directory
"somefile.txt" 7L, 129C written
EDIT: adding 1 more error I forget to mention
When I open the copied file in vim, I get a notice that vim found a swap file for the same file name.
E325: ATTENTION
Found a swap file by the name "inotfiles/.somefile.txt.swp"
etc, etc.
Swap file "inotfiles/.somefile.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
// End edited section
So I know I might be able to fix this problem by properly excluding vim swap files, but it seems to me that the larger question is "Why are cp errors going to stdout and how to stop them?" I should pipe them somewhere else probably.
Also I need a way to prevent an infinite while loop. Is there any other way to write that section?
Instead of asking inotifywait to watch out for vim's swap files, why not get vim to
put it's temporary files in a better (less intrusive) location:
For example, how about:
mkdir -p ~/tmp/.vim
And then in your .vimrc
set backupdir^=~/tmp/.vim/
set directory^=~/tmp/.vim/
http://vim.wikia.com/wiki/Remove_swap_and_backup_files_from_your_working_directory
Think about it: What if you do get your regexes to exclude swap files - and then an emacs user joins you and wants to use emacs to edit files in the same directory - you'll have a mess of regexes before you know it!

Bash script for taking a screenshot, renaming and moving

First bash script and I'm running into some issues. I want to take a screenshot, then change the name of the .png to a random number (so that pictures don't overwrite). After it's renamed I want to move the picture to my dropbox folder.
This is what I've got:
#!/bin/bash
#Take screenshot
import -window root $HOME/screenshot.png
#Move to dropbox folder
mv $HOME/screenshot.png $HOME/Dropbox/Max-Max/$RANDOM.png
When I run it dropbox is getting some kind of something because my taskbar icon indicates a file transfer. When I open up the folder however, nothing's there.
Thanks for the help.
Instead of $RANDOM use $(date|tr " :" _)
Much more useful
You can do that with scrot like this:
scrot -e 'mv $f ~/Dropbox/Max-Max'
But your script looks fine... Try to create an empty file first to make sure your dropbox functions fine.
echo > ~/Dropbox/Max-Max/testfile
The commands you're using are correct. The only way it could fail is if Max-Max doesn't exist. mv moves and renames files among existing directories -- mv cannot create directories.

Resources