Monday, 20 August 2012

Backup your website easily


  • Difficulty: Easy
  • Application: Backups
If you want to back up a directory on a computer and only copy changed files to the backup computer instead of everything with each backup, you can use the rsync tool to do this. You will need an account on the remote computer that you are backing up from. Here is the command:

rsync -vare ssh jono@192.168.0.2:/home/jono/importantfiles/* /home/jono/backup/

Here we are backing up all of the files in /home/jono/importantfiles/ on 192.168.0.2 to /home/jono/backup on the current machine.

Running multiple X sessions


  • Difficulty: Easy
  • Application: X
If you share your Linux box with someone and you are sick of continually logging in and out, you may be relieved to know that this is not really needed. Assuming that your computer starts in graphical mode (runlevel 5), by simultaneously pressing the keys Control+Alt+F1 - you will get a login prompt. Insert your login and password and then execute:
startx -- :1
to get into your graphical environment. To go back to the previous user session, press Ctrl+Alt+F7, while to get yours back press Ctrl+Alt+F8.
You can repeat this trick: the keys F1 to F6 identify six console sessions, while F7 to F12 identify six X sessions. Caveat: although this is true in most cases, different distributions can implement this feature in a different way.

Creating Mozilla keywords


  • Difficulty: Easy
  • Application: Firefox/Mozilla
A useful feature in Konqueror is the ability to type gg onion to do a Google search based on the word onion. The same kind of functionality can be achieved in Mozilla by first clicking on Bookmarks>Manage Bookmarks and then Add a New Bookmark. Add the URL as:
http://www.google.com/search?q=%s
Now select the entry in the bookmark editor and click the Properties button. Now enter the keyword as gg (or this can be anything you choose) and the process is complete. The %s in the URL will be replaced with the text after the keyword. You can apply this hack to other kinds of sites that rely on you passing information on the URL.
Alternatively, right-click on a search field and select the menu option "Add a Keyword for this Search...". The subsequent dialog will allow you to specify the keyword to use.

Fix a wonky terminal


  • Difficulty: Easy
  • Application: bash
We've all done it - accidentally used less or cat to list a file, and ended up viewing binary instead. This usually involves all sorts of control codes that can easily screw up your terminal display. There will be beeping. There will be funny characters. There will be odd colour combinations. At the end of it, your font will be replaced with hieroglyphics and you don't know what to do. Well, bash is obviously still working, but you just can't read what's actually going on! Send the terminal an initialisation command:

reset

and all will be well again.

Replacing same text in multiple files


  • Difficulty: Intermediate
  • Application: find/Perl
If you have text you want to replace in multiple locations, there are several ways to do this. To replace the text Windows with Linux in all files in current directory called test[something] you can run this:
perl -i -pe 's/Windows/Linux/;' test*
To replace the text Windows with Linux in all text files in current directory and down you can run this:
find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt
Or if you prefer this will also work, but only on regular files:
find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'
Saves a lot of time and has a high guru rating!

Check processes not run by you in linux


  • Difficulty: Expert
  • Application: bash
Imagine the scene - you get yourself ready for a quick round of Crack Attack against a colleague at the office, only to find the game drags to a halt just as you're about to beat your uppity subordinate - what could be happening to make your machine so slow? It must be some of those other users, stealing your precious CPU time with their scientific experiments, webservers or other weird, geeky things!
OK, let's list all the processes on the box not being run by you!
ps aux | grep -v `whoami`
Or, to be a little more clever, why not just list the top ten time-wasters:
ps aux  --sort=-%cpu | grep -m 11 -v `whoami` 
It is probably best to run this as root, as this will filter out most of the vital background processes. Now that you have the information, you could just kill their processes, but much more dastardly is to run xeyes on their desktop. Repeatedly!

Master on Find Command in Linux


This command is an extremely handy tool for programmers in shell scripting and various other system administrative tasks. In fact, you will save a lot of time using the find command which would have otherwise been wasted trying to find the file. With the option of imposition of various criteria whilst searching for files, find is the ideal command to look for.
There are several versions of find e.g. POSIX find, AIX find, GNU find etc. Since we are concerned with Linux, this post will be based on GNU find.

1. Using find command
$ find myFile.txt
Search for myfile.txt in the current directory.
$ find . -name myFile.txt
Search for myFile.txt in the current directory and its sub-directories.
Here, ‘.’ represents the current directory.
You can specify many places to search, for e.g.
$ find  /home  /usr .  -name “*.txt”
Search all files with .txt extension in /home, current directory and /usr.
To search files without case sensitivity, use
$find . -iname myFile.txt

2. With Wildcards
$ find /home -type f -name myFile*
Search for all the files whose filename starts with myFile in the home directory and its sub-directories.
-type f : to search for files only
$ find /home -type d -name *john
Similarly, it will search for all the directories with directory name ending with john.
$find /home -type f -name [ldt]uck
It will search for files with filenames luck or duck or tuck in tje home directory and its sub-directories.
$find . -type f -name ?uck
By introducing ‘?’ in the above example, find command searches for a 4 digit filename whose initial letter can be any character.
Check out:
$find . -type f -executable -name f*ball
$find . -type f -name f*b*
$find . -type f -writable -name *woo*
$find . -type l -readable -name *.jpg   (-type l : for link )
3. With Date / Time
Find command permits you to search for files based on
(*) last data modification time ( mtime or mmin )
(*) last access time ( atime or amin )
(*) last status changed time ( ctime or cmin)
a. mmin / mtime
$ find /home -mmin -10
Search for all the files whose data was modified less that 10 minutes ago.
$ find /home -mmin 10
Search for files whose data was modified exactly 10 minutes ago
$ find /home -mmin +10
Search for files whose data was modified more than 10 minutes ago
Similarly , if you use mtime instead of mmin, find command will search for files modified in 24 hour periods.
$ find /home -mtime -1
Search for files modified in the last 24 hrs.
b. amin / atime
$find /home -amin -10
Similarly, it will search for files that were accessed within 10 minutes ago.
$find /home -atime +10
Search for files accessed more than 10 days ago.
c. cmin / ctime
$find /home -cmin 10
It will list the files whose status (i.e. change in the ownership or access permissions) was changed exactly 10 minutes ago.
$find /home -ctime -10
Search for files whose status was changed less than 10 days ago.
Remember: You always have the option of combining these options.
$find /home -atime +2 -amin -10
(search for files that were were last accessed 2 to 10 minutes ago)
4.  –exec parameter
The -exec parameter defines what to do with the file. This is indeed a handy and an important option to learn.
$find /home -empty -exec rm {} \;
( search for empty files in home directory and its sub-directories and remove them using rm command )
$find /home -name “*.doc” -exec ls {} \;
( search for document files and list them );
$find /home -name “*.doc” -ok rm {} \;
( search for document files and remove them. But it will prompt a question mark after each file).
5. With Permissions
$find . -perm 644
(search for files with permissions 644 ie read and write permission for owner, read permission for group and other users.)
$find . -perm -644
( same as above but without regard to the presence of any extra permission bits, eg. the executable bit )
$find . -perm /444
( search for files which are readable by somebody ie. owner or group or anybody else )
Remember :      $find . -perm -440
$find . -perm -u+r,g+r
$find . -perm -u=r,g=r
All three commands search for the files which are readable by both their owner and their group.
Similarly,          $find . -perm /440
$find . -perm /u+r,g+r
$find . -perm /u=r,g=r
All three commands search for the files which are readable by either their owner or their group.
6. Operators
Listed in order of decreasing precedence:
a. ( expr )
$ find /home \( -size +200c \)
Search for files with size greater than 200 bytes (c is for bytes).
b. ! expr  :  True if expr is false
$ find /home \! -perm 644
It will not list the files or directories with permission 644.
c. expr1 expr2  :  expr2 is not evaluated if expr1 is false
$ find /home -perm 644 -size -2k
Search for files with permission 644 and size less than 2 kilobytes.
( expr1 -a expr is same as expr1 expr2
i.e. $find /home -perm 644 -a -size 2k  is same as
$find /home -perm 644 -size 2k )
d.  expr1 -o expr2 : expr2 is not evaluated if expr1 is true.
$find /home -size 6M -o -size 1G
Search for files which is 6 Megabytes or files with 1 Gigabytes.
e.  expr1 , expr2  : both expr1 and expr2 are always evaluated.
$find /home -name “*.doc” -print , -name “*.pdf” -print

7. With I/O Redirection and Pipes
$find / -size +4M > list_of_files.txt
Search for files greater than 4 Megabytes and redirect the standard output to the file list_of_files.txt.
$ find /home -size +4M | wc -l
Search for files greater than 4 Megabytes and count number of files (by counting number of lines).

8. With Users and Groups
Search for files belonging to a user
$find / -user rabi “*.doc”
Search for files belonging to a group
$find / -group fortystones “*.doc”
Search for files that do not belong to any user
$find / -nouser “*.pdf”
Search for files that do not belong to any group
$find / -nogroup “*.pdf”

9.
$find / -newer mydoc.doc
Search for files that were modified more recently than mydoc.doc.
$find / -anewer mydoc.doc
Search for files that were last accessed more recently than file mydoc.doc was modified.
$find / -cnewer mydoc.doc
Search for files’ status was last changed more recently than file mydoc.doc was modified.
$find / -used 2
Search for files that were last accessed 2 days after its status was last changed.
10. With -print, -print0, -printf
$find / -name “*.pdf” -print
(print to the standard output, followed by a newline )
$find / -name “*.pdf” -print0
(print to the standard output, followed by a null  character instead of a newline )
$find / -name “*.pdf” -printf “%g %s %p\n”
Search for pdf files and print to the standard output, its group name , size in bytes and the filename. Here \n is for new line.
For more options regarding printf, refer manual page of find.
e.g. %a : file’s last access time
%c  : file’s last status change time
%d  : file’s depth in the directory tree
%m : file’s permission bits
%t   : file’s last modification time
%u  : file’s user name etc.
Until you do not play with the find command by going through its manual page, experimenting with its options, mixing these options together, you will not be able to use this wonderful command confidently.