Skip to main content

Basic commands used in Linux

Hey Guys, I am a newbie to linux. So started learning linux. Here I am sharing basic commands which are required to perform basic operations in linux.


Common commands useful for beginners-


1. man - man command is used to display manual pages called "man pages" .  Manual pages provides detail documentation about commands and other aspects of the system, including configuration files, system calls, library routines and the kernal (core of OS).


 e.g. $  man ls                                                  provides information about "ls" command.

Other commands line "whatis" and "whereis" can also be used to access information about maual pages.
whatis display description of a manual page while whereis  display location of manual page.

2. pwd - it is an acronym for print working directory. The basic use of pwd  is to find the full path to the current directory 
                 e.g $  pwd
                 /root

3. cd - this command is used to change current directory. e.g $  cd /etc
On typing cd and cd ~ your current directory will change to home directory. "cd .." can be used to go at parent directory while "cd -" can be used to go at previous directory.

4.  ls - this command is used to list contents of the directory. 
                 e.g. ls
                 root.sh    name.txt
     
"ls -a" option can be used to show all the files including hidden files of the directory. (hidden files are have name starting with a dot)
"ls -l" or "ls -hl"can be used for long listing of the file.

5. mkdir - this command is used to create directory. When used with "-p" then directory will be created along with required parent.
e.g 
$ mkdir etc                                                         Here directory etc will be created.
  
$   mkdir -p etc/var                                            Here both directories are created.

6. rmdir - this command is used to remove empty directory. Also "rmdir -p" can be used to recursively remove directories.

7. file - this command is used to determine the type of file. 
e.g $ file price.sh
price.sh : Bourne-Again shell script text executable

We can use "file *" to display information about all the files of current directory.

8. touch - this command can be used to create a new empty file. It can also be used to change the timestamps (i.e., dates and times of the most recent access and modification) on exisiting files and directories.
e.g. $ touch file1

A nice feature of touch command is that it does not overwrite existing files with the same name. instead of that, it changes the last access times for such files to the current time.

To get the details about options available with touch command use help. The complete timestamps for any file or directory can be viewed by using the stat command. 

9. rm - this command is used to remove file. e.g $ rm file1 
 Here the removed file cannot be restored back. So, we can use "rm -i" to ask confirmation before deleting the file forever from the system. "rm -rf" can be used to recursively remove files from the current directory. Here "f" means force. Be careful while using the -rf as it can erase entire file system accidentally.

10. cp - this command is used to copy file from source to target. 
e.g. 
$ cp file1 etc/                                              Here file1 will be copied to etc directory.
     
$ cp file1 file2 etc                                       Here file1 and file2 will be copied to etc directory. 

While copying multiple file the target should be directory.

We can use "mv" command to move file to another directory. The "rename" command can be used to enable renaming of many files at once. 
e.g. $ rename 's/txt/png' *.txt
Here all the files of current directory with extension txt will be renamed to png.

11. cat - it is one of the most frequently used command. It can be used for following purpose -
i) Display test files on screen
ii) Copy text files
iii) Combine text files
iv) Create new text files

e.g. 
$ cat file1 file2 file3              ( it will concatenate all these files)
$ cat > file1.txt
This is file1                          (file1 will be created. To save and exit press ctrl +d)
$ cat  file1.txt > file2.txt        (contents of file1.txt will be copied to file2.txt)


Linux is a free version of UNIX. The free part is not meant in money terms but rather that the 
source code for Linux is freely avialable for inspection, modification and what you feel you 
can/should do.
X-windows is a mode of Linux where your screen can be splits in small parts called Windows, that 
allow you to do several things at the same time and view graphics in a nice way.
In case of test terminal, monitor has only the capability to display text stuff, no graphics (or 
perhaps a very basic graphics dsiplay).

Linux stores data and programs in files. The part of the hard disk where you are authorised to 
save data is called your home directory.

The system has files of many different type. To find the type of a file do 
file file-name


In Linux files comes with permissions, a way to decide who can read, write (or execute) a file. 
To look at the permissions of a file you can use the command

ls -l file-name

a file with read and write permissions for the owner, read for the group and execute for others 
(a strange combination!) will show as
-rw-r----x

To change the permissions of a file or directory you can use the chmod command. Just put first 
the accounts to which you want to apply the changes (u for user or owner, g for group and o for 
others), then whether to grant (+) or remove (-) permissions and then r, w or x for the 
corresponding type of permission. For example
   chmod g+rw file-name
   chmod o-rw file-name
