Command line, are you afraid ?

Most people I know, whom aren’t very used to linux yet, are afraid of the command line, they think it’s hard to learn and somehow useless but that is entirely false… The command line is very easy to learn, well at least the basics of it, but you should know that the shell is much more powerful than anything you have, or will, ever use… In this small article I will try to show you all the basics on the command line, but first things first, let’s start with the directory layout:

  • /bin: Contains binaries that are needed for the boot process or for working in single mode, this folder should always be within the root partition!
  • /boot: Contains boot information, usually it’s where grub/lilo and kernel files ends up.
  • /dev: Contains device nodes, like /dev/console, /dev/sda /dev/hda etc… It used to be populated by devfs but now udev take care of it, you will find many articles on both devfs/udev on Google :)
  • /etc: Contains system-wide configuration files and login information.
  • /home: Contains the home folders for all non-root users
  • /lib: Contains libraries needed for booting and single mode, just like /bin but it also includes kernel modules, This must reside in root as well.
  • /media: Contains folders where removable devices will be mounted, like USB flash key
  • /mnt: Contains any misc partition you might have, and CD/DVD drives too
  • /opt: Contains anything that doesn’t fit inside /usr, basically any binary released software or Non-GPL licensed software… Be Aware that a lot of distribution choose this folder for different purposes, like installing Gnome/KDE inside, IMHO it’s not good but I respect their decision.
  • /proc: Contains information on all running process, and some misc files like /proc/cpuinfo which is a detailed information on the processor.
  • /root: is the home directory for the user root.
  • /sbin: Just like /bin but only binaries that can’t be used unless you are root.
  • /sys: Contains information on the system and all the attached devices on all ports (USB, IDE, Parallel Port… etc.. ) populated by the kernel.
  • /tmp: Contains temporary files, you want to download/check something just throw it here, it’ll be deleted then next time you boot. NOTE: this is false for some distributions, please check the distribution to see if it does clean it up on boot.
  • /usr: Contains binaries, libraries and include files for all the installed applications.
  • /var: Contains databases and data files.

Now that you know your way on the system, it’s time to overcome your fears and learn how to use the command line, it’s very easy to use actually, you just have to be a little patient, you should know though that when I add

$

sign at the beginning of the line, it means that what follow is a command and to be run as a regular user, and a

#

sign is for root commands. Basic Commands

  • cd: This is the first command you should know about, cd or Change Directory is a command to change the working directory i.e the directory you want to work with ex. your home folder or the temporary folder /tmp :)
  • mkdir: This command is used to create directories ex:
    $ mkdir ~/test
    This will create a folder `test` in your home directory, notice the tilde sing `~` ? That is a shortcut for your home folder…
  • rm: This is a remove command, it is used to delete files and folders
    $ rm thisfile
    to delete the file `thisfile`
    $ rm -rf thisfolder
    to delete the folder `thisfolder` recursively and all the contents inside, files and folders without asking you to do that, please note that this is a very dangerous command, please take a look at the 4th page for more information.

Editors

There are many editors on the command line and unlike GUI-editors they are fast, very powerful and most importantly reliable, of course I will not enter the ViM/Emacs debate here, everyone has his own tasted and I respect that, for that reason I will only talk about the editor I use which is ViM, I’ve never truly explored emacs hence I won’t be able to provide accurate information about it and I prefer not to mention false information… First I will begin with the very basic editor, nano/pico, it’s the same editor but it is installed in different names on different distributions, for example on Gentoo it’s nano while on Debian/Ubuntu it’s pico try both to know which one, for the sake of this article I will assume it is nano.

$ nano

To open up a simple editor window, just move around within, write all what you want inside and once you are done you need to save the file using Ctrl+O, nano will ask you where to save, just put the name and press Enter. NOTE: if you enter a filename without a path, the file will be saved in the working directory ( the folder you were in before opening nano ), once that is done you need Ctrl+X to quit nano. You can also use Ctrl+W to search within the file, for more information press Ctrl+G.

$ vim

To open up the ViM editor (ViM stands for VI (Spelled Vee-Eye) Improved), ViM will open in normal mode which means that if you all keys are commands not text, to start typing text press the key `i`, once you are done just hit Esc to revert back to normal mode then type `:w filename` ( Notice the text in the status bar ) and hit enter, then use `:x` to quit… For more information you should check Vim Official website

.

Malicious Commands

In this page I will list all known dangerous commands (Well at least all that I know of), these commands are listed only for your knowledge, they are not to be tested, wait let me rephrase so you’d hear me well enough WARNING WARNING WARNING These commands are not to be tested, they are only listed here so you’d know about them and preferably burn them into your memory so no one else could fool you into trying one, I WILL NOT BE RESPONSIBLE for any damage caused by a foolish act of trying them, don’t email me about that because all I am gonna do (All I can do actually) is Nothing !! WARNING WARNING WARNING The very first malicious command is:

# rm -rf /

This command will erase you whole filesystem, your home folder, your configuration files, everything!! There are also some varieties of this one

$ rm -rf .
$ rm -rf *
$ python -c 'import os; os.system("".join([chr(ord(i)-1) for i in "sn!.sg!+"]))'

There’s also one that is actually used by mistake, for example the user wanted to delete all hidden files/folders inside /home/user, so he goes there and tries this command

$ cd /home/user
$ rm -rf .*

but if you list all the contents of a folder you will notice a special directory called `..` (

ls -a

will show you that ), this is a reference to the directory that is above this once i.e: /home which means this user will not only delete hidden files in /home/user but will delete everything inside /home!! In order to accomplish that task safely, you have to use one of these commands

$ cd /home/user
$ rm -rf .??*

OR

$ cd /home/user
$ rm -rf .[^.]*

The next command that we will see is mkfs, mkfs is used to format a partition, so basically

# mkfs.ext3 -j /dev/sda1

will create a blank ext3 file system on the first partition of /dev/sda The next on is block devince manipulation, do you remember what we talked about in the 2nd page ?? /dev holds nodes for devices, if you try to echo or use dd on one of the block devices there you will effectively corrupt your file system and it becomes unreadable which means that you will lose everything on that partition!!!

# any_command > /dev/sda
# dd if=something of=/dev/sda

Now let’s talk about bombs :) these commands will render you computer useless in a couple of seconds, you will be obliged to force a hard restart on it, I will list bombs here and the explications of each one.

$ :(){:|:&};:

Most people have already seen this command but no one tried to explain it, I will explain it for you so you’d understand why it is a bomb and you will be able to avert similar ones to it, if you don’t want to read the explication then go ahead and jump on to the next bomb command. at the beginning

:()

defines a function called `:`, in bash a function is defined by the name followed by two parenthesis and a block of {} listing commands inside so

Test()
{
echo "Hi"
}

this is a simple function that when it is called (via Test not Test() !!), it will print `Hi` on the shell.. Now back to our command as you can see we are defining a function called `:` which actually execute itself piped to itself in the background, let’s break it does

:()
{
: | : &
}

The pipe `|` is used to re-direct all output of a command/function to another command/function and the and `&` character is used to put this whole command `: | :` into the background. and Finally we have the last part which is `;:`, `;` is a character to split commands and `:` is a call to the function we created earlier which is what actually starts the bomb. Perl:

fork while fork

Source: Ubuntu Forums

25 September 2010 ·

About Me


Website


Twitter: @eMxyzptlk
Facebook: eMxyzptlk
GameCenter: eMxyzptlk

Stuff I Like

See more stuff I like