File delete action if condition exists - chef-recipe

New to chef recipe and google is not showing me an example of what I need to do.
I have a file that I want deleted when chef finds it exists.
I am not finding any google examples of something like
file /path/foo do
action delete
end if /path/foo exists
and the chef file documentation is not showing anything like
file /path/foo do
condition exists
action delete
end
Is the only way to use a script like bash?
bash 'delete_foo' do
if [ -f /path/foo ] then
/bin/rm /path/foo
fi
end

file '/path/foo' do
action :delete
only_if { File.exist? '/path/foo' }
end

Related

Windows (All) how to append date to end of new folder creation?

I'm trying to figure out how to create a new folder that has the date, not time, appended to the end of the directory name. I just need the current time of creation, and nothing more.
Trying to use something really basic like the following as an example...
if exists CNC_%date% goto EXIST
if not exists CNC_%date% goto CREATE
:CREATE
mkdir CNC_%date%
:EXIST
echo Folder already exists!
echo Check directory and rename it to prevent loss of data.
echo.
echo Press any key to exit.
pause >nul
goto END
:CREATE
echo Creation successful!
echo Press any key to exit.
pause >nul
:END
exit
... results in the creation of a nested directory like "C:\"CNC_Fri 11"\22\2013" because of the backslashes.
IS there any way to pipe the backslashes through a native Windows program, and switch them with underscores? In Linux grep would have been my answer, but I need a native Windows method as this needs to be portable.
mkdir cnc_%date:/=_%
Use the date variable with the slashs replaced with underscore

using wget to overwrite file but use temporary filename until full file is received, then rename

I'm using wget in a cron job to fetch a .jpg file into a web server folder once per minute (with same filename each time, overwriting). This folder is "live" in that the web server also serves that image from there. However if someone web-browses to that page during the time the image is being fetched, it is considered a jpg with errors and says so in the browser. So what I need to do is, similar to when Firefox is downloading a file, wget should write to a temporary file, either in /var or in the destination folder but with a temporary name, until it has the whole thing, then rename in an atomic (or at least negligible-duration) step.
I've read the wget man page and there doesn't seem to be a command line option for this. Have I missed it? Or do I need to do two commands in my cron job, a wget and a move?
There is no way to do this purely with GNU Wget.
wget's job is to download files and it does that. A simple one line script can achieve what you're looking for:
$ wget -O myfile.jpg.tmp example.com/myfile.jpg && mv myfile.jpg{.tmp,}
Since mv is atomic, atleast on Linux, you get the atomic update of a ready file.
Just wanted to share my solution:
alias wget='func(){ (wget --tries=0 --retry-connrefused --timeout=30 -O download_pkg.tmp "$1" && mv download_pkg.tmp "${1##*/}") || rm download_pkg.tmp; unset -f func; }; func
it creates a function that receives a parameter "url" to download the file to a temporary name. If it is successful, it is renamed to the correct filename extracted from parameter $1 with ${1##*/}. and if it fails, deletes the temp file. If the operation is aborted, the temp file will be replace on the next run. after all, unset -f removes the function definition as the alias is executed.

rake directory command silently fails?

I'm a newbie to rake so there's probably a simple explanation. I wanted to create some directories and copy in some files for a simple install script e.g.
task :default => ['mktd1', 'mktd2' ] do
end
task :mktd1 do
mkdir "testdata"
cp "x.tmp", "testdata/x.tmp"
end
task :mktd2 do
directory "testdata1"
cp "x.tmp", "testdata1/x.tmp"
end
mkdir works as long as the testdata directory doesnt already exist, but "directory" silently fails (i.e. does nothing) leading to a rake abort because the directory isn't there for the cp command.
Have I misunderstood what directory directive is supposed to do?
So the answer was I had misunderstood how rake is supposed to work. To achieve what I wanted I needed to declare a task that had a dependency on the testdata1 directory. e.g.
task :default => [ 'testdata1/x.tmp' ] do
end
directory "testdata1"
file "testdata1/x.tmp" => ["testdata1"] do
cp "x.tmp", "testdata1/x.tmp"
end
This of course creates a file_creation task x.tmp which depends on the testdata1 directory, and a default task that depends on the x.tmp file creation task. I feel dumb.

Linux recycle bin script

I'm creating a recycle-bin script in SH shell linux in three differant scripts, delete, trash and restore.
The first two scripts are working fine; 'Delete' moves the selected file to the recycle-bin while logging a text file called 'trashinfo' which shows the original path location of the file (to be later used in restore) and 'Trash' which removes everything in the recycle-bin.
The 'restore' script should take the logged path name gained in the delete script and return the file to its original location. I've spent more time than I'd like to remember on this and cant get the restore script to work properly!
Below is the script I've written, as far as I can make out I'm grepping for the filename variable in the text file that holds the pathname, eg 'restore testfile', this is then combined with the basename command, the testfile is then moved into the location thats been grepped and combined with the basename.
Anyone have any pointers on where I'm going wrong?
if [ "$*" != -f ]
then
path=grep "$*" /usr/local/bin/trashinfo
pathname=basename "$path"
mv "$path" "$pathname"
path=$(grep "$*" /usr/local/bin/trashinfo)
pathname=$(basename "$path")

I want to prompt a user for the first 5 characters of a file then have it search for the files

I'm trying to write a script that will prompt the user for the first 5 charters of a file name then have it search the directories for any files that start with that. Then I want it to check to see if a folder is created with the file names and if not create one then move the files there. But if there is a directory for it already then to just move the files too the folder.
Break it down step by step:
"prompt the user for the first 5 characters of a file name" -- you can use the shell read command to get the data. Try a simple shell script:
#!/bin/bash
read foo
echo "foo = $foo"
"if a folder is created with the file names" -- you can use find to see if a file exists. for example:
find . -name abcde\*
"But if there is a directory for it already then to just move the files too the folder." -- the command mkdir takes a -p option so that, if the directory already exists, it won't do anything.