will grant read and write permissions to the group for file-name and remove those same 
permissions for all other users.

The su command, also referred to as substitute user, super user, or switch user, allows a computer operator to change the current user account associated with the running virtual console.
When run from the command line, su asks for the target user's password, and if authenticated, grants the operator access to that account and the files and directories that account is permitted to access.

john@localhost:~$ su - jane
Password:

jane@localhost:~$

Basics about MakeFile

Makefiles are a simple way to organize code compilation. A makefile is a special file, containing shell commands, that you create and name makefile. The makefile contains a list of rules. These rules tell the system what commands you want to be executed. Most times, these rules are commands to compile (or recompile) a series of files. 

A simple makefile consists of rules in the following manner:
target.... : prerequisites....
             recipe
             ......
Here target is usaually the name of a file that is generated by a program like names of executable or objects files. A target can also be the name of an action carry out, such as 'resolve'.
prerequisite is a file that is used as input to create a target and recipe is an action carried out while executing makefile using make command.

For example:

       test: test1.o test2.o                                                                           
             cc -o test1.o test2.o                                                                   
      test1.o : test1.c                                                                                 
                 cc -c test1.c                                                                          
      test2.o : test2.c test2.h                                                                      
                  cc -c test2.c                                                                         

Here, test is the name of executable. test1.o and test2.o are object files. 
test2.c uses test2.h header file.

variables in the makefile can be defined in the following manner  -
objects = test1.o test.2.o
test : $(objects)

A Makefile contain five kinds of things - explicit rules, implicit rules, variable definitions, directives, and comments.

Comments

Popular posts from this blog

Show Image on canvas HTML5

Here is the sample code to select image from system and display it on html5 canvas and javascript: <input type= "file" id= "selectedImage" /> <canvas id= "myCanvas" width= "500" height= "500" > </canvas> Javascript code: $( "#selectedImage" ).change( function (e) { var URL = window .URL; var url = URL.createObjectURL(e.target.files[ 0 ]); img.src = url; img.onload = function () { var canvas = document .getElementById( "myCanvas" ); var ctx = canvas.getContext( "2d" ); var imgSize = calculateAspectRatioFit(img.width, img.height, canvas.clientWidth, canvas.clientHeight); ctx.clearRect( 0 , 0 , canvas.width, canvas.height); ctx.drawImage(img, 0 , 0 , imgSize.width, imgSize.height); } }); function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) { var ratio = Math .min(maxWi

Pass byte array from C# to C++ and vice-versa

Pass byte array from C# to C++                           If you want to pass a byte array to native DLL as parameter, you can use the Intptr to do this, please check the demo below. /C++ API code: TestDLL_API void TestArrayPara (BYTE * pArray, int nSize) { for ( int i= 0 ; i<nSize; i++) printf( "%d\n" , pArray[i]); } //C# code: class TestClass { [DllImport(@"TestDll.dll")] public static extern void TestArrayPara (IntPtr pArray, int nSize); public static void Test () { byte [] array = new byte [ 16 ]; for ( int i = 0 ; i < 16 ; i++) { array[i] = ( byte )(i + 97 ); } int size = Marshal.SizeOf(array[ 0 ]) * array.Length; IntPtr pnt = Marshal.AllocHGlobal(size); try { // Copy the array to unmanaged memory. Marshal.Copy(array, 0 , pnt, array

Display Tooltip for Combo Box item C#.NET Winforms

In windows form combo box control sometimes while adidng items dynamically we have items whose width is greater than width of combox box control. In this case for making UI more user friendly we can show tooltip over such item. Here is the sample C# code to display such tooltip:  Add a Tooltip control on the form.  Add following code : this . combo_box1 . DropDownStyle = System . Windows . Forms . ComboBoxStyle . DropDownList; this . combo_box1 . DrawMode = DrawMode . OwnerDrawFixed; this . combo_box1 . DrawItem += new DrawItemEventHandler(combo_box1_DrawItem); this . combo_box1 . DropDownClosed += new EventHandler(combo_box1_DropDownClosed); this . combo_box1 . MouseLeave += new EventHandler(combo_box1_Leave); void combo_box1_DrawItem( object sender, DrawItemEventArgs e) { if (e . Index < 0 ) { return ; } string text = combo_box1 . GetItemText(combo_box1 . Items[e . Index