Differences

This shows you the differences between the selected revisions of the page.

custom_commands 2010-12-11 custom_commands 2024-10-08 (current)
Line 3: Line 3:
This is list of [[custom_command|custom commands]] that users of WinSCP found useful. You are encouraged to add your own. Also check the [[ui_pref_commands|built-in examples]]. This is list of [[custom_command|custom commands]] that users of WinSCP found useful. You are encouraged to add your own. Also check the [[ui_pref_commands|built-in examples]].
-===== Backup & Deleting =====+===== [[archiving]] Archiving =====
-==== Backup a File with current Date & Time ====+==== Extensions ====
 +  * [[library_example_zip_and_upload|*]]
 +  * [[extension_archive_and_download|*]]
-==== Rename a File with current Date & Time ====+==== Tar/Gzip ==== 
 +<code custom-command-remote> 
 +tar -xvzf --directory="!?&Extract to which directory:?.!" "!" 
 +</code>
-''mv ! $(echo &quot;!&quot; | sed "s/\..*$//").$(date '+%Y-%m-%d_%H-%M').$(echo ! | awk -F&quot;.&quot; '{ print $NF }')''+&lt;code custom-command-remote&gt; 
 +tar -czvf "!?&amp;Enter an Archive Name:?archive.tgz!" 
 +··--exclude=&quot;!?&amp;Exclude files matching pattern:?*.exe!&quot; 
 +  !&amp; 
 +&lt;/code&gt;
 +==== [[gzip]] Gzip ====
 +<code custom-command-remote>
 +gzip -f "!"
 +</code>
 +<code custom-command-remote>
 +gunzip -f "!"
 +</code>
----- +==== [[zip]] Zip ====
-submitted by [Flak]+
-==== Backup a File with it's Last Modified Date & Time ====+To unpack to the current directory:
-==== Rename a File with it's Last Modified Date &amp; Time ====+&lt;code custom-command-remote&gt; 
 +unzip -o &quot;!" 
 +</code>
-===== Archiving =====+To unpack to an alternate directory:
-==== Tar/Gzip ==== +&lt;code custom-command-remote> 
-··tar -xvzf --directory="!?&Extract to which directory:?.!" "!"+unzip -o -d "!?&Extract to directory:?.!" "!"
 +</code>;
-  tar -czvf "!?&Enter an Archive Name:?archive.tgz!" +To create an archive:
-    --exclude="!?&Exclude files matching pattern:?*.exe!"  +
-    "!?&Specify directory or file to compress:?!"+
-==== Zip ====+&lt;code custom-command-remote> 
 +zip -r "!?&Enter an Archive Name:?archive.zip!" !& 
 +</code&gt;
-··unzip &quot;!&quot;+==== [[rar]] Rar ====
-==== Rar ==== +&lt;code custom-command-remote> 
- +rar a -ep "!?&Enter an Archive Name:?archive.rar!" !&amp; 
-··rar a -ep "!?&Enter an Archive Name:?archive.rar!" &quot;!&amp;&quot;+&lt;/code&gt;
===== [[delete]] Quick Deleting ===== ===== [[delete]] Quick Deleting =====
Line 43: Line 61:
Use following command to quickly delete large directory structures: Use following command to quickly delete large directory structures:
-<code>rm -f -r "!"</code>+<code custom-command-remote> 
 +rm -f -r "!" 
 +</code>
The same command can ask for confirmation before deleting: The same command can ask for confirmation before deleting:
-<code>test "!?Do you really want to delete ?no!" == "yes" && rm -rf !&</code>+<code custom-command-remote> 
 +test "!?Do you really want to delete ?no!" == "yes" && rm -rf !& 
 +</code> 
 + 
 +Both above solutions require a shell access. If you do not have it, use the following solution. 
 + 
 +===== [[delete_background]] Deleting on Background ===== 
 + 
 +Use the following command to delete a large directory structure in a separate process: 
 + 
 +<code custom-command-local> 
 +cmd /C start "" cmd /C ""%WINSCP_PATH%\WinSCP.com" /command "open !E" "cd !/" "rm !&" "exit" & "%WINSCP_PATH%\WinSCP.exe" "!E" /refresh "!/"" 
 +</code> &winpath 
 + 
 +Check custom command options //Local command//, //Apply to directories// and //Use remote files//.
