Mass changing files’ modification date on Windows

It’s sometimes useful to be able to change a file’s modification date. Linux makes it easy by simply using the “touch” command. I found this rather useful in cases where files were transferred between servers, which unfortunately do not have their times synchronized.

So, how can this be done on Windows?

Here is the command to issue in order to modify a single file’s modification time:

copy filename,,+

But in most cases, you’d be looking at mass-changing the modification date of files inside a certain directory. To do so, use the following command:

for %f in (c:\path\to\folder\*.*) do copy %f,,+

This would loop over all files containing a “dot” (i.e. extension) in c:\path\to\folder, and changes their modification time to the current system time. If for example you want the above to only go through XML files within that folder, then use *.xml instead of *.*

PS: the above command would work if you’re manually issuing it via the command prompt. However, if you’d like to embed that it in a batch file, make sure to replace %f with %%f. In other words, the command would turn into:

for %%f in (c:\path\to\folder\*.*) do copy %%f,,+

Windows “Workstation” service not starting up? read this

At one point in time, I wasn’t able to RDP into a windows host. Despite using the correct credentials, I was greeted with the following error: “Unable to obtain Terminal Server User Configuration”.

I had to physically access the machine and look into the Event Viewer for hints, but not much could be found there.

Next stop: services.msc — this showed that the “Workstation” service is stopped. Starting it up failed.

As a matter of fact, several services that depend on the aforementioned one failed to start (obviously).

Long story short, the following sorted out the issue, but I do not claim it’s a trick that would always work:

  • I opened the registry editor (regedit.exe)
  • Browsed to:
    • HKEY_LOCAL_MACHINE
    • SYSTEM
    • CurrentControlSet
    • Services
    • lanmanworkstation (not lanmanserver)
  • Deleted the following keys:
    • DependOnService
    • DependOnGroup
  • Restarted

While this did the trick, manually interfering with service dependencies is not always a good diea.

 

Highlighting “grep” matches

“grep” is a useful Linux tool allowing the user to print lines matching a specific search pattern. Most popular uses is by “piping” the output of one command to “grep” (such as cat /path/to/file | grep findme), or by directly looking for a pattern inside one or more file (e.g. grep findme /path/to/*.txt).

This would print lines that match the search string / expression. However, how to colorize the matching patterns? Simple! Use the --color=always flag, i.e.:

grep --color=always -i findme /path/to/*.txt

Next, if you’re doing lots of “grep” work and don’t want to keep specifying the color flag, you can opt for the following:

Add it to the GREP_OPTIONS environment variable

export GREP_OPTIONS=’--color=always’

Or, if you’re using bash as your shell, add it to /etc/bashrc. Simply edit that file using your preferred editor (e.g. vim, nano, etc.), and add the above export command at the very end of the file. When done, save and exite the editor, then issue the following command, so that the changes take effect: source /etc/bashrc

Determining, via the shell prompt, the Linux distribution in use

SSH is sometimes the only mode of access you have to a server. So how do you instantly determine the distro in use?

The following would work for most popular distributions (RedHat, CentOS, Ubuntu, etc.):

cat /etc/*-release

If the above returns “No such file or directory”, try the following which would work on Debian-based distros:

lsb_release -a

Finally, for kernel-related data, use the following command:

uname -a

You’ll be able to determine the kernel version and release date, whether it’s a 32 or 64 bit hardware and the current machine hostname.

Flushing / clearing DNS in Mac OSX

Below is how to clear / flush the DNS cache in Mac OSX.

(1) Open the terminal (Finder -> Applications -> Utilities -> Terminal)

(2a) In case your operating system is ‘Leopard’ (10.5.*) or ‘Snow Leopard’ (10.6.*), issue the following command:

dscacheutil -flushcache

(2b) Otherwise, for older operating systems, issue the following command:

lookupd -flushcache

This is the equivalent of the following Windows command: ipconfig /flushdns

Deleting a file, the name of which starts with a hyphen (Linux)

You might have come across a difficulty in deleting a file in Linux whose name starts with a dash/hyphen.

Adding an escape character (e.g. rm -rf \-filename) or quoting the file name (e.g. rm -rf “-filename”) will not do the job.

The solution is simple: explicitly provide the full or relative path to the file. e.g.:

rm -rf /path/to/-file

or the following – if for instance your current working directory is the one containing the file in question

rm -rf ./-file

Linux: finding files modified or accessed at a certain time

The ‘find’ utility is quite handy when used with the proper parameters. Below is a few ways to find files/folders based on the time they were accessed or modified.

Example 1:

find /path/to/folder -type f -name “*.txt” -mtime -5

This will find ‘files’ (-type f) within the folder /path/to/folder (or one of its sub directories), whose name ends with ‘.txt’, and which were modified (mtime) less than 5 days ago

Example 2:

find /path/to/folder -type d -name “stat*” -mtime +10

This would find ‘directories’ (-type d) within the folder /path/to/folder (or one of its subdirectories), whose name starts with ‘stat’, and which were modified more than 10 days ago

Example 3:

find . -iname “*.pdf” -mtime +2 -mtime -7

This would help you find files or folders in the ‘current directory’ (.), whose name ends with ‘.pdf’ regardeless of the case of that suffix (e.g. example.PDf would be matched), which were modified more than 2 days and less than 7 days ago (i.e. between 3 to 6 days ago).

Example 4:

find / -type f -atime -5 -size +100k

This example makes use of atime, i.e. time of access (as opposed to mtime, which is the time the file was modified). The above command would help you find files in your entire system (/ being the system root, unless your shell is jailed), which were accessed less than 5 days ago, and which are at least 100 kbytes in size.
PS: if you do not specify the ‘b’, ‘k’, ‘m’, etc., the default would be blocks of 512 bytes. For instance, using +100 instead of +100k, means you’re looking for files sized at least 100*512=51200 bytes (50 kbytes).

More examples to be added…

Facebook’s new photo viewer

Facebook recently introduced a new way of browsing photos and albums. The new photo viewer uses the lightbox technique whereby the page is dimmed and a box is overlaid with a larger version of the selected image.

Some of the improvements it brings are the ability to stay on the page without having to go back in your browser’s history (you simply hit the close ‘x’ button and you’re back to where you were), faster photo flipping/browsing and the ability to download a high res version of the image (if available).

For those who hate this lightbox preview, you may switch to the old fashioned way by simply opening the picture in a new tab (hold ctrl key + click) or a new window (hold shift key + click) instead of simply clicking on the picture. You may then continue browsing in the new tab/window the same way you were used to.

Forcing links that open in a ‘new window’ to open in a ‘new tab’

Do you want to force links that open in a new window to open in a new tab instead? The answer is pretty simple.

Firefox

  • Type the following in the url bar: about:config
  • Edit the following variables:
    • browser.link.open_external -> 3
    • browser.link.open_newwindow -> 3
    • browser.link.open_newwindow.restriction -> 0
  • If any of those variables is not yet defined, simply right click and select ‘new->integer’, enter the variable name then its integer value.

IE8

Simply:

  • Go to: tools -> internet options
  • Then make sure you select ‘always open pop-ups in a new tab‘ under ‘when a pop-up is encountered

That’s it!