Linux 4 All

Best Linux tricks source

Posts in the Linux category

Some time ago I had to deal with a request to delete all but the newest 50 files from a folder. To make my life a little bit miserable, there were other folders inside that folder which shouldn’t be deleted and obviously some files with space in their names

In the end I did a small script taking the folder as first parameter and number of files to keep as the second one.

#!/bin/bash
if [ $# -eq 0 ]
 then
  echo " This script will delete all but newest files from a specified folder."
  echo ""
  echo " Usage: source $0  "
  exit 1
fi

TOTAL_FILES=$(find $1 -maxdepth 1 -type f -print0| xargs -0 ls -lt | wc -l)
FILES_TO_DELETE=$(echo "$TOTAL_FILES-$2" | bc)

find $1 -maxdepth 1 -type f -print0 | xargs -0 ls -t | tail -$FILES_TO_DELETE | sed -e 's/ /\\ /g' | xargs rm

To disable cluster services and change VG locking type to no:

service rgmanager stop && chkconfig rgmanager off
vgchange -c n $CLUSTERED_VG_GROUP
sed -i 's/locking_type = 3/locking_type = 1'
service clvmd stop && chkconfig clvmd off
service cman stop && chkconfig cman off

To enable cluster services and change VG locking type to yes:

service cman start && chkconfig cman on
service clvmd start && chkconfig clvmd on
sed -i 's/locking_type = 1/locking_type = 3'
vgchange -c y $CLUSTERED_VG_GROUP
service rgmanager start && chkconfig rgmanager on

Should you need to disable Trace and Track methods on Apache, add the following lines to httpd.conf and restart Apache.


 RewriteEngine on
 RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
 RewriteRule .* - [F]

TraceEnable off

I admit that is pretty lame, but until today I had no idea how to kill a specific MySQL query. It is quite easy.

First we need to get the queries:

mysql> show processlist;
+------+------+-----------+------+---------+------+-------+------------------+
| Id   | User | Host      | db   | Command | Time | State | Info             |
+------+------+-----------+------+---------+------+-------+------------------+
| 5585 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+------+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

Then kill the id:

mysql> kill 5585;
ERROR 1317 (70100): Query execution was interrupted
mysql>

This query will display both the total size and number of tables for each MySQL engine.

SELECT engine,ROUND(SUM(index_length+data_length)/1024/1024,2) AS 'size in MB',count(engine) FROM information_schema.tables GROUP BY engine;

A nice MySQL query to get all FTP users and passwords from Plesk:

# mysql -u admin -p$(cat /etc/psa/.psa.shadow) -D psa
SELECT REPLACE(sys_users.home,'/home/http/vhosts/','') AS domain,
sys_users.login, accounts.password
FROM sys_users
LEFT JOIN accounts on sys_users.account_id=accounts.id
ORDER BY sys_users.home ASC;

A small and simple script to get users from a RHEL/CentOS box. It assumes that first UID is 500 and it won’t go beyond UID 999

1. Get all users

awk -F ":"  '$3>=500 && $3<1000  {print $1}' /etc/passwd

2. Get users with valid shell.

awk -F ":"  '($3>=500 && $3<1000) && ($7 ~ /sh/) {print $1,$7}' /etc/passwd

3. Get ftp users

awk -F ":"  '($3>=500 && $3<1000) && ($7 !~ /sh/) {print $1,$7}' /etc/passwd

This is a small shell script to get all files greater than 100M starting from “/”. Replace SIZE and FS values to fit your needs.

SIZE=100M; FS='/'; find $FS -size +$SIZE -exec du -hs {} \; 2>/dev/null | sort -nrk 1,2

A little “find and sort” magic to find largest 50 files on a server:

find / -type f -size '+1024k' -printf "%s %h/%f\n" | \
sort -rn -k1 | \
head -n50 | \
awk '{ printf("%5dMB\t%s\n", $1/1048576, substr($0, index($0, " ")+1, length($0))) }' 

People do stupid things. Most common is to recursively change all permission on / instead of just a folder:

chown -R user / var/www/html

Ooops, I accidentally added a space there and now I’m screwed. I will have to reinstall the system. 🙁 Well, not exactly. If you were careful enough to get ACL backup of your file system, you might have a chance:

getfacl -epR / | sed -e 's/\/\//\//g' >$(hostname).acl

Restoring all the permissions from an acl file is easy. All you need is running the following command:

setfacl --restore=$(hostname).acl

If you didn’t destroy everything, but just a folder, first you will need to use a text parser like awk, sed, perl or whatever suits you to get a specific folder ACL from the file you now have. Below example restores /etc

sed -n '/^#\ file:\ \/etc/,/^$/p' $(hostname).acl > etc.acl
setfacl --restore=etc.acl