Find & delete files

find /path/to/dir -args

Delete empty directories

find /path/to/dir -empty -type d -delete

Delete empty files

find /path/to/dir -empty -type f -delete

Count all empty files or directories

Count empty dirs only

find /path/to/dir/ -empty -type d | wc -l

Count empty files only

find /path/to/dir/ -empty -type f | wc -l

Where :

  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

This is useful when you need to clean up empty directories and files in a single command.

Cleanup unnecessary files and Desktop OSses clutter

cd "$SOURCEDIR"
find ./ \( -name ".DS_Store" -or -name "._*" -or -name "Thumbs.db" -or -name "*.tmp" -or -name "*.lnk" -or -name "TemporaryItems" -or -name "folders.501" -or -name ".TemporaryItems" -or -name "__MACOSX" \) -ls -delete

Change ownership of files owned by a specific username

find /path/to/dir/ -user username -exec chown newuser:newgroup {} +

Carriage return and line feed

Line feed and carriage return are two different ways of how your computer sees the Enter key.

While for example the character M is interpreted the same way in Windows and in UNIX, the ENTER key is interpreted differently. This causes problems when transferring files and data from one platform to another one.

When transferring files from one platform to another, you can experience that ENTERs are not translated in a way thay you would expect.

Windows uses carriage return – line feed.

UNIX uses line feed.

Macintosh uses carriage return.

A line feed is expressed in hexadecimal as 0a. A carriage return is noted as hexadecimal 0d. Windows carriage return – line feed is 0a0d in hexadecimal notation.

How do I spot a problem with carriage return and line feed?

When you take a UNIX file and open it in Windows NOTEPAD, you will most likely see something like the following example:

<?php // $Id: blog.module,v 1.271.2.2 2007/04/23 17:05:11 dries Exp $log_user($type, &$edit, &$user) {&nbsp;&nbsp; if ($type == 'view' && user_accesa page that displays the most recent blog entries from all the users tion handbook <a href="@blog">Blog page</a>.', array('@blog' => 'httpORDER BY n.created DESC"), 0, variable_get('feed_default_items', 10))ser->uid) && user_access('edit own blog')) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $output = '<li>'. blog entries of all users.&nbsp; */ function blog_page_last() {&nbsp;&nbsp; global $object(db_query('SELECT i.*, f.title as ftitle, f.link as flink FROM ge) {&nbsp;&nbsp;&nbsp;&nbsp; // Breadcrumb navigation&nbsp;&nbsp;&nbsp;&nbsp; $breadcrumb[] = array('path' =may_cache) {&nbsp;&nbsp;&nbsp;&nbsp; $items[] = array('path' => 'blog', 'title' => t('Bloblog', array('title' => t('Read the latest blog entries.'))) .'</div>

All the lines are compressed and enters are clearly missing. Well, they are not completely missing, they are just coded in a format that Notepad does not understand. Why this happens, you can find that at this page: Line termination: line feed versus carriage return.

How do I know what system a file was created in?

When you open the file in a HEX editor, you can tell by looking at the HEX code to find out whether your Enters were encoded as carriage returns, line feeds, or the pair of both.

Enter as line feed

This picture shows you that Enters have been encoded as line feed.

How do I fix a problem with carriage return?

One way to fix this is to open the file using a HEX editor and replace all the “bad” enters with the “good” ones. In our case, we would replace all references to 0a with 0d0a. Then, this file should be readable by Windows Notepad.

One very nice and easy fix is to:

-> open your file in Notepad,
-> copy the content of the file,
-> paste it into MS Word,
-> copy the text in MS Word,
-> and paste it back to Notepad

If you do this with the file pictured above, you will get the following result:

Enter as carriage return line feed

You can see that all Enters have been coded in a form of carriage return – line feed, that is 0d0a.

You can open the file in Windows Notepad and see the code nicely formatted line by line as they were on the UNIX computer.

Line termination: line feed versus carriage return 0d 0a

Line feed and carriage return are two different ways of computer interpretting the ENTER key. Line feed and carriage return are two different ways how lines are ended in the computer language. Both the line feed and carriage return originate in the typewriter age.

The best way to explain this is is to ask:

“What do you suppose your computer sees when you press the Enter key?”

You can rest assured that your computer translates the ENTER key stroke into something. But while for example the character A is interpreted the same way by all common platforms (Windows, UNIX, Mac), the ENTER key is interpreted differently.

This causes problems when porting files from one platform to another one.

How ENTER works in computer language?

