Saturday, January 29, 2011

BASH commands : sleep, time, ps, top, tail, date, expr, seq, jobs, df

1. sleep 2 - sleps for 2 seconds
2. usleep 2000000 - sleeps for 2 millions micro seconds
3. which time - /usr/bin/time - to see how much time your script run
Example: time sleep 3
Returns:
real 0m3.006s
user 0m0.000s
sys  0m0.000s
time ls - return the time taken for "ls" command to run
4. ps -aux - show all processes from all users
ps -aux | grep bash - return all bash instances that are running
5. top - snapshot picture view of the current process, CPU, memory which is refreshed
watch tail /var/log/messages - view messages real time, see all the time the bottom of the file
time tail /var/log/messages - return the time taken for tail
6. date - prints the current date
date -r file - returns the last modified date of the file
date -r file +%s - return on UNIX the last modification time in seconds from 1970
date +%s - return the seconds that past from 1907
date +%d - returns the date
date +%d-%T - return the date-hour:minute:second
date +%d-%T-%b - return the date-hour:minute:second-Month
date +%F - returns 2011-1-29
touch 'date +%F'.log - creates a file 2011-1-29.log
echo "success" >> 'date +%F'.log - write 'success' into file
cat 2011-1-29.log - return the line from the file "success"
rm -rf 2011-1-29.log - delete the file
date +%N - generates a random number
7. man expr
which expr - return /usr/bin/expr - exaluates expresions arithmitec, comparison for integers strings, lenght, extract a substring
Example: expr 10 + 10 - returns 20
expr 10 * 10 - returns ERROR
expr 10 \* 10 - returns 100
expr 10 / 10 - return 1
Example:i=1
echo $i
i='expr $i + 1'
echo $i - return 2
Example:expr test = test; echo $? - return 1 in boolean which is correct
expr test1 = test2; echo $? - return 0 in boolean which is incorrect
expr length testString - returns 10
expr substr testString 1 3 - returns "tes"
expr substr testString 1 5 - returns "testS"
Example:var1=string1
expr substr $var1 1 3 - returns "str"
expr length $var1 - returns 7
8. seq 10 - display at standard output all the numbers from 0 to 10
seq 0 9 - display at standard output all the numbers from 0 to 9
seq 9 - display at standard output all the numbers from 1 to 9

seq 100 > file1.data -> file1.data will contain numbers from 1 to 100
seq 10000000 > file1.data ->file1.data will contain numbers from 1 to 1000000 this will take some time
CTRL+Z - sends a job to the backgound. in oder for the job to continue type "fg" and brings the job to foreground
jobs - display the commands that where stopped with CTRL+Z
CTRL+C - KILL the job. In this case command jobs won't display any command stoped with CTRL+Z
ls -lh file - display the file with the size in mega bytes
"seq 100000 > file.data&" OR "seq 100000 > file.data &" - sends the job to the backgound
watch ls -l file.data - watch how the file grows in time

9. df -h - displays the disk left on your computer

No comments:

Post a Comment