Wednesday, July 10, 2013

Useful Commands [Learn ubuntu/Linux with me -Day 3]

Boring Thoery:

What is a command?
A command can be given in any of these ways
  1. An executable program [files located in usr/bin directory] .
  2. Any C,C++,Python Program.
  3. Any builtin command [Ex:exit,cd]
    wait a sec how do i know if a command is builtin or not??
    There's a command which can do that for you.type <command>
    Ex: type cd  ,type exit (try these in your terminal)

    cam@hac3er:~$ type cd
    cd is a shell builtin
    cam@hac3er:~$ type exit
    exit is a shell builtin
    Interested to know more about builtins check these links
    Bash Manual 
  4. A Shell Function
  5. An alias

Interesting Part:


type – Display A Command's Type:

The type command is a shell builtin that displays the kind of command the shell will
execute, given a particular command name. It works like this:
Useage:type <command>
where “command” is the name of the command you want to examine. Here are some.Try these in your terminal
examples:

cam@hac3er:~$ type cd
cd is a shell builtin
cam@hac3er:~$ type exit
exit is a shell builtin
cam@hac3er:~$ type ls
ls is aliased to `ls --color=auto'
cam@hac3er:~$ type cp
cp is /bin/cp
cam@hac3er:~$ type mv
mv is /bin/mv

You can see different outputs which are self explanatory.cp is a command which is at a given location you can find cp at that location.

which – Display An Executable's Location: 

Usage:which <Executable>
 Just try these you'll understand

cam@hac3er:/$ which ls
/bin/ls
cam@hac3er:/$ which cd
cam@hac3er:/$ which cp
/bin/cp
cam@hac3er:/$ which mv
/bin/mv
cam@hac3er:/$ which exit
cam@hac3er:/$



Remember which works for only executables i mean the scripts written in a programming language.If you try to invoke it on builtins then it may throw an error or displays nothing.

help – Get Help For Shell Builtins:

You can get help from terminal by typing this command.you'll not understand a bit if you are a starter.
Usage:  help <comand>
bash has a built-in help facility available for each of the shell builtins. To use it, type “help” followed by the name of the shell builtin. For example:
cd is a builtin


cam@hac3er:/$ help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.
  
    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
    .........................

    .........................(you'll get half a page of text)


Hey what if i need to get help for non builtins like cp or mkdir??
That would be a good question
Usage: Executable --help
Executable means like cp,mv,mkdir =>these are scripts remember??


cam@hac3er:/$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z, --context=CTX  set the SELinux security context of each created
                      directory to CTX
      --help     display this help and exit
      --version  output version information and exit

Report mkdir bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'mkdir invocation'


cam@hac3er:/$ cp --help


Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
.............................trimmed I got a huge help text..................



man – Display A Program's Manual Page:

Most executable programs intended for command line use provide a formal piece of documentation called a manual or man page. A special paging program called man is used to view them.
Usage: 1)man <command>    Ex: man ls
            2)man section search_term    Ex: man 5 passwd



I didn't get the second syntax.
Then go through this boring theory but it's worth it
man uses less to display[remember ??less is used to view files,we discussed that in our previous posts]
man is divided into several sections so that is why you need to provide a section number for that
  Section       Contents
   1                 User commands
   2                 Programming interfaces kernel system calls
   3                 Programming interfaces to the C library
   4                 Special files such as device nodes and drivers
   5                 File formats
   6                 Games and amusements such as screen savers
   7                 Miscellaneous
   8                 System administration commands
Sometimes we need to look in a specific section of the manual to find what we are looking for.


apropos – Display Appropriate Commands:

Usage: apropros search_term
A simple search utility of command line.

cam@hac3er:/$ apropos floppy
fdformat (8)         - low-level format a floppy disk
mbadblocks (1)       - tests a floppy disk, and marks the bad blocks in the FAT
mformat (1)          - add an MSDOS filesystem to a low-level formatted floppy disk
mxtar (1)            - Wrapper for using GNU tar directly from a floppy disk

 

whatis – Display A Very Brief Description Of A Command:
The whatis program displays the name and a one line description of a man page
matching a specified keyword:
Usage: whatis cd

cam@hac3er:/$ whatis cd
cd: nothing appropriate.
cam@hac3er:/$ whatis ls
ls (1)               - list directory contents
cam@hac3er:/$ whatis mkdir
mkdir (1)            - make directories
mkdir (2)            - create a directory
cam@hac3er:/$ whatis cp
cp (1)               - copy files and directories
cam@hac3er:/$ whatis mv
mv (1)               - move (rename) files
cam@hac3er:/$



Creating Your Own Commands With alias:
Hey one thing that's irritating is that there is no command on my name :(
Let's create one.
One common thing i do is i often change my directory to desktop.. and give a command ls...this is a regular task i do.what if i combine both of them i.e changing directory and displaying it's contents.wow that's cool
Usage:alias yourname(somename)= <'command1;command2'>


You need to type properly and note that commands will be in between singlequotes
right click on a directory if you're not sure about the path of any directory.

cam@hac3er:/$ alias ajay='cd /home/cam/Desktop;ls;'
 

cam@hac3er:/$ ajay
1.html  2.py  comp_game.py   fb_pp.py  fb.py~  got.py~        Sublime Text 2
1.py    ajay  comp_game.py~  fb.py     got.py  ioredirection  test.py


cam@hac3er:~/Desktop$ type ajay
ajay is aliased to `cd /home/cam/Desktop;ls;'
 

cam@hac3er:~/Desktop$ unalias ajay
 

cam@hac3er:~/Desktop$ type ajay
bash: type: ajay: not found

 


To see all the aliases defined in the environment, use the alias command without
arguments.To remove that alias use:unalias name_of_the_alias


we'll it's too much for a post.Chek these links and keep typing in your terminal.
Bash Reference >>>Bash FAQ   >>>GNU Documentation >>Wikipedia

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment