Linux Top 20 useful Shell Commands

Linux command line examples from practice with 20 commonly used commands

You don’t want to see a complete list here, but there may be a start for some of the common Linux commands, useful for sysops and admins.

1. Translate ASCII character set to octal, decimal, and hexadecimal

$ man ascii
man page ascii characters
Illustration: man ascii

2. Output calendar to console

$ cal
Calendar view 6 months with week number, cal -n 6 -w

Calendar view 6 months with week number

$ cal -n 6 -w

3. Edit binary hexadecimal in VIM

$ vi file
 :%!xxd
 ..
 :%!xxd -r
 :wq

4. Compare two files

$ diff -w file1.txt file2.txt
2c2.3
< Jacqueline --> C. Meier
> Jacqueline Mayer

5. Find content in files recursively using find

$ find . -type f -print0 | xargs -0 grep "mojito"

6. Send a file as e-mail

$ mail -s subject jacque@unblog.ch < notes.txt

7. Download multiple URLs from list

$ cat download-urls.txt | xargs wget -c

8. Remove duplicate lines with awk

$ awk '!($0 in array) { array[$0]; print }' temp

Line output of /etc/passwd with the same uid and gid

$ awk -F ':' '$3==$4' /etc/passwd

9. Conversion from Windows/DOS (CR/LF) to Unix (LF) format

$ sed 's/.$//' filename
# Text Search and Replace
$ sed -e s/dog/cat/g file.txt > file.new
# Deletes all blank lines
$ sed '/^$/d' filename
# Deletes spaces at the end of each line
$ sed 's/ *$//' filename

10. Recursive String Search in Files

$ grep -r "ramsch" *

11. How many CPMs does my computer have?

$ grep processor /proc/cpuinfo | wc -l

12. View Free Memory

$ cat /proc/meminfo | grep MemFree
$ free -m

13. View active processes

$ ps -ef
$ ps aux
$ vmstat 5 10

14. Create directory tree (tree)

$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

15. Output Asterisk PBX Applications

$ asterisk -rx "show applications" | awk '{print $1}' | sed -n -e "s/:"//p

16. Number of MTA emails identified by SPAM

$ cat /var/log/maillog | egrep -c '(Alert\!)|(identified spam)|(reject\:)'

17. How long does the computer run

$ uptime

18. Set time and date

$ date -s "Dec 12 18:30:00 2014"

19. Who am I and the effective user ID, UID and GID output

$ whoami
$ id

20. Output list of most recently logged in users

$ last -a

Server Memory cache buffers reset

Linux admin does not know this, according to a conscience uptime almost all memory is used up for cache and buffers of the server, actually it is not necessary to intervene in the memory management of the system, but the system is scarce in memory, this can be prevent with a simple script.

free_mem

After login as root and open the script in editor:

$ vi /usr/sbin/clearcache.sh

#!/bin/bash
# freeing cache buffers

echo $(date) >> /var/log/clearcache.log

freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)

cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)

sync; echo 3 > /proc/sys/vm/drop_caches

freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)

echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM." >> /var/log/clearcache.log

Now make the script executable.

chmod u+x /usr/sbin/clearcache.sh

and run as crontask at 5, 1 3 and 9 p.m.

echo "0 5,13,21 * * root /usr/sbin/clearcache.sh" >> /etc/crontab

Use free -m to observe free memory before and after the reset, the task is also logged under /var/log/clearcache.log and can be output to stdout with “tail /var/log/clearcache.log”.

clear cache
clear cache