Linux file command with example

Linux file command with example

file command is used to determine the file type. It does not care about the extension used for file. It simply uses file command and tell us the file type. It has several options.

Syntax:

  1. file <filename>  

1) How to determine the file type of a file

# file Karun.txt 
File.txt: ASCII text

2)How to determine filesystem type of partition using file command
 the -s flag enables reading of block or character files .

# file –s /dev/sda1
/dev/sda1: SGI XFS filesystem data (blksz 4096, inosz 256
, v2 dirs)

cal command in Linux with example

  cal command in Linux with example 


About cal

The cal command displays a simple, formatted calendar in your terminal.
If no options are specified, cal display the current month in your terminal, with the current day highlighted.

1.   To Print Current month calendar

cal 

2.   Display previous/current/next month calendar using single command 

cal -3

     3. To Display particular month of the year calendar

cal -m 12

4.   Print number of days in year using -j option

cal -m 12

5. Display the calendar for December of this year.


cal -m 12
       6. To Display specified year calendar

cal 2010

How to set Hardware Clock Date Time using hwclock command

How to set Hardware Clock Date Time using hwclock command


hwclock is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). The hardware clock is independent of the operating system you use and works even when the machine is shut down. This utility is used for displaying the time from the hardware clock. 

Hardware clock is also called as BIOS clock. You can change the date and time of the hardware clock from the BIOS.

The hardware clock stores the values of: year, month, day, hour, minute, and second. It is not able to store the time standard, local time or Coordinated Universal Time (UTC), nor set the Daylight Saving Time (DST).

1. Display Hardware Clock Date and Time


Just type hwclock, which will display the date and time of your system’s hardware clock. This is the same date and time that you’ll see from the BIOS screen. You can also use option -r, or –show to display the date and time.
# hwclock
Sat 18 Aug 2018 08:26:12 AM PDT  -0.312862 seconds

# hwclock -r
Sat 18 Aug 2018 08:20:54 AM PDT  -0.109748 seconds

# hwclock --show
Sat 18 Aug 2018 08:21:12 AM PDT  -0.640982 seconds

2. Set Hardware Clock Date and Time Manually

To Set the Hardware Clock to a Specific Date and Time


The following will set the date to Aug 11th, 2013. Please note that if you don’t specify the time, it will be set to 12:00:00 AM.
# hwclock --set --date 8/19/2018

# hwclock
Sun 19 Aug 2018 12:00:04 AM PDT  -0.703489 seconds
You can also set both date and time at the same time as shown below. Please note that it will use the local time by default.
# hwclock --set --date "8/19/2018 23:10:45"

# hwclock
Sun 19 Aug 2018 11:10:48 PM PDT  -0.562862 seconds

3. Copy System Time to Hardware Time


You really want to make sure your hardware clock has the same date and time as the system clock. As you see currently there is a difference between the system clock and hardware clock time.
# date
Sat Aug 18 08:16:17 PDT 2018

# hwclock
Sat 18 Aug 2018 08:26:53 AM PDT  -0.687841 seconds
So, use the hwclock command as shown below to set the hardware clock same as system clock. 
hwclock --systohc 












How to set and view system date & time using date command

How to set and view system date & time using date command

date command is used to display the system date and time. date command is also used to set date and time of the system. 

    1.   To display the current date and local time, type the following at a shell prompt:
# date 
Mon Sep 17 17:30:24 EDT 2018
    
     2.   To display the current date and time in UTC, type the following at a shell prompt:
 # date -u
Mon Sep 17 17:30:24 UTC 2018
    
     3.   To change the current date & time, run the date command with the --set or -s option as root:
# date –s ’17-sep-2018 17:00’
   
     4.   To change the current time
# date –s 15:40:30 

  
Using - -date or –d option for displaying past dates:

· Date and time of 2 years ago.
Command:
$date --date="2 year ago"
Output:
Sat Oct 10 23:42:15 PDT 2019

· Date and time of 5 seconds ago.
Command:
$date --date="5 sec ago"
Output:
Tue Oct 10 23:45:02 PDT 2018

· Date and time of previous day.
Command:
$date --date="yesterday"
Output:
Mon Oct 9 23:48:00 PDT 2018

· Date and time of 2 months ago.
Command:
$date --date="2 month ago"
Output:
Thu Aug 10 23:54:51 PDT 2018

· Date and time of 10 days ago.
Command:
$date --date="10 day ago"
Output:
Sat Sep 30 23:56:55 PDT 2018

Using - -date option for displaying future date:

· Date and time of upcoming particular week day.
Command:
$date --date="next tue"
Output:
Tue Oct 17 00:00:00 PDT 2018

· Date and time after two days.
Command:
$date --date="2 day"
Output:
Fri Oct 13 00:05:52 PDT 2018

· Date and time of next day.
Command:
$date --date="tomorrow"
Output:
Thu Oct 12 00:08:47 PDT 2018

· Date and time after 1 year on the current day.
Command:
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2019

How to Set Time, Timezone and Synchronize System Clock Using timedatectl Command

How to Set Time, Timezone and Synchronize System Clock Using timedatectl Command


The timedatectl utility is distributed as part of the systemd system and service manager and allows you to review and change the configuration of the system clock. You can use this tool to change the current date and time, set the time zone, or enable automatic synchronization of the system clock with a remote server.

1. To display the current time and date on your system, use timedatectl command from the command line as follows:
~]$ timedatectl
      Local time: Mon 2016-09-16 19:30:24 CEST
      Universal time: Mon 2016-09-16 17:30:24 UTC
        Timezone: Europe/Prague (CEST, +0200)
              NTP enabled: no
NTP synchronized: no
RTC in local TZ: no

2. To view all available time zones, run the command below:
timedatectl list-timezones

3. To set your local timezone in Linux, we will use set-timezone switch as shown below.
# timedatectl set-timezone “Asia/Kolkata”

 4. To set the date and time on your system, using the timedatectl command as follows:
# timedatectl set-time 15:58:30

5.To set date only, we can use set-time switch along the format of date in YY:MM:DD (Year, Month, Day).
# timedatectl set-time 2018-11-20

6. To set both date and time:
# timedatectl set-time '2018-11-20 16:14:50'

7. To start automatic time synchronization with remote NTP server, type the following command at the terminal.
# timedatectl set-ntp yes

8. To disable NTP time synchronization, type the following command at the terminal.
# timedatectl set-ntp no






Linux Introduction


                   Linux Introduction


History of Unix:

           

History of Unix

Unix is also an OS.
*    Unix is a Command line OS written in the C Programming language.
*    Started in 1965 by AT & Bell labs.
*    Created by Dennis Ritchie & Ken Thompson in 1969.
*    Written in 100% C language and released in 1973.
*    Support only CLI mode.

UNIX Flavors:
Before Linux

*    In 80’s, Microsoft’s DOS was the dominated OS for PC
*    Apple MAC was better, but expensive
*    UNIX was much better, but much, much more expensive. Only for minicomputer for commercial applications.
*    People was looking for a UNIX based system, which is cheaper and can run on PC.
*    Both DOS, MAC and UNIX were proprietary, i.e., the source code of their kernel is protected
*    No modification is possible without paying high license fees
   Beginning of Linux:

*   A famous professor Andrew developed Minix, a simplified version of UNIX that runs on PC
*   Minix is for class teaching only. No intention for commercial use.

  History of Linux

*   Linux is an open source operating system.
*   Linux kernel is developed by Linus Torvalds in 1991.
*   Linux is an update version of Unix OS.
*   Linus Torvalds creates open source, UNIX-like kernel, released under the GPL (General Public License). 
*   At that time Linus Torvalds was a graduate student from Helsinki university.
*   Written in C & C++ Languages.
*   Support CLI & GUI modes.
*   Linux version 0.0.1

Developer of Unix,Minix & Linux




Feature of Linux:


                                                                    Multi-user
Allow multiple users to login and use the resources at the same time.
Multi-processing
Allow to performs multiple process at the same time.
Multitasking
Capable of running multiple applications and process at the same time.
Portability
Can be installed on all hardware architecture.
Reliability
Large servers have been successfully being running without a single second of down time.
Security
Inbuilt firewall (iptables) and SELinux.
Open Source
Free Software along with the source code and documentation.


Linux Architecture:


KERNEL:

Kernel is a computer program that manages I/O (input/output) requests from software, and translates them into data processing instructions for the central processing unit and other electronic components of a computer. The kernel is a fundamental part of a modern computer's operating system.

Work of Kernel: -                                                                 
*   Schedules programs
*   Manages data/file access and storage
*   Enforces security mechanisms
*   Performs all hardware access
*   Provides access to Shell

Kernel Diagram




  
 SHELL:

A shell is a program that provides the traditional, text-only user interface for Linux and other Unix-like operating systems.

Work of Shell: -
*   Presents each user with a prompt
*   Interprets commands types by a user
*   Executes user commands
*   Supports a custom environment for each user

Shell Diagram































APPLICATION & Utility: 

*   Open Office: word processing, spreadsheets, drawing
*   Adobe Acrobat Reader
*   Mozilla: GNOME web Browser
*   Kmail: The KDE Mail Application
*   Evolution: An Email and Calendar Program
*   X-CD-ROAST: CD/DVD Burning Application
*   Sound Applications 


Type of Login User:


When a shell is used interactively, it displays a string when it is waiting for a command from the user. This is called the shell prompt. When a regular user starts a shell, the default prompt ends with a $ character.

The $ is replaced by a # if the shell is running as the super user, root.


Virtual Console:
Users access the bash shell through a terminal. A terminal provides a keyboard for user input and a display for output. On text-based installations, this can be the Linux machine's physical console, the hardware keyboard and display. Terminal access can also be configured through serial ports.

Another way to access a shell is from a virtual console. A Linux machine's physical console supports multiple virtual consoles which act like separate terminals. Each virtual console supports an independent login session.

If the graphical environment is available, it will run on the first virtual console in Linux 7. Five additional text login prompts are available on consoles two through six (or one through five if the graphical environment is turned off). With a graphical environment running, access a text login prompt on a virtual console by holding Ctrl +Alt and pressing a function key (F2 through F6). Press Ctrl+Alt+Fl to return to the first virtual console and the graphical desktop.