Categories
Linux Server Administration Tips

How to count files (get the number of files) under a directory in Linux?

Just perform this command under the directory:

find . -type f | wc -l

and you will get the total number of files under it (recursively). Simply copy and paste that to do the trick.

Or if you just need to count the files directly under the current directory:

ls -f | wc -l

Which is a lot faster than the previous solution.

This should also work on all *nix OS.

Categories
SQL / MySQL Tips and Tutorials

Using SQL to Find records existing in one table but not in another

The 2 tables are built with an identical data structure. Say you have records spread in both tables but neither of them are complete because both contain unique records. You need to consolidate the 2 tables into one with all unique records from each table.

NOT EXISTS clause is just the way to do it in SQL. Let’s try:

SELECT * FROM table2 WHERE NOT EXISTS (SELECT * FROM table1 WHERE id = table2.id)

This will select all records from table2 that don’t exist in table1 by id. To get a complete table of unique records from both table1 and table2, you need to just insert the results here into table1.

INSERT INTO table1 (SELECT * FROM table2 WHERE NOT EXISTS (SELECT * FROM table1 WHERE id = table2.id))