File operations in linux using simple commands

Once you are equipped with the basic commands in linux, the next step will be learning file operations in linux using commands. For a primer on basic commands check linux basic commands.

Have you ever tried to copy,paste,move or remove the files in your linux system using commands? Tough eh? Not really!

file operations in linux

Lets begin with the commands on how to create files and directories.

 

 

FILE OPERATIONS IN LINUX

*Creating empty file

# touch file1

This command creates an empty file file1

*creating a file with text content

# cat > file1
…input line1
…input line 2

To save the file press CTRL+D

*View the file content

# cat file1

Opens the file and shows the contents

*Append (add lines to the bottom) the file with more number of input lines

# cat >> file1
…new input line

Save with CTRL +D

*Creating a directory

# mkdir dir1

The above command creates a directory dir1 . Let me remind you a directory is similar to the folder.

*Creating multiple directories in a single line of command

# mkdir dir1 dir2 dir3

It will create three directories dir1 dir2 and dir3

Note: While creating a directory which has space between the names, use double quote(” “). For example, “my directory”.

*Copying the file

# cp file1 dir1

The command above will copy and paste the file1 into directory dir1.
If you copy a file to another regular file, it will overwrite to the destination file.

*Copying a directory

# cp -r dir1 dir2

The option ‘-r’ is used to copy the directory along with its contents and subdirectories.

*Moving a file or directory

# mv file1 dir1
or
# mv dir1 dir2

Its similar to cut and paste operation.

Note: Renaming a file is similar to moving the old file to a new file with different name in same location.

*Deleting a file

# rm file1

use the option “-f” to force delete.

*Deleting a directory

#rm -r dir1
or
#rm  -rf  dir1 ( to force delete)

Linux basic commands for Beginners-Part 2

Welcome to the next part of linux basic commands.

In case you have missed the previous post, or need a quick review on where we started, here is the link.

Continuing from the last post in Basic linux commands series, I am going to mention the most commonly used commands by a Linux BEGINNER.

linux basic commands

Changing the hostname

#hostnamectl  set-hostname  yourhostname.domain

The hostname can be with or without domain name.

Checking OS details

#hostnamectl

In systems based on redhat , this command will show you the OS, kernel, vendor details.

Configuring date and time

#date

This command shows you the date and time currently in your OS.

#timedatectl 

It provides more details on the timezone and the NTP status.

#timedatectl set-time 12:00

the above command will change the time to 12 o clock.

#timedatectl  set-time  2018-07-01 

This changes the date to 7th of july 2018.

Note that the command to change date and time is same. And the syntax for date and time should be strictly followed to avoid the errors.

Kernel details

#uname  -r

Shows the kernel name.

#uname  -a

Shows kernel name, version, architecture and other details.

Current directory name

#pwd

Prints the current working directory on screen. In other words shows your current location in directory hierarchy.

Shutdown

#shutdown -h 12:00

The system will shutdown at 12’o clock.

#shutdown -h now

The system will shutdown instantly.

Power off and reboot

#poweroff

powers off the OS.

#reboot

reboots the system.

You may wonder what is the difference between “shutdown and poweroff”, the first one is a systematic process whereas the later powers off the OS without closing any running programs or saving changes.

Other useful Linux basic commands

#logname

Shows the name of user logged in that terminal first.

#lastlog

Shows last successful login attempts of all user accounts.

#lastb

Gives the list of accounts which failed the login.

#tty

displays the name of current terminal.

#su username

switches to the user account mentioned in place of “username”.

 

These linux basic commands are good to start with , if you further want to explore then you can use this site link.

Basic linux commands-Part 1

If you got to start with the Linux, A thorough knowledge in Basic linux commands is must have. without much ado lets check out the various commands and tools to begin the journey of linux. If you still need to have more clear understanding of concept before starting here, refer to what is linux 

Basic linux commands

Linux offers a wide range of commands for both privileged and non-privileged use. Privileged or Super User commands are used for system management and are meant only for privileged users. Non-privileged commands can be run with regular user rights.

