This is an old revision of the document!

Useful Custom Commands

This is list of custom commands that users of WinSCP found useful. You are encouraged to add your own. Also check the built-in examples.

Advertisement

Advertisement

Archiving

Tar/Gzip

tar -xvzf --directory="!?&Extract to which directory:?.!" "!"
tar -czvf "!?&Enter an Archive Name:?archive.tgz!"
  --exclude="!?&Exclude files matching pattern:?*.exe!"
  !&

Zip

To unpack to the current directory:

unzip "!"

To unpack to an alternate directory:

unzip -d "!?&Extract to directory:?.!" "!"

To create an archive:

zip -r "!?&Enter an Archive Name:?archive.zip!" !&

Rar

rar a -ep "!?&Enter an Archive Name:?archive.rar!" !&

Quick Deleting

Use following command to quickly delete large directory structures:

rm -f -r "!"

Advertisement

The same command can ask for confirmation before deleting:

test "!?Do you really want to delete ?no!" == "yes" && rm -rf !&

Deleting on Background

Use the following command to delete a large directory structure in a separate process:

cmd.exe /c start "" cmd /c ""C:\Program Files (x86)\WinSCP\WinSCP.com" /command "open !S" "cd !/" "rm !&" "exit" & "C:\Program Files (x86)\WinSCP\WinSCP.exe" "!S" /refresh "!/""

Replace C:\Program Files (x86) with C:\Program Files on 32-bit systems.

Check custom command options Local command, Apply to directories and Use remote files.

Changing Ownership

Use following command if current protocol does not allow changing ownership:

chown "!?New owner:?!" !&

To change group use chgrp instead of chown. Or use following command:

chown "!?New owner (and group: user:group):!" !&

or with two separate prompts:

chown -R "!?New owner:?!":"!?New group:?!" !&

URL Address of Selected File

Using Local Command

Example shows how to use local custom command that maps file path to URL.

!`cmd.exe /c echo @echo off > %TEMP%\url.bat & 
echo set FILE_PATH=%1 >> %TEMP%\url.bat & 
echo echo https://www.example.com%FILE_PATH:home/user/public_html=~user% >> %TEMP%\url.bat`
cmd.exe /c %TEMP%\url.bat !/! & pause

Advertisement

Hints:

  • Note that the above should be entered as one line!
  • Check custom command options Local command and Use remote files.
  • If you want to copy the URL to clipboard instead of displaying it, replace the trailing & pause with | clip.
  • For your particular use, just replace https://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).

Using Remote PHP Code

Example shows how to launch simple PHP script that maps file path to URL.

echo '<?="https://www.example.com".str_replace("home/user/public_html", "~user", "'`pwd`'")."/!\n"?>' | php -q

Hints:

  • Note that the above should be entered as one line!
  • 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.
  • For your particular use, just replace https://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).
  • 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`'.

Using Remote Shell Code

Another example using shell commands only, in case PHP is not available:

echo 'https://www.example.com'`pwd`'/!' | sed s#home/user/public_html#~user#

Templates

cp /path/to/template.html "!?&New HTML file:?!"

Viewing End of a File (log)

In Console Window

tail "!"

Advertisement

You may specify further how much to transfer, e.g.

tail --lines 2000 "!"

Check custom command option Show results in terminal.

Continuously in Separate Window

cmd /C start "!@:!/!" "%ProgramFiles%\PuTTY\plink.exe" -P !# !U@!@ tail -f "!/!"  

Check custom command options Local command and Use remote files.

Searching for a File

find . -name "!?Search for files:?!"

Check custom command option Show results in terminal.

Searching for a text/string within a directory

grep -H -r "!?Search for text:?!" *  | cut -d: -f1

Check custom command option Show results in terminal.

Backup

Backup a File with Current Date and Time

cp ! $(echo "!" | sed "s/\..*$//").$(date '+%Y-%m-%d_%H-%M').$(echo ! | awk -F"." '{ print $NF }')

Rename a File with Current Date and Time

mv ! $(echo "!" | sed "s/\..*$//").$(date '+%Y-%m-%d_%H-%M').$(echo ! | awk -F"." '{ print $NF }')

Advertisement

Remote Transfer (FXP)

Use following remote custom command to transfer selected remote files to an another server:

scp -p -r !& !?Username:?!@!?Host:?!:!?Destination path:?!

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:

scp -p -r !& !?Username@Host:?!:!?Destination path:?!

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 PuTTY SSH client) to verify the destination server host key.
  • Use for example Agent forwarding to forward your private key to the destination server for authentication.
  • Another less-secure method of authentication is use of sshpass tool (see its man page).

File Compare

WinMerge

Select Local command type.

"C:\Program Files (x86)\WinMerge\WinMergeU.exe" /e /x /u "!" "!^!"

Replace C:\Program Files (x86) with C:\Program Files on 32-bit system.

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.

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:
    command_that_prints_non_interesting_progress_on_error_output > /dev/null
  • Redirect error output to standard output, to see it in terminal, but avoid error message:
    command_that_prints_interesting_info_on_error_output 2>&1
  • Use || true to discard exit code:
    command_that_returns_255_code || true
  • Or combine these together:
    command_that_prints_interesting_info_on_error_output_and_returns_255_code 2>&1 || true

Advertisement

Last modified: by 155.55.60.110