How to calculate and print number of files in each subfolder of the current folder on Linux

find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr

That is: find all subdirectories of current directory; for each subdirectory, find all files, and print file count and path. Finally it sorts the result to show folders with most files on top
Note that the first entry is the folder itself. Also this script omits soft links and does not count sub-folders as files.

For example:

[someone@somewhere]# cd /etc/X11/
[someone@somewhere]# find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr
10 .
7 ./xinit
3 ./xinit/xinitrc.d
0 ./xorg.conf.d
0 ./xinit/Xclients.d
0 ./fontpath.d
0 ./applnk

How to calculate and print number of files in each subfolder of the current folder on Linux

Selenium WebDriver on Firefox: Working with Add-Ons

Running Selenium WebDriver on Firefox with Static Add-Ons

  1. Create a special profile for Firefox
  2. Install add-ons on that profile
  3. Start Firefox as described here

Installing Add-On when Starting Selenium WebDriver on Firefox

import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxDriver;
// ...
final String addOnPath = "C:\\Temp\\youraddon.xpi";
File addOnFile = new File( addOnPath );
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension( addOnFile );
WebDriver driver = new FirefoxDriver( profile );

Getting List of Installed / Active Add-Ons with Selenium WebDriver on Firefox

There’s no easy way to achieve this unfortunately. So the method below is really an ugly hack, but it get the job done:

  • Firefox is loaded on about:addons page
  • The page contains list of add-ons in JSON format, which can be parsed
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "browser.startup.homepage", "about:addons" ); 
WebDriver driver = new FirefoxDriver( profile );
driver.get( "about:addons" );
String source= driver.getPageSource();
final String addOnsSectionStart 		= "<browser id=\"discover-browser\"";
source = source.substring( source.indexOf( addOnsSectionStart ) + addOnsSectionStart.length());
source = source.substring( source.indexOf( "{%22" ) );
source = source.substring( 0, source.indexOf( "\" clickthrough" ) );
source = URLDecoder.decode( source, "UTF-8" );
JSONObject addonObjects = new JSONObject(source);
JSONArray jsonAddonArray = addonObjects.names();
		"user disabled",
		"is compatible",
		"is blocklisted",
for(int i = 0; i < jsonAddonArray.length(); i++)
	JSONObject jsonAddonObject = addonObjects.getJSONObject(jsonAddonArray.getString(i));

The output will look like this:

type      	version        	user disabled  	is compatible  	is blocklisted 	name
plugin        	false          	true           	false          	QuickTime Plug-in 7.7.1
plugin      	false          	true           	false          	Java(TM) Platform SE 6 U26
theme     	17.0.1         	false          	true           	false          	Default
plugin    	2.4.2432.1652  	false          	true           	false          	Google Updater
extension 	2.28.0         	false          	true           	false          	Firefox WebDriver
Selenium WebDriver on Firefox: Working with Add-Ons

Creating a First Facebook Application

  1. Before you start, you need
    • Facebook account (obviously) with added Developers app.
    • A web location to host the application. In that location, create a sub-folder for your application (can be empty for now). Later referred to as
  2. Go to Facebook Developers page and click Set Up New Application button.
  3. Enter the Application Name, agree to the Terms of Service, and click Create Application button.
  4. On Edit Application page, which will be opened after application is created (or can be opened by choosing Edit Settings on My Applications page, while application you want to edit is selected), go to Canvas tab.
  5. In Canvas Page URL, specify the URL, on which application will be available inside Facebook.
  6. In Canvas Callback URL, specify the URL.
  7. Go to your hosted space and create a “default” page (e.g. default.htm or index.html, depending on hosting service) with the authentication code described on Authentication Page, Single Sign-on with the JavaScript SDK section. The values for FACEBOOK_APP_ID and FACEBOOK_SECRET have to be replaced with values that can be found on Basic tab of Edit Application Page.
  8. Now try to access your application via Facebook Canvas Page URL. You should get the “Logged out” page at first, with option to login. Once you login, you should see a “User ID”. This application doesn’t do anything, but this is your first app! You can add a “Hello, world!” line to it too, if you wish.
Creating a First Facebook Application

A few PowerShell 2.0 Know-Hows

Script Snippets

1. Create a Unique Folder Name