These commands are essential in order to work productively and manage the system efficiently.

Understanding the command syntax

The general command syntax is :

command option argument

You can use zero or more options and arguments in a command. Some commands have default options and arguments that they use when executed, and you do not need to mention them. Other commands require At least an Argument or option. An option, also called as switch or flag, modifies the behavior of the command, and an argument works as the target on which command is executed.

To make things simpler let’s follow few examples:

$ ls (no option, no argument; the default argument is the current directory name)
$ ls –l (one option, no argument; the default argument is the current directory name)
$ ls directory_name (no option, one argument)
$ ls –l directory_name (one option, one argument)

lets discuss it in detail

Files and directory listings

The ls (list) command is one the most important and easiest of Basic Linux commands and it displays a list of files and directories on the screen. It supports several options, given below-

Option Description

a    :Lists all files along with the hidden files. If a file or directory name                       starts  with a dot, it is considered hidden.
lh   :long listing with file sizes in human readable format, i.e, Bytes, KB MB               or GB.
l      :Displays long listing with detailed file information. Includes the file                   type, permissions, link count, owner, group, size, date and time of last               modification, and name of the file
ld    :Displays long listing of the specified directory.
R     :Lists contents of the given directory and all its sub-directories                              (recursive  listing).
lt     :Lists all files sorted by date and time with the latest file first.
ltr   :Lists all files sorted by date and time with the oldest file first.

For instance the option “-a” is included in this exmple :

#ls -a
#ls -a <dir name>

The angular bracket < > is not used, it is just to show the example content inside it.

Printing Working Directory

The pwd (print working directory) command shows the user’s current location in the directory tree.

The below command when executed by the testuser from its home directory shows-

$pwd
/home/testuser

Changing the directory

The cd (change directory) command is  used to navigate the directory tree. Run the following commands as testuser.
To change directory to /usr/ bin

$ cd /usr/ bin

To go back to the home directory, issue either of the following:

$ cd
$ cd ~

To go to the home directory of testuser :

$ cd ~testuser

where ~ is “tilde” symbol.

To go to the root directory, use the forward slash character:
$ cd /

To switch between current and previous directories, use the cd command with the dash character:

$ cd

To go one directory up to the parent directory, use dot twice:

$ cd ..

Showing the Terminal name

This command displays the terminal name we are currently logged on to:

$ tty

Displaying Currently Logged-In Users

The who command jumps to the /var/ run/ utmp file and presents the list of users currently logged on to the system:

$who

Viewing User Login Name

The whoami (who am i) command displays the effective username of the person executing this command:

$ whoami

The logname (login name) command shows the name of the real user who originally logged in to the system:

$ logname

Displaying History of Successful User Login Attempts

The last command reports the history of successful user login attempts and system reboots.

To list all user login, logout, and system reboot records, use the last command without any arguments:

$last

Viewing History of Failed User Login Attempts

Among the Basic linux commands the lastb command reports the history of unsuccessful user login attempts by reading the /var/ log/ btmp file.

To list all unsuccessful login attempts, type the lastb command without any arguments.

The last and lastb Basic linux commands should be executed with the root user account privilege.

#lastb

Checking Recent User Logins

The lastlog command shows the recent user logins by accessing the /var/ log/ lastlog file. T

#lastlog

Command for system infomation

The uname command provides basic information about the system.
Without any options, this command displays the Operating System name only.

# uname
Linux

You can use the –a option to get details.

# uname –a

output:

Linux host1. example.com 3.10.0-123. el7. x86_64 #1 SMP Mon May 5 11: 16: 57 EDT 2014 x86_64 x86_64 x86_64 GNU/ Linux

The information returned by the second command is:

Linux (Kernel name)
host1. example.com (Hostname of this system)
3.10.0-123. el7. x86_64 (Kernel release)
#1 SMP Mon May 5 11: 16: 57 EDT 2014 (Date and time of this kernel built)
x86_64 (Machine hardware)

 

These are few Basic linux commands. However, there are more and I will keep you tipped up in my forthcoming posts.

 

