Copy and delete files from SFTP folder - sftp

I have to pick (remove) the files with file mask FileName_A_* and FileName_B_* from SFTP location and place them in an sharedrive.
I tried using WinSCP. I have created an HourlyFile.txt file with below code and placed it under C:\Program Files (x86)\WinSCP . Another batch file HourlyFile.bat to execute the script from HourlyFile.txt
HourlyFile.txt:
option batch abort
option confirm off
open sftp..........
get -filemask="FileName_A_*" /outbound/test/* \\sharedrive
get -filemask="FileName_B_*" /outbound/test/* \\sharedrive
del /outbound/test/FileName_A_*
del /outbound/test/FileName_B_*
exit
HourlyFile.bat:
winscp.com /script=HourlyFile.txt
pause
I tried with below options to delete the file but got the error message "Unknown command". Also the above code is copying subfolder from /outbound/test/ , which it should not.
Commands tried:
del /outbound/test/FileName_A_*
-del /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_20190604_090002
delete /outbound/test/FileName_A_20190604_090002.csv

If you want to download and delete the files, you better use -delete switch of the get command. This way, you can be sure that WinSCP deletes only those files that were really successfully downloaded.
get -delete /outbound/test/FileName_A_* \\sharedrive\
get -delete /outbound/test/FileName_B_* \\sharedrive\
See WinSCP article How do I create script that synchronizes files and deletes synchronized files from source afterward?
To answer your literal question: WinSCP has no del command. WinSCP has rm command:
rm /outbound/test/FileName_A_*
rm /outbound/test/FileName_B_*

On some Unix servers the wildcard command would be:
mrm /outbound/test/FileName_A_*
, in the event
rm /outbound/test/FileName_A_*
doesn't work, returning error:
rm: Access failed: File not found (/outbound/test/FileName_A_*)

Related

Run jq command in git-bash

jq command not found after adding jq executable
installing jq on git bash
My usecase is more similar with above shared references. I tried to execute a hook that needs to parse a json file. When hook gets executed it throws bash: jq:command not found error. So. I downloaded jq-win64.exe file and copied it to /usr/bin in Git folder. Then from git-bash I run export PATH=$PATH:"/C/Program Files/Git/usr/bin/jq-win64.exe" command and there is no error but when I checked jq --version command it still shows bash: jq:command not found error
Am I missing something? I even tried in windows cmd but is of no use. Hope someone can help me.
Thanks in advance!!!
PATH contains directories. That means what you should do:
Rename jq-win64.exe to jq.exe or just jq. (e.g. cp ~/Downloads/jq-win64.exe /usr/bin/jq).
You don't have to export your path, /usr/bin is already part of it.
If you didn't rename the file to jq (or jq.exe), then you would have to run it as jq-win64 in your console.
You could also put the binary into ~/bin folder, which should be part of PATH too. If it isn't, you can add it. Then you don't need to mess with your global binaries folder.

How do i delete a file in remote location

I have delete 1 file locally and added new files locally, i dont want to sync those new files to the remote system but rather delete only the one that has been removed from src.I tried the command below but it deletes file from remote also.
rsync -avh --delete /home/ssastry/ ssastry#10.196.105.121:/tmp
I want the only file deleted from local to get deleted from remote . i dont want new additions.
Try below command.
rsync -r --delete --existing --ignore-existing /home/ssastry/ ssastry#10.196.105.121:/tmp
From man page:
--existing :skip creating new files on receiver
--ignore-existing : skip updating files that exist on receiver

Rsync wont delete files from an external drive

Can someone please tell me why the following command does not delete the extra files on backup2Drive.
Backup1Folder is a folder and backup2Drive is a drive.
rsync -avh --exclude '._*' --delete /Volumes/ext/Backup1Folder/ /Volumes/backup2Drive
you are either missing --delete-excluded or you are skipping the delete step because of previous i/o errors

Added Alias to .bashrc but no results

I added an alias (alias homedir='cd /export/home/file/myNmae'
) to .bashrc in my home directory and restarted the session. When I run the alias it says homedir: command not found.
Please advice.
This is because .bashrc is not sourced everytime, only for interactive non login shells .bashrc is sourced.
From the bash man page.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/pro-
file, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the
first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the
--norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
i found the solution - i added it to the .profile file and restarted the session - it worked

Permissions when iterating over files in directory Unix TCSH scripting

I'm writing a script that will print the file names of every file in a subdirectory of my home directory. My code is:
foreach file (`~/.garbage`)
echo "$file"
end
When I try to run my script, I get the following error:
home/.garbage: Permission denied.
I've tried setting permissions to 755 for the .garbage directory and my script, but I can't get over this error. Is there something I'm doing incorrectly? It's a tcsh script.
Why not just use ls ~/.garbage
or if you want each file on a separate line, ls -1 ~/.garbage
backtic will try to execute whatever is inside them. You are getting this error since you are giving a directory name within backtic.
You can use ls ~/.garbage in backtics as mentioned by Mark or use ~/.garbage/* in quotes and rely on the shell to expand the glob for you. If you want to get only the filename from a full path; use the basename command or some sed/awk magic

Resources