Best Linux Tips, Tricks And Command
Lines
1.
Find out the elapsed time of a running
process
There are a lot of processes running on your Linux system. Here is a command that will let you know how long the process has been running:
#ps -eo "%p %c %t"|grep "sshd"
In the above command %p is pid, %c is command and %t is elapsed time.
In response to the above command, you will get the following output:
2850 sshd 172-01:37:22
29532 sshd 125-09:07:10
There are a lot of processes running on your Linux system. Here is a command that will let you know how long the process has been running:
#ps -eo "%p %c %t"|grep "sshd"
In the above command %p is pid, %c is command and %t is elapsed time.
In response to the above command, you will get the following output:
2850 sshd 172-01:37:22
29532 sshd 125-09:07:10
2.
Changing file names from upper case to
lower
To manually change the case (upper to lower or vice versa) of a large number of files can be tedious. So, here is a script that can make life easy:
#to change uppercase filenames to lowercase
#!/bin/sh
if [ $# -eq 0 ] ; then
echo Usage: $0 Files
exit 0
fi
for f in $* ; do
g=`echo $f | tr "[A-Z]" "[a-z]"`
echo mv -i $f $g
mv -i $f $g
done
If you want to change the case from lower to upper, replace
g=`echo $f | tr "[A-Z]" "[a-z]"`
with
g=`echo $f | tr "[a-z]" "[A-Z]"` in the script.
To manually change the case (upper to lower or vice versa) of a large number of files can be tedious. So, here is a script that can make life easy:
#to change uppercase filenames to lowercase
#!/bin/sh
if [ $# -eq 0 ] ; then
echo Usage: $0 Files
exit 0
fi
for f in $* ; do
g=`echo $f | tr "[A-Z]" "[a-z]"`
echo mv -i $f $g
mv -i $f $g
done
If you want to change the case from lower to upper, replace
g=`echo $f | tr "[A-Z]" "[a-z]"`
with
g=`echo $f | tr "[a-z]" "[A-Z]"` in the script.
3.
Counting the number of files in a directory.
Here is a simple command that can count the number of files in a directory (not the hidden Counting the number of files in a directory
ones):
#echo * | wc –w
Here is a simple command that can count the number of files in a directory (not the hidden Counting the number of files in a directory
ones):
#echo * | wc –w
4.
How to check the date and time the
system was rebooted and booted.
Here is a simple command to check the system's reboot date and time:
#last reboot
reboot system boot 2.6.18-53.el5 Sat Aug 6 18:02 (8+04:45)
wtmp begins Sat Aug 6 18:02:07 2011
The command below will give you the date and time the system was booted:
#who -b
system boot 2011-08-24 09:43
Here is a simple command to check the system's reboot date and time:
#last reboot
reboot system boot 2.6.18-53.el5 Sat Aug 6 18:02 (8+04:45)
wtmp begins Sat Aug 6 18:02:07 2011
The command below will give you the date and time the system was booted:
#who -b
system boot 2011-08-24 09:43
5.
For Securing files.
Here is a simple tip to password protect your files:
#vi -x test
This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
Now, whenever you open this file, it will ask for that password first.
Here is a simple tip to password protect your files:
#vi -x test
This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
Now, whenever you open this file, it will ask for that password first.
6.
Finding the full path of the shell
command.
There is a command named which that takes one or more arguments as input. It prints to standard output the full path of the shell command. It does this by searching for an executable or script in the directories listed in the environment variable PATH:
#which poweroff
/usr/bin/poweroff
If the command is not found, it gives the output shown below:
#which moodule
/usr/bin/which: no moodule in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/aarsh/bin)
There is a command named which that takes one or more arguments as input. It prints to standard output the full path of the shell command. It does this by searching for an executable or script in the directories listed in the environment variable PATH:
#which poweroff
/usr/bin/poweroff
If the command is not found, it gives the output shown below:
#which moodule
/usr/bin/which: no moodule in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/aarsh/bin)
7.
Power yourself with Netstat
Here are a few uses of the netstat command that can help you.
Here are a few uses of the netstat command that can help you.
To display the kernel interface
table:
#netstat -i
#netstat -i
To display the kernel routing table:
#netstat -rn
To display all open network sockets:
#netstat -uta
To display network statistics:
#netstat -s
8. Print a file with line numbers
If you want a file with line numbers (say for printing), you can use the 'nl' command in Linux:
#nl file.c
This prints the file with line numbers to standard output or this can be even redirected to afile as shown below:
#nl file.c > output.txt
Here, output.txt will have the codes of file.c with each line having a line number.
#netstat -rn
To display all open network sockets:
#netstat -uta
To display network statistics:
#netstat -s
8. Print a file with line numbers
If you want a file with line numbers (say for printing), you can use the 'nl' command in Linux:
#nl file.c
This prints the file with line numbers to standard output or this can be even redirected to afile as shown below:
#nl file.c > output.txt
Here, output.txt will have the codes of file.c with each line having a line number.
9.
Search and delete files from a folder.
If you want to delete all the .lock files from a folder, use the following command:
#find -name *.lock | xargs rm -rf
This will find all the files with the .lock extension and delete them. This can be done for any files that you need to delete.
If you want to delete all the .lock files from a folder, use the following command:
#find -name *.lock | xargs rm -rf
This will find all the files with the .lock extension and delete them. This can be done for any files that you need to delete.
10. Scan open ports
The command given below will scan all the open TCP ports on the loopback interface:
#nmap -sS -O 127.0.0.1
In general, you can use the following:
#nmap -sS -O
To scan open UDP ports in the system, use the command given below:
#nmap -sU -O
The command given below will scan all the open TCP ports on the loopback interface:
#nmap -sS -O 127.0.0.1
In general, you can use the following:
#nmap -sS -O
To scan open UDP ports in the system, use the command given below:
#nmap -sU -O
No comments:
Post a Comment