Also check out the website for ubuntu to have an broader overview.

Linux booting process. A systematic guide on the Linux boot related concept.

As a system administrator you should have an understanding of the Linux booting process.  In this post, you will learn about BIOS, the boot loader, the Linux kernel and Runlevels.

 

Linux booting process

Lets discuss and Understand the Linux booting process one by one

The BIOS

 

The BIOS stands for Basic Input/Output System.  It’s a firmware used in the booting process and it’s the first piece of software that is executed when a computer is powered on.  The BIOS is doesn not depend onthe OS type, rather it is independent.

Its primary purpose is to test the hardware on the motherboard by executing Power On Self Test and to load a boot loader or operating system. The POST performs some basic checks of hardware components such as the CPU, memory, and storage devices.  Only if the POST succeeds, the BIOS attempt to load the boot loader.

The BIOS contains a list of bootable devices such as hard disks, DVD drive, USB devices, and others depending on what hardware is connected.

The BIOS searches the list for a bootable device in the order of boot priority.  You can change this order by getting into BIOS configuration by interrupting the boot sequence.  The key combination used to do this will be different from one hardware manufacturer to another.

Once a bootable device has been found, the BIOS runs the boot loader.  Generally the GRUB boot loader will be used, but there are other boot loader too like, LILO boot loader.  LILO is an abbreviation for LInux LOader, while GRUB stands for GRand Unified Bootloader.

The primary purpose of the boot loader is to start the operating system.  You will see a series of messages from the boot loader. These messages provides an option to interrupt the boot process and interact with the boot loader.  If there are multiple operating systems installed, you can select one.  You can also instruct the boot loader to pass different boot options to the operating system.

The Initial RAM Disk

The initial RAM disk, also known as “initrd,” is a temporary file system that’s loaded into RAM. when the linux booting process starts,  this file system contain helpers that perform hardware detection and load the necessary modules, sometimes drivers, to get the original file system mounted.  Suppose that, if the root file filesystem is located on an LVM (Logical Volume Manager) volume, the initrd image will contain the modules required to mount that logical volume as the root file system.  Once the root file system is mounted the job of initrd is done and the operating system continues loading from the actual root file system.

Kernel and Initial RAM Disk Location

The Linux kernel, the initial RAM disk, and other files needed to boot the operating system are stored in /boot.  Here is a list of contents in /boot directory for a linux system.

$ ls -1F /boot

abi-3.13.0-46-generic
config-3.13.0-46-generic
grub/
initrd.img-3.13.0-46-generic
System.map-3.13.0-46-generic
vmlinuz-3.13.0-46-generic

 

The Kernel Ring Buffer

The kernel ring buffer contains messages related to the Linux kernel.  A ring buffer is a data structure with almost constant size. If the buffer is completely full, old messages are discarded .To see the contents of the kernel ring buffer, use the command –

#dmesg

Runlevels and Targets

The last step in Linux booting process is Runlevel. Linux uses runlevels to decide which processes and services to start. The Runlevels are mentioned below :

0  :Shuts down the system
1  :Single user mode.?Used for maintenance.
2  :Multi-user mode with graphical interface. (Debian/Ubuntu)
3  :Multi-user text mode (RedHat/CentOS)
4  :Undefined
5  :Multi-user mode with graphical interface. ?(RedHat/CentOS)
6  :Reboot

Previously, runlevels were controlled by the init program.  The init configuration was stored in /etc/inittab. To change the default runlevel using init, you would edit the /etc/inittab file. However, init alternatives such as systemd and upstart are quickly taking the place of init with systemd currently being the most widely used.

Instead of runlevels, systemd has the targets.  These targets are equivalent to runlevels.  You can find the list of available targets in /lib/systemd/system. The runlevel targets are actually symbolic links to the real targets.  For example, runlevel5.target is a symlink to graphical.target.

Rebooting a System

For rebooting you can use the reboot or shutdown commands as well as the runlevel/target.

Here’s how to reboot with init using coomand :

# telinit 6

