Showing posts with label Cygwin. Show all posts
Showing posts with label Cygwin. Show all posts

Friday, December 27, 2019

Trim characters from the end of all directory names in a folder

On my windows system, I had a folder with only other folders in it, and all of them had three characters on the ends of their names that I wanted to delete. I started up a Cygwin Bash shell to do the job.

I first attempted to use 'find' to create a one line command to rename all of these folders, but it turns out bash for and do were the answer, thusly:
for file in *; do mv "$file" "${file%???}"; done

The syntax for doing the trimming came from this thread (although sadly the pythonesqe method of trimming with colons and a negative end position didn't work in cygwin bash for some reason): https://stackoverflow.com/questions/19530429/rename-file-by-removing-last-n-characters
The example of using for/do came from here: https://stackoverflow.com/questions/7450818/rename-all-files-in-directory-from-filename-h-to-filename-half

Saturday, October 6, 2012

The incredible batch call of wkhtmltopdf

Like extra credit, this command line combined nearly everything I've done recently all at once. Within cygwin, I'm using find on a directory with the -exec option, and calling wkhtmltopdf on every file found. The only clunky thing about this method is that it makes .htm.pdf files which I have to rename to .pdf.

find . -name "D*.htm" -exec cmd /C "C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe" -s Letter {} {}.pdf \;

The awesome trick was doing this in cygwin. For some reason, wkhtmltopdf wasn't running in cygwin naitive mode (it is a GnuWin utility in the form that I have it in), so I found from the below blog how to use cmd /C and I stuck the whole thing into the -exec of find and it somehow worked.

Yes, well, the downside is that I have to trim an extra .htm out of the pdf filename, I suppose that a real unix master would run the result of find through something else to trim the orignal suffix off before passing it into cmd /C, I should figure that out some day.

http://siddesh-bg.blogspot.com/2012/07/how-to-run-dos-commands-from-cygwin.html

Wednesday, December 7, 2011

Stop Cygwin sed adding ^M

So, when I used sed on my files, it would convert windows ascii text to linux ascii text and changing the CRLF on the end of each line of every file it touched to just ^M, regardless of if it made the requested other substitution. This royally screwed up Tortise SVN's ability to tell if the file had been seriously changed. My search for the answer led me to a lot of interesting discoveries about how cygwin deals with file types, ways to finesse how it deals with file types (most of which didn't work), and then the answer itself so simple.

On the cygwin message boards, all the moderators could manage was snarky comments saying "read the FAQ about text mounts":

http://www.cygwin.com/ml/cygwin/2002-01/msg01432.html

Here's some not entirely clear additional info that this is happening during the i/o redirection process and not in sed:

http://www.cygwin.com/ml/cygwin/2002-01/msg01433.html

The basic concept from the above was that the problem I'm having is the standard behavior for cygwin sed; it's supposed to do this. So, what are text mounts? Never really got the feeling I learned for sure, but it seems to have something to do with how the file system was mounted by Cygwin initially? Here may be the faq on text modes:

http://www.cygwin.com/cygwin-ug-net/using-textbinary.html

Somewhere I got the idea that cygwin sed could be forced to output files as windows files if the input filename is given with a colon or backslash in the path name. However, after beating on xargs to make it do that, the output was still not a windows format (backing up ideas from the second link above).

During this process I got more familiar with using xargs with the -I option from this page:

http://www.mkssoftware.com/docs/man1/xargs.1.asp

From this next link I learned two things. First, that the -i argument to sed was the "edit in place" parameter, this was what made it possible to not need to manually command the redirect of sed's output. Secondly, sed has a -b argument to specifiy the file type as binary. The Cygwin man page didn't mention this, as I recall, but it was the answer. This has the effect of telling sed to not convert the line endings, and viola, now the lines in files which sed does not change do not show up as changed anymore:

http://www.gnu.org/software/sed/manual/sed.html

Saturday, December 3, 2011

VI show end of line characters

Two suggestions:

First, ":set list". Love the mnemonic.

http://www.chrispian.com/quick-vi-tip-show-hidden-characters/

I recently found out that using this causes all tab characters to be ripped out or something?

Heres the post that mentioned replacing the /t's, using :e ++ff unix, and other neat hints:

http://superuser.com/questions/97692/vim-show-line-feeds-carriage-return

Wednesday, November 30, 2011

Cygwin comparing binary files

It turns out that cmp is da bomb. cmp -l lists the locations of the bytes that are different and the two values for two binary files. So simple. Here's a link where I learned this, lots of other neat ideas there too:

http://stackoverflow.com/questions/688504/binary-diff-tool-for-very-large-files