Linux Reference Material

Linux Distros

Linux Distros

Here is some collection of Linux reference items, useful for Linux server admins. I personally refer to them often so decided to put in this blog.

Clear Memory Cache on Linux Server

  1. Connect via shell using a program such as Putty
  2. At the shell prompt type crontab -e <enter> as this will allow you to edit cron jobs for the root user.
    • If you are not familiar with vi (linux editor) you press “i” to insert text and once done hit “esc” and type “:wq” to save the file.
  3. Scroll to the bottom of the cron file using the arrows key and enter the following line:
    0 * * * * /root/clearcache.sh
  4. Create a file in '/root' called 'clearcache.sh' with the following content:
    #!/bin/sh
    sync; echo 3 > /proc/sys/vm/drop_caches
  5. Once you have saved this file, the job is complete!

 Find Big Directories

du -smh $(find $1 -type d -maxdepth 1 -xdev) | sort -g

Get the Number of Registered SIP Users on Asterisk

asterisk -rx ‘sip show peers’ | grep -a ‘OK’ 2> /dev/null | awk ‘END {print NR}’

OR if grep gives problems

asterisk -rx ‘sip show peers’ | awk ‘OK’ 2> /dev/null | awk ‘END {print NR}’

OR

asterisk -rx ‘sip show peers’ &> /dev/null | awk ‘OK’ | awk ‘END {print NR}’

Convert MP3 Files to 8KHz Wave

$ lame –decode soundfile1.mp3 soundfile1.wav
$ sox -V soundfile1.wav -r 8000 -c 1 -w soundfile1.raw

To convert a whole clutch o’ files, stick them all in the same directory and do this:

$ for i in *.mp3; do lame –decode $i `basename $i .mp3`.wav; done
$ for i in *.wav; do echo $i; sox $i ${i%%.wav}.raw ; echo ${i%%.wav}.raw; done

Check Number of Files and Folders

ls -l | grep ^- | wc -l
ls -l | grep ^d | wc -l
ls -l | grep ^l | wc -l

echo $(($(ls -l | grep -v ^d | wc -l)-1))

ls *.txt | wc

Using find command with -type switch:

-type c
File is of type c:

b block (buffered) special

c character (unbuffered) special

d directory

p named pipe (FIFO)

f regular file

l symbolic link

s socket

for t in files links directories; do echo `find . -type ${t:0:1} | wc -l` $t; done 2> /dev/null

for t in files links directories; do echo `find . -maxdepth 1 -type ${t:0:1} | wc -l` $t; done 2> /dev/null

ls -l |perl -e ‘while(<>){$h{substr($_,0,1)}+=1;} END {foreach(keys %h){print “$_ $h{$_}\n”;}}’

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

Backup the Whole Linux System Using Tar

tar -zcvpf /archive/full-backup-`date ‘+%d-%B-%Y’`.tar.gz –directory / –exclude=mnt –exclude=proc .

The above command specifies the options “z” (compress; the backup data will be compressed with “gzip”), “c” (create; an archive file is begin created), “v” (verbose; display a list of files as they get backed up), “p” (preserve permissions; file protection information will be “remembered” so they can be restored). The “f” (file) option states that the very next argument will be the name of the archive file (or device) being written. Notice how a filename which contains the current date is derived, simply by enclosing the “date” command between two back-quote characters. A common naming convention is to add a “tar” suffix for non-compressed archives, and a “tar.gz” suffix for compressed ones.

The “–directory” option tells tar to first switch to the following directory path (the “/” directory in this example) prior to starting the backup. The “–exclude” options tell tar not to bother backing up the specified directories or files. Finally, the “.” character tells tar that it should back up everything in the current directory.

Truncate a File

cat /dev/null > <filename>

Find All Files in a Directory and its Subdirectories

find . -type f | wc -l
du -a | cut -d/ -f2 | sort | uniq -c | sort -nr
ls | wc -l

ls -l | grep ^d | wc -l
ls -l | grep ^l | wc -l

Find All Files in a Directory Only

ls -l | grep ^- | wc -l
echo $(($(ls -l | grep -v ^d | wc -l)-1))

Find All Subdirectories in a Directory

ls -l | grep ^- | wc -l
echo $(($(ls -l | grep -v ^d | wc -l)-1))
find . -maxdepth 1 -type d | wc -l

Find All Softlinks in a Directory

ls -l | grep ^l | wc -l

Find All Subdirectories in a Directory and its Subdirectories

find . -type d | wc -l

Mirror a Website

wget -m <URL>

Remove All Leading and Trailing Spaces in a File

sed ‘s/^[[:space:]]*\(.*\)[[:space:]]*$/\1/’

Add Colors to Grep Outout

export GREP_OPTIONS=’–color=auto’

Mass Renaming of the Files, e.g. js?P to js

find . -name ‘*.js?P’ -exec sh -c ‘mv “$0” “${0%.js?P}.js”‘ {} \;

Mass Renaming Uppercase to Lowercase

for f in `find`; do mv -v $f `echo $f | tr ‘[A-Z]’ ‘[a-z]’`; done

Mass Renaming Convert Spaces to Underscores

rename ‘ ‘ ‘_’ *

Insert A Character, e.g. ‘;’ At the End of Each Line in Vi

:%s/$/;/g

Insert A Character, e.g. ‘;’ At the Start of Each Line in Vi

:%s/^/;/g

Extracting an APK from Android phone

adb shell pm list packages

Then you may select an app, for instance twitter

adb backup -apk com.twitter.android

An important thing here is to not set up a password for encrypt your backup

This is going to create a file named as backup.ap, but you still can’t open it. For this you got to extract it again but using the dd command.

dd if=backup.ab bs=24 skip=1| openssl zlib -d > backup.tar

Now extract the tar contents and that’s it.y select an ap

Change a word in multiple files

grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'

Leave a Reply

Your email address will not be published. Required fields are marked *