Quick Scripts
Deleting duplicate files with parenthesis number
CMD Windows 7+
for /f "delims=|" %f in ('dir /B "C:\dir\to\duplicate\files\*(1).png"') do del "C:\dir\to\duplicate\files\%f"
GNU Bash 4
...
Removing a tracker from each file in a directory
CMD Windows 7+
For Windows, I found using the Transmission CLI tool transmission-edit to be the easiest.
I also prefer this for-loop method as it provides the full path to the torrent within the loop.
set loc=C:\path\to\torrents && for /r %loc% %f in (*) do "C:\path\to\transmission-edit.exe" -d "http://www.example.org/announce.php" "%~dpnxf"
GNU Bash 4
...
Executing a command for each file in a directory
CMD Windows 7+
This method will ensure %~dpnxf is the full path of the file.
set loc=C:\path\to\files && for /r %loc% %f in (*) do echo "%~dpnxf"
GNU Bash 4
...
Recursively executing a command for each file in a directory
CMD Windows 7+
for /R "C:\path\to\directory\of\directories" %i in (*.bin) do echo "%i"
GNU Bash 4
...
Copy directory contents to another directory
CMD Windows 7+
...
GNU Bash 4
For this to work, the /. at the end of the source directory is required.
cp -r "/path/to/source/dir/." "/path/to/dir"
Hardlink directory contents to another directory
CMD Windows 7+
...
GNU Bash 4
For this to work, the /. at the end of the source directory is required.
cp -lr "/path/to/source/dir/." "/path/to/dir"
Unrar all .rar and .r files in multiple subdirectories
CMD Windows 7+
...
GNU Bash 4
For this version to work you must be in the directory (eg. cd) with the subdirectories. Extracted data will be stored in the directory the terminal is within.
unrar e -r -o- *.rar
Remove first N-characters from string
CMD Windows 7+
...
GNU Bash 4
The string variable must be encapsulated in "${}" and separated by a colon with a valid character index, for this example 5 . The double quotes tell Bash to process with built-in functions (ie. interpolate) compared to how single quotes force literal character interpretation. Lastly, the dollar sign and curly brackets is a disambiguation mechanism, telling Bash this particular text is processed a variable within the context of the built-in function.
STR="This is a string."
REMOVED_FIRST_FOUR_CHARS="${STR:5}"
echo "$REMOVED_FIRST_FOUR_CHARS"
>is a string.
Remove last N-characters from string
CMD Windows 7+
...
GNU Bash 4
The string variable must be encapsulated in "${}" and separated by a colon with a valid character index, for this example -4 . The double quotes tell Bash to process with built-in functions (ie. interpolate) compared to how single quotes force literal character interpretation. Lastly, the dollar sign and curly brackets is a disambiguation mechanism, telling Bash this particular text is processed a variable within the context of the built-in function.
STR="This is a string."
REMOVED_FIRST_FOUR_CHARS="${STR::-4}"
echo "$REMOVED_FIRST_FOUR_CHARS"
>This is a st
Merge multiple video files into an single contiguous output with FFmpeg
CMD Windows 7+
Should be similar to the GNU Bash 4 version below...
GNU Bash 4
For this script, place the full path to each video after concat: and separated by the pipe symbol | . The output video file will follow the sequence of input videos supplied: video1, video2, video3. If you need a different sequence, simply change the order of the input files. The output file will not be re-encoded but may be lossy if you use MP4 with H.264 and AAC.
ffmpeg -i "concat:/path/to/video1.mp4|/path/to/video2.mp4|/path/to/video3.mp4" -c copy /path/to/merged_output.mp4
To concatenate MP4 with H.264 and AAC losslessly, the FFmpeg documentation recommends transcoding them into MPEG-2 transport streams (.ts) first. The output .ts files can be used in the command above.
ffmpeg -i /path/to/video1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts /path/to/intermediate_output1.ts
Convert .AVI to .MKV with FFmpeg / Generate timestamps with FFmpeg
CMD Windows 7+
Should be similar to the GNU Bash 4 version below...
GNU Bash 4
.AVI files do not come with internal timestamps for .MKV to use, adding -fflags +genpts right after ffmpeg tells FFmpeg to generate those missing timestamps.
ffmpeg -fflags +genpts -i "/path/to/avi_video.avi" -c copy /path/to/merged_output.mkv