===== [[ownership]] Changing Ownership ===== ===== [[ownership]] Changing Ownership =====
Line 53: Line 87:
Use following command if current [[protocols|protocol]] does not allow changing ownership: Use following command if current [[protocols|protocol]] does not allow changing ownership:
-<code>chown "!?New owner:?!" !&</code>+<code custom-command-remote> 
 +chown "!?New owner:?!" !& 
 +</code>
-To change group use ''chgrp'' instead of ''chown''.+To change group use ''chgrp'' instead of ''chown''. Or use following command:
-===== [[url]] URL Address of Selected File =====+&lt;code custom-command-remote> 
 +chown &quot;!?New owner (and group: user:group):!" !& 
 +</code&gt;
-Example shows how to launch simple PHP script that maps file path to URL.+or with two separate prompts:
-<code>echo '&lt;?=&quot;http://www.example.com". +<code custom-command-remote&gt
-str_replace("home/user/public_html", &quot;~user", "'`pwd`'"). +chown -R "!?New owner:?!":"!?New group:?!" !&amp; 
-&quot;/!\n"?&gt;' | php -q</code>+</code>
-Hints:+===== [[time]] Changing Modification Date/Time =====
-··* Note that the above should be entered as one line! +&lt;code custom-command-remote&gt; 
-  * Do not forget to check custom command option //Show results in terminal// or //Copy results to clipboard//, otherwise you will not get the results. You may also want to use the command for directories, check //Apply to directories// too. +touch -d &quot;!?Date:!&quot; !&amp; 
-··* For your particular use, just replace ''%%http://www.example.com%%'' with right URL prefix, ''home/user/public_html'' with right path prefix and ''~user'' with right URL replacement (it can even be empty in some cases). +&lt;/code&gt;
-··* If you do mapping like above, i.e. for server containing multiple user's homepages, you can make it universal by replacing ''user'' with ''%%'`whoami`'%%''.+
-Another example using shell commands only, in case PHP is unavailable. +===== [[chmod]] Recursively Changing Permissions of Files or Folders only ===== 
- +For changing directory permissions: 
-<code>echo 'http://www.example.com'`pwd`'!' | sed s#home/lgtngstk/public_html#~user#</code>+<code custom-command-remote> 
 +find ! -type d -exec chmod !?&Permissions:?755! {} \; 
 +</code&gt; 
 +For changing file permissions: 
 +&lt;code custom-command-remote> 
 +find ! -type f -exec chmod !?&amp;Permissions:?644! {} \; 
 +</code> 
 +Check custom command options //Remote command// and //Apply to directories//.
===== Templates ===== ===== Templates =====
-··cp /path/to/template.html "!?&New HTML file:?!"+<code custom-command-remote> 
 +cp /path/to/template.html "!?&New HTML file:?!"
 +</code>;
===== [[tail]] Viewing End of a File (log) ===== ===== [[tail]] Viewing End of a File (log) =====
-··tail "!"+==== In Console Window ==== 
 + 
 +<code custom-command-remote> 
 +tail "!"
 +</code>;
You may specify further how much to transfer, e.g. You may specify further how much to transfer, e.g.
-··tail --lines 2000 "!"+<code custom-command-remote> 
 +tail --lines 2000 "!"
 +</code>;
Check custom command option //Show results in terminal//. Check custom command option //Show results in terminal//.
-===== [[search]] Searching for a File =====+==== Continuously in Separate Window ====
-··find . -name "!?Search for files:?!"+&lt;code custom-command-local> 
 +cmd /C start "!@:!/!" "%ProgramFiles%\PuTTY\plink.exe" -P !# !U@!@ tail -f ";!/!" 
 +</code>;
-Check custom command option //Show results in terminal//.+Check custom command options //Local command// and //Use remote files//.
 +===== [[search_file]] Searching for a File =====
 +<code custom-command-remote>
 +find . -name "!?Search for files:?!"
 +</code>
 +
 +Check custom command option //Show results in terminal//.
===== [[search_text]] Searching for a text/string within a directory ===== ===== [[search_text]] Searching for a text/string within a directory =====
-··grep -H -r "!?Search for text:?!" *  | cut -d: -f1+<code custom-command-remote> 
 +grep -H -r "!?Search for text:?!" *  | cut -d: -f1 
 +</code>
Check custom command option //Show results in terminal//. Check custom command option //Show results in terminal//.
 +
 +===== Backup =====
 +
 +==== Backup a File with Current Date and Time ====
 +<code custom-command-remote>
 +cp ! $(echo "!" | sed "s/\.[^.]*$//").$(date '+%Y-%m-%d_%H-%M').$(echo ! | awk -F"." '{ print $NF }')
 +</code>
 +
 +==== Rename a File with Current Date and Time ====
 +<code custom-command-remote>
 +mv ! $(echo "!" | sed "s/\.[^.]*$//").$(date '+%Y-%m-%d_%H-%M').$(echo ! | awk -F"." '{ print $NF }')
 +</code>
 +
 +===== [[fxp]] Remote Transfer (FXP) =====
 +
 +Use following remote custom command to transfer selected remote files to an another server:
 +
 +<code custom-command-remote>
 +scp -p -r !& !?Username:?!@!?Host:?!:!?Destination path:?!
 +</code>
 +
 +You may want to hard-code the //Username// and //Host//, if you work with one destination server only. Or you can use a single prompt for both (possibly even for //Destination Path//) to keep the flexibility, yet to reduce number of prompts:
 +
 +<code custom-command-remote>
 +scp -p -r !& !?Username@Host:?!:!?Destination path:?!
 +</code>
 +
 +Note that you need to ensure you can connect and authenticate to the destination server without any prompts:
 +  * Connect at least once from an interactive terminal (such as [[&url(putty)|PuTTY SSH client]]) to verify the destination server host key.
 +  * Use for example [[ui_login_authentication#forwarding|Agent forwarding]] to forward your private key to the destination server for authentication.
 +  * Another less-secure method of authentication is use of ''[[https://sourceforge.net/projects/sshpass/|sshpass]]'' tool (see its [[https://www.mankier.com/1/sshpass|man page]]).
 +
 +===== [[compare]] File Compare =====
 +
 +Note that WinMerge and other major diff tools are supported by official [[extension_compare_files|*]]. The following is just an example how to use other diff tools.
 +
 +==== WinMerge ====
 +
 +Select //Local command// [[custom_command#types|type]].
 +
 +<code custom-command-local>
 +"C:\Program Files (x86)\WinMerge\WinMergeU.exe" /e /x /u "!" "!^!"
 +</code>
 +
 +Replace ''C:\Program Files (x86)'' with ''C:\Program Files'' on 32-bit system. &winpath &win32 &win64
 +
 +Arguments:
 +
 +  * ''/e'' -- Enables you to close WinMerge with a single ''Esc'' key press;
 +  * ''/x'' -- Closes WinMerge (after displaying an information dialog) when you start a comparison of identical files;
 +  * ''/u'' -- Prevents WinMerge from adding either path (left or right) to the Most Recently Used (MRU) list.
 +
 +===== [[errors]] Suppressing Errors =====
 +
 +WinSCP will display error message, when the custom command returns exit code different than 0 or 1; or prints a message on error output, but no normal output.
 +
 +To suppress the error message:
 +
 +  * Redirect error output to ''/dev/null'', to discard it: \\ <code>
 +command_that_prints_non_interesting_progress_on_error_output > /dev/null</code>
 +  * Redirect error output to standard output, to see it in terminal, but avoid error message: \\ <code custom-command-remote>
 +command_that_prints_interesting_info_on_error_output 2>&1</code>
 +  * Use ''|| true'' to discard exit code: \\ <code custom-command-remote>
 +command_that_returns_255_code || true</code>
 +  * Or combine these together: <code custom-command-remote>
 +command_that_prints_interesting_info_on_error_output_and_returns_255_code 2>&1 || true</code>

Last modified: by flak