When you press A on your keyboard, you will see letter A on the screen. When you press B on your keyboard, you will see letter B on the screen.

Each of these two characters gets translated into a code that the computer can understand. Each of these two characters is one byte long, and your computer has some algorithm for translating these human readable letters into code numbers.

The computer number assigned to the letter A happens to be 65 (you can find this in the ASCII table). In other words, the decimal representation of the letter or character A is 65. If you translate this into the computer zeros and ones, you will get 1000001, which is 8 bits, that is one byte.

Decimal binary hex or hexadecimal – What is it?

Characters’ code values are represented by various utilities using different numbering schemes. Some times we talk about the decimal number system. In this system, letter A is represented by number 65. Letter B is represented by number 66.

Some times (rarely) symbols are expressed in binary number system. In this numbering scheme, letter A would be 1000001 as we have already mentioned, and the letter B would be expressed as 1000010.

Quite often, symbols are expressed using the hexadecimal number system. Letter A in this system is expressed as 41 and letter B is 42.

Decimal 10000001, binary 65, and hexidecimal 41 are different numeric “languages” for the same letter.

There are other numbering schemes such as octal, but we won’t get into those.

So, what does my computer see when I hit ENTER?

When you look inside some file that you created, you can find out. All that you need for this job is some hexadecimal editor. You can use for example the PSPad editor.

Open a text file in the text editor and type the following sequence:

A
hit enter
B
hit enter

Now when you open the file in a hexadecimal editor in a HEX view, you should see the following:

Carriage return 0d line feed 0a

Note that the letter A shows as 41 and the letter B shows as 42.

If your file was created in Linux, you would also see a single character 0a after each letter, that is, wherever you pressed the ENTER key.

If your file was created in Windows, the pair of characters 0d and 0a would appear any place where you pressed the ENTER key.

The hexadecimal 0a, a control character as opposed to a printing character, is called a line feed.

The hexadecimal 0d is called a carriage return.

Pretty much all the programs on the Windows platform understand and expect the hexadecimal 0a0d pair in text. The 0d0a pair of characters is the signal for the end of a line and beginning of another.

On the other hand, a UNIX program expects the single 0a character to denote the same thing.

A problem arises with cross-platform exchange of files. If you transfer files from Linux to Windows or Mac, the software sees something other than what it expects.

Windows versus UNIX versus Macintosh

As if it was not enough, Macintosh makes the whole story even more complicated. The original Mac operating system used carriage return 0d as the line separator. 

So, to summarize, Windows uses carriage return – line feed, UNIX and newer Mac use line feed, and older Macs use carriage return.

Windows :

preceding text -> 0d 0a -> succeeding text

UNIX, Mac OS X :

preceding text -> 0a -> succeeding text

MAC (OS-9 and earlier):

preceding text -> 0d -> succeeding text

ASCII table

ASCII is a standard for character encoding used by computers and communications equipment to represent text. ASCII is short for American Standard Code for Information Interchange. You can find the ASCII table below.

Computers can only understand numbers, so a code ASCII is the numerical representation of a character such as M or 8 or $ or an action of some sort. ASCII uses a single byte to represent each character. A byte is generally the smallest addressable unit of data on a computer. It is a continuous sequence of eight bits, that is zeros or ones.

This means that one byte could represent any of 256 characters ranging in binary notation from 00000000 to 11111111. Eight bits allows 256 combinations of zeros and ones.

ASCII table

This following ASCII table lists the ASCII characters and their decimal, octal and hexadecimal numbers.

Below you can find the 128 standard character encodings in US-ASCII, which is the original and most basic version of ASCII. Each of these numbers in the ASCII table is a seven digit binary number between 00000000 and 01111111. The eighth bit (the left-most bit) was originally reserved for use as a parity bit.

The first 32 ASCII codes in the ASCII table below (zero through 31 in decimal notation, or 00000000 through 00011111 in binary) are reserved and are called control characters. These are non-printing ASCII characters. They were originally intended to control devices, most importantly printers. These characters appear in the most left column. The ASCII non-printing characters are rarely used for their original purpose these days.

