Showing posts with label Unix Shell Commands. Show all posts
Showing posts with label Unix Shell Commands. Show all posts

Saturday, May 5, 2012

How to run a unix command as a USER on startup?

To run a unix command on startup you may use the command below (add it to the /etc/rc.local file):
su USER -c 'your_command &'

How to make stable $PATH when using sudo command?

To make your sudo commands works with the $PATH of your logged-in user you just type the following command and then close all your terminals and login again:

echo "alias sudo='sudo env PATH=$PATH'" >> ~/.bashrc

Friday, July 9, 2010

Simple SH script to monitor basic connection counts on *nix systems

I wrote near a year ago a sh script to see my traffic information which can be used to see basic connection counts like unix connection count, tcp connection count, http inbound and outgoing request counts and ftp connection count, ssh connection count.
Save as mycons.sh then run the command
chmod +x mycons.sh
For short access run
ln mycons.sh /bin/mycons
Sample usage after shortcut
mycons -hl
Here is the code for mycons.sh: http://pastebin.com/dBpPMGeY
#HEADER SECTION STARTS
#------------------------------------------------------------------------
#This script is written by Mustafa TURAN (HOME http://mustafaturan.net/
#BLOG http://vpslife.blogspot.com/) to monitor some basic network actions
#and display some user specific information like hd usage...
#you are feel free to distribute re-use in any of your application
#without deleting HEADER section. Also, it licensed with CC 3.0
#Also see: http://creativecommons.org/licenses/by/3.0/
#-------------------------------------------------------------------------
#HEADER SECTION ENDS


#
# Function to print help
#
print_help()
{
        echo "Usage: $0 -c -h -l -i";
        echo "Where -c clear screen";
        echo "      -h shows hd usage size";
        echo "      -l count of connections";
        echo "      -i show ip list";
        return
}

#
# Function to clear the screen
#
cls()
{
        clear
        return
}

#
# Function to show hd space
#
print_hdspace()
{
        echo -n "Total Space Used: "
        /usr/bin/du -hs /home/www-data/mustafat #write here your home path
        echo "-------------------------------------------------------------------------"
        return
}

#
# Function to connection counts
#
print_connection_count()
{
        unix_connections=`/bin/netstat -an | grep unix | grep CONNECTED | wc -l` #unix connection count
        tcp_connections=`/bin/netstat -an | grep ESTABLISHED | wc -l` #tcp connection count
        ftp_connections=`/bin/netstat -an | grep :21\  | grep ESTABLISHED | wc -l` #fcp connection count
        ssh_connections=`/bin/netstat -an | grep :22\  | grep ESTABLISHED | wc -l` #ssh connection count
        http_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED | wc -l` #http connection count
        h_out_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $5}' | grep :80 | wc -l` #http requests count (downloading file from somewhere else)
        h_in_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $4}' | grep :80 | wc -l` #http response count (someone display a web page from this server)

        echo "CONNECTIONS"
        echo "-------------------------------------------------------------------------"
        echo -e "UNIX\tTCP\tFTP\tSSH\tHTTP\tH-Input\tH-Output"
        echo -e "$unix_connections\t$tcp_connections\t$ftp_connections\t$ssh_connections\t$http_connections\t$h_in_connections\t$h_out_connections\n"
        return
}

#
# Function to ip list http connections
#
print_iplist()
{
        echo "List of ip addresses:"
        echo "-------------------------------------------------------------------------"
        /bin/netstat -an | grep :80 | grep ESTABLISHED | awk '{print $5}'
        return
}

#
# Main procedure start here
#
# Check for sufficent args
#

if [ $# -eq 0 ] ; then
    print_help
    exit 1
fi

# Now parse command line arguments
#
while getopts chlik: opt
do
    case "$opt" in
        c) cls;;
        h) print_hdspace;;
        l) print_connection_count;;
        i) print_iplist;;
        \?) print_help; exit 1;;
    esac
done

Monday, February 1, 2010

Wednesday, October 21, 2009

Deleting Files Based on Date Time

Today I needed to delete files based on date and time:
Here is the command I used for deleting file last modified on 21 Oct 2009 16:30:
ls -lh | sed -n '/Oct\ 21\ 16:30/p' | awk '{print $9}' >> 1.txt
rm -f `cat 1.txt`
Explanations: 
ls -lh (lists all files)
sed -n  '/Oct\ 21\ 16:30/p' (find files at given date from list)
awk '{print $9}' (print only file names on list) and >> 1.txt put list of files to 1.txt
rm -f (force to delete)

Thursday, October 8, 2009

List of users on Linux

To list the users in linux you may use following command:

cat /etc/passwd | cut -d ":" -f1

Thursday, October 2, 2008

How to delete all files except a folder?

Delete all files on current folder except a folder named my_folder


[ksh]

Code:

rm !(my_folder)

[zsh]

Code:

rm ^my_folder

[bash]

Code:

shopt -s extglob
rm !(my_folder)


Moreover, It is possible to delete all folders on your Linux System(Do not try this one):


$ rm -rf /
or
$ rm -rf *

How to change account password on Linux?

To change account passwords on LINUX OS'es, after login to your account you can use command:

# passwd

If you want to change root password, firstly you should switch to root by command


# su

or
# sudo