0% found this document useful (0 votes)
8 views

Lab Basics

c basics

Uploaded by

bejjiprasad2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab Basics

c basics

Uploaded by

bejjiprasad2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

National Institute of Technology Karnataka

Department of Computer Science and Engineering

CS111 - Computer Programming Lab: Basic Commands

1 Introduction

This report will guide you through the essentials of using Linux commands,
Nano editor, and compiling and running a C program.

2 Basic Linux Commands


2.1 Listing Files and Directories

To list files and directories in the current directory, use the ls command:
1 ls

Changing Directories

To change the current working directory, use the cd command followed by the
path to the desired directory:
1 cd / path / to / directory

Creating Directories

To create a new directory, use the mkdir command followed by the name of the
new directory:
1 mkdir new_directory

2.2 Removing Files and Directories

To remove a file, use the rm command followed by the file name:


1 rm filename
To remove a directory and its contents, use the rm -r command:
1 rm -r directory_name

1
2.3 File Operations
Copying Files

To copy a file, use the cp command followed by the source file and the desti-
nation:
1 cp source_file destination_file

Moving Files

To move or rename a file, use the mv command:


1 mv old_filename new_filename

Viewing File Contents

To view the contents of a file, use the cat command:


1 cat filename

For longer files, you might use less or more:


1 less filename
2 more filename

2.4 File Permissions


Viewing File Permissions

To view file permissions, use the ls -l command:


1 ls -l

Changing File Permissions

To change file permissions, use the chmod command. For example, to set per-
missions to 755:
1 chmod 755 filename

3 Opening the Linux Terminal

To open a Linux terminal, follow these steps:


• Using a Keyboard Shortcut: Press Ctrl + Alt + T. This is a common
shortcut in many Linux distributions to open the terminal.

2
• Using the Application Menu: Open the application menu (often acces-
sible by clicking the icon in the lower-left corner of the screen), search for
Terminal or Console, and click on it.
• Using a Right-Click Option: In some desktop environments, you can
right-click on the desktop or within a file manager and select Open Terminal
or Open in Terminal.

4 Nano Editor

Nano is a simple, user-friendly editor available in Ubuntu and other Unix-like


systems.

4.1 Basic Commands

• Open a File: nano filename.c


– Opens filename in nano. Creates a new file if it does not exist.
• Save Changes: Ctrl + O (write out)
– Prompts for the filename. Press Enter to confirm.
• Exit nano: Ctrl + X
– Exits the editor. Prompts to save changes if there are unsaved modifi-
cations.
• Cut Text: Ctrl + K
– Cuts the current line or selected text. Press multiple times to cut
multiple lines.
• Paste Text: Ctrl + U
– Pastes the text cut using Ctrl + K.
• Undo Action: Ctrl + (underscore), then U
– Undoes the last action. Limited undo support.

4.2 Navigation Shortcuts

• Move Cursor:
– Arrow keys: Move the cursor in the direction of the arrow.
– Ctrl + A: Move to the beginning of the line.
– Ctrl + E: Move to the end of the line.
– Ctrl + Y: Move up one page.

3
– Ctrl + V: Move down one page.
• Scroll Through Document:
– Page Up and Page Down: Scroll up or down one screen at a time.

4.3 File Operations

• Save As (Write Out): Ctrl + O


– Enter a new filename to save the file under a different name.
• Open a New File:
– Open a new file directly by entering a new filename or using Ctrl + O
and specifying a new name.

5 Compiling C Code

To compile C code in the terminal, you will use the gcc compiler, which is the
GNU Compiler Collection for C language.

5.1 Creating a Simple C Program

Create a C source file named hello.c with the following content:


1 # include < stdio .h >
2

3 int main () {
4 printf ( " Hello , ␣ World !\ n " ) ;
5 return 0;
6 }

5.2 Compiling the C Program

To compile the C program, open the terminal and navigate to the directory
containing hello.c. Use the following command:
1 gcc -o hello hello . c

Here, -o hello specifies the output file name, which will be hello.

5.3 Running the Compiled Program

After compilation, run the program using:


1 ./ hello

4
This should display:
1 Hello , World !

You might also like