To reboot using systemd use the systemctl command :

# systemctl isolate reboot.target

To reboot using the reboot command :

# reboot
The format of the shutdown command is as follows :

shutdown [options] time [message]

The option to use reboot with shutdown format is  -r. 
You can specify the time to shutdown using the “HH:MM” format.
You can also use +N where N represents the number of minutes to wait.  Finally, you can use the “now” keyword to start immediately.  Optionally, you may specify a message that will be broadcast to all users logged into the system.

# shutdown -r now

Powering Off a System

To power off a system, you can use runlevel 0, the poweroff target, or the poweroff command. we can power-off with init by executing following command :

# telinit 0

Power off a system with systemctl :

# systemctl isolate poweroff.target

Finally, you can use the poweroff command :

# poweroff

If you need to refresh on what is Linux, refer this post. 

What is Linux?

What is Linux? Many of the devices you own probably, such as Android phones, digital storage devices, personal video recorders, cameras, wearables, and more, run on Linux. Even your car has Linux running in its controllers.

What is Linux

Linux has been around since mid 90’s. since then it reached a user base spanning wide regions. Linux is in your cars, phones, and refrigerators.Linux was (and still is) one of the most reliable, secure, and worry-free operating systems available, before it became the platform to run desktops, servers, and embedded systems across the globe.

Worry not-here is all the information you need to get up to speed on the Linux platform.

Linux is the best-known and most-used open source operating system. As an operating system, Linux is software that sits underneath all of the other software on a computer, receiving requests from those programs and relaying these requests to the computer’s hardware

The term “Linux” is generally used to refer to the Linux kernel, but also the set of programs, tools, and services that are included together with the Linux kernel to provide all of the components of an operating system. Some people, specially members of the Free Software Foundation, refer to this collection as GNU/Linux, for the reson many of the tools included are GNU components. But there are operating systems which does not include GNU tools and components as a part of their operating system. Android, for example, uses a Linux kernel but uses its own tool. And so does Redhat.

How is Linux different from other Operating Systems?

Like other operating systems, may have graphical interface, and types of software you are used to on other operating systems, such as office tool, have Linux variants. In many cases, you wil find Linux version of the same program you use on other systems. If you use a computer or other electronic device, you can use Linux.

But Linux is also different from other OS platforms in various ways. First of all, Linux is open source software. The source code which is used to create Linux is free and available to the public to edit, view, and—for users with the advance skills—to contribute to.

Though the core components of the Linux operating system are generally common, there are distributions of Linux, which have different software options. It makes Linux is highly customizable, because not just applications, such as text editors and web browsers, can be swapped out. Linux users also can choose core components, such as CLI or GUI, and other user-interface components.

Who are the Linux users?

Chances are you’re probably already using Linux, whether you know it or not. Between one- and two-thirds of the webpages on the Internet are generated by servers running above Linux.

The Companies and individuals prefer Linux for their servers because for its security features, and one can get excellent support from a large community of users. Also companies like Canonical, SUSE, and Red Hat, offer commercial support.

How was Linux created?

Linux was created in 1991 by Linus Torvalds, a student at the University of Helsinki. Torvalds built linux as a free and open source alternative of minix, a Unix based OS that was popularly used in academic setups. He originally wanted to name it “Freax,” but the server administrator Torvalds used to distribute the original code named his directory “Linux” after a combination of Torvalds’ first name and the word Unix, and the name remained.

How can start using Linux?

Chances are you’re using Linux already and not aware of it, but if you want to install Linux on your home computer for a try out, the easiest way is to pick a popular linux distribution that is designed for your hardware type (for example, laptop or tablet device) and give it a shot. Although there are several distributions on the internet, most of the older, well-known distributions are good choices for beginners because of its large user communities that can help troubleshoot if you get stuck somewhere. Popular distributions include Debian, Fedora, Mint, and Ubuntu, but there are many others.

Where can I start learning about Linux?

Four Linux distros for kids

Want a fulfilling IT career? Learn Linux

Building a Linux lab and its great potential in education