After playing around in the terminal I found that my desktop directory had a file named "~$Löp.docx" that wasn't visible outside the terminal. The file seems to be some kind of crash log for a (now deleted) word file called "Löp". I would like to remove the file and have thus far failed to enter a rm command that works.
When trying to echo the filename as
echo "~$Löp.docx"
I get the output
~?p.docx
How can I retain the correct name for a rm statement?
Just Quote it, here's an example:
rm '~$some file name.docx'
Related
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}"
I need to create a script to move all the files with certain extension from root of USB1 to specificFolder.
So far I have created a move.command file with this code
#!/bin/bash
mv /VOLUMES/USB1/*.dat /VOLUMES/USB1/specificFolder
and I am setting x bit on the file to make it executable with
chmod +x move.command
When I double click the file to launch, terminal opens up and this message is displayed
/Volumes/USB1/move.command ; exit;
logout
[Process completed]
But files are not moved over.
What am I missing?
In case somebody needs this: I solved the problem by using .sh file instead of .command file and just using this piece of code in the file
mv /VOLUMES/USB1/*.dat /VOLUMES/USB1/specificFolder
First line with /bin/bash was not needed.
Also note that the file extension is case sensitive, this command only moved .dat files and not .DAT files.
I am new to bash scripting and I have to create a script that will run on all computers within my group at work (so it's not just checking one computer). We have a spreadsheet that keeps certain file information, and I am working to automate the updating of that spreadsheet. I already have an existing python script that gathers the information needed and writes to the spreadsheet.
What I need is a bash script (cron job, maybe?) that is activated anytime a user deletes a file that matches a certain extension within the specified file path. The script should hold on to the file name before it is completely deleted. I don't need any other information besides the name.
Does anyone have any suggestions for where I should begin with this? I've searched a bit but not found anything useful yet.
It would be something like:
for folders and files in path:
if file ends in .txt and is being deleted:
save file name
To save the name of every file .txt deleted in some directory path or any of its subdirectories, run:
inotifywait -m -e delete --format "%w%f" -r "path" 2>stderr.log | grep '\.txt$' >>logfile
Explanation:
-m tells inotifywait to keep running. The default is to exit after the first event
-e delete tells inotifywait to only report on file delete events.
--format "%w%f" tells inotifywait to print only the name of the deleted file
path is the target directory to watch.
-r tells inotifywait to monitor subdirectories of path recursively.
2>stderr.log tells the shell to save stderr output to a file named stderr.log. As long as things are working properly, you may ignore this file.
>>logfile tells the shell to redirect all output to the file logfile. If you leave this part off, output will be directed to stdout and you can watch in real time as files are deleted.
grep '\.txt$' limits the output to files with .txt extensions.
Mac OSX
Similar programs are available for OSX. See "Is there a command like “watch” or “inotifywait” on the Mac?".
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!
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.