(From: “Hey PowerShell guy, how can I create a new folder using a name based on the current date”)

function Create-UniqueFolder([string]$path) {
  md ($path + "\result_$((get-date).toString('yyyyMMddHHmmss'))")

2. Check if Script Runs on x86 or x64 Platform

(From: “What is the best way to program against PowerShells x64 vs x86 Variability”)

function Is-Wx64 {
  return test-path (join-path $env:WinDir "SysWow64")

3. Locate an Assembly (in Known Path, such As GAC)

function Get-AssemblyPathByName([string]$name) {
   return [System.Reflection.Assembly]::LoadWithPartialName($name).Location

4. Read XML File

$file = "XML file path"
[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
# ... for example ...

5. Replace All Occurrences of a String in File

function Replace-InFile([string]$file, [string]$find, [string]$replace) {
  (Get-Content $file) | Foreach-Object {$_ -replace $find, $replace} | Set-Content $file

6. Handle a Process

Start a Process, Get Process Output and Standard Error Output, Wait for Process to Complete

# ... example doesn't load user profile, but redirects and retrieves stdout and stderr
$StartInfo = New-Object System.Diagnostics.ProcessStartInfo
 $StartInfo.FileName = "process path and name"
 $StartInfo.Arguments="process arguments, if any"
 $StartInfo.LoadUserProfile = $false
 $StartInfo.RedirectStandardOutput = $true
 $StartInfo.RedirectStandardError = $true
 $StartInfo.UseShellExecute = $false
 $StartInfo.WorkingDirectory = "working directory path"

 $process = [System.Diagnostics.Process]::Start($StartInfo)
# At this point can access process properties, e.g.:
Write-Host ("Started process with PID=" + $process.Id)

# Saving output to a file
 $stderr = $process.StandardError
if ($stdout -ne $null) {
  $output = $stdout.ReadToEnd()
  $outfile = "location and name of file to save output to"
  if ($output -ne "") { $output | Out-File $outfile }
# Saving standard error output to a file
$stdout = $process.StandardOutput
 if ($stderr -ne $null) {
  $errors = $stderr.ReadToEnd()
  $errfile = "file to save errors to"
  if ($errors -ne "") { $errors | Out-File $errfile }

 [void] $process.WaitForExit

7. Get Script Own Path

(From: Get Script Directory)

$scriptPath = Split-Path $MyInvocation.MyCommand.Path

Syntax and Cmdlets

1. Show Something on Screen

Write-Host "whatever you want to show on screen"

2. Run WMI Query

gwmi win32_service # or other class

Also handy in conjunction with filter:

gwmi win32_service | where-object {$_.Name -eq "service name" }

… And query on top of that:

gwmi win32_service | where-object {$_.Name -eq "service name" } | select Status,State,StartName,StartMode

3. Text File


Get-Content "my file"


Set-Content "my file" "my content"


Add-Content "my file" "my content"

4. Check if File Exists

test-path "file path and name"

5. String Functions

See: The String’s the Thing

6. Comparison Operators

See: About Comparison Operators

7. Special Characters (like tab, newline)

`0 -- Null
`a -- Alert
`b -- Backspace
`n -- New line
`r -- Carriage return
`t -- Horizontal tab
`' -- Single quote
`" -- Double quote

A few PowerShell 2.0 Know-Hows

Importing data from Excel to SQL Server

Different ways to accomplish this are described in Microsoft KB article #321686. If importing is not recurring, or persistent connection is not possible, it makes sense to use Distributed Query. Running such query in SQL Server Management Studio of SQL Server 2005 will, however, produce the following error:

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

It can be resolved by enabling “advanced options” and “Ad Hoc distributed Queries” option, executing query to import data, and (optionally) reverting the configuration to its original state:

# First, enable advanced options
sp_configure ‘show advanced options’, 1
# Second, enable execution of Ad Hoc queries
sp_configure ‘Ad Hoc Distributed Queries’, 1
# Import data from Excel to SQL table
SELECT * INTO TableName FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;Database=Location\Name.xls’, [SheetName$])
# Revert both options back: first disable Ad Hoc distributed queries
sp_configure ‘Ad Hoc Distributed Queries’, 0
# … And hide advanced options
sp_configure ‘show advanced options’, 0
Importing data from Excel to SQL Server