CharDecOctHex/CharDecOctHex/CharDecOctHex/CharDecOctHex
null byte000000x00|space3200400x20|@6401000x40|`9601400x60
start of heading100010x01|!3300410x21|A6501010x41|a9701410x61
start of text200020x02|3400420x22|B6601020x42|b9801420x62
end of text300030x03|#3500430x23|C6701030x43|c9901430x63
end of transmission400040x04|$3600440x24|D6801040x44|d10001440x64
enquiry500050x05|%3700450x25|E6901050x45|e10101450x65
acknowledge600060x06|&3800460x26|F7001060x46|f10201460x66
bell character700070x07|3900470x27|G7101070x47|g10301470x67
backspace800100x08|(4000500x28|H7201100x48|h10401500x68
horizontal tab900110x09|)4100510x29|I7301110x49|i10501510x69
newline1000120x0a|*4200520x2a|J7401120x4a|j10601520x6a
vertical tab1100130x0b|+4300530x2b|K7501130x4b|k10701530x6b
formfeed1200140x0c|,4400540x2c|L7601140x4c|l10801540x6c
carriage return1300150x0d|4500550x2d|M7701150x4d|m10901550x6d
shift out1400160x0e|.4600560x2e|N7801160x4e|n11001560x6e
shift in1500170x0f|/4700570x2f|O7901170x4f|o11101570x6f
data link escape1600200x10|04800600x30|P8001200x50|p11201600x70
device control 11700210x11|14900610x31|Q8101210x51|q11301610x71
device control 21800220x12|25000620x32|R8201220x52|r11401620x72
device control 31900230x13|35100630x33|S8301230x53|s11501630x73
device control 42000240x14|45200640x34|T8401240x54|t11601640x74
negative acknowledge2100250x15|55300650x35|U8501250x55|u11701650x75
synchronous idle2200260x16|65400660x36|V8601260x56|v11801660x76
end of trans block2300270x17|75500670x37|W8701270x57|w11901670x77
cancel2400300x18|85600700x38|X8801300x58|x12001700x78
end of medium2500310x19|95700710x39|Y8901310x59|y12101710x79
substitute2600320x1a|:5800720x3a|Z9001320x5a|z12201720x7a
escape2700330x1b|;5900730x3b|[9101330x5b|{12301730x7b
file separator2800340x1c|<6000740x3c|\9201340x5c||12401740x7c
group separator2900350x1d|=6100750x3d|]9301350x5d|}12501750x7d
record separator3000360x1e|>6200760x3e|^9401360x5e|~12601760x7e
unit separator3100370x1f|?6300770x3f|_9501370x5f|delete12701770x7f
ASCII Table

Note that lower and upper case characters are understood by the computer differently.

Reset debian-sys-maint MySQL password

On Ubuntu systems there is a (system) mysql user debian-sys-main that is used by the system’s init scripts to control the mysql database, e.g. to start or stop the mysql server. The password of this user is stored (in clear text) in /etc/mysql/debian.cnf. If this password does not match the the actual password in the mysql server the mysql init scripts will fail:

# /etc/init.d/mysql restart* Stopping MySQL database server mysqld
[fail]* Starting MySQL database server mysqld
[ OK ]# /etc/init.d/mysql status/usr/bin/mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'debian-sys-maint'@'localhost' (using password: YES)'

Moreover trying to update the mysql server will fail with an error like:

/var/cache/apt/archives/mysql-server-5.1_5.1.37-1ubuntu5.1_i386.deb E: Sub-process /usr/bin/dpkg returned an error code (1)

To fix the problem you have to update the mysql password for the user debian-sys-main:

  1. Get the password from /etc/mysql/debian.cnf.
    The clear text password is stored twice in the file (the lines starting with
    “password =”:# Automatically generated for Debian scripts. DO NOT TOUCH!
    [client]
    host = localhost
    user = debian-sys-maint
    password = your-secret-password
    socket = /var/run/mysqld/mysqld.sock
    [mysql_upgrade]
    host = localhost
    user = debian-sys-maint
    password = your-secret-password
    socket = /var/run/mysqld/mysqld.sock
    basedir = /usr
  2. Update the password in the mysql server (you need mysql root access):
    mysql –user root –password mysql> SET PASSWORD FOR ‘debian-sys-maint’@‘localhost’ = PASSWORD(‘your-secret-password’);
  3. If an previous mysql-server system upgrade failed, just restart the upgrade.

The (debian) documentation can be found in /usr/share/doc/mysql-server-5.1/README.Debian:

[…] You may never ever delete the special mysql user “debian-sys-main”. This
user together with the credentials in /etc/mysql/debian.cnf are used by the
init scripts to stop the server as they would require knowledge of the mysql
root users password else.
So in most of the times you can fix the situation by making sure that the
debian.cnf file contains the right password, e.g. by setting a new one
(remember to do a “flush privileges” then). […]