• Arun Kumar

Important Linux Commands

This article describes important Linux commands that you must know as a system or database administrator.


Check CPU Cores in Linux

Use one of the below commands to find number of CPUs in Linux including cores

cat /proc/cpuinfo|grep processor|wc -l
nproc --all
getconf _NPROCESSORS_ONLN

Linux Create Directory

To create a single directory in Linux

mkdir LinuxForBeginners

To create multiple directory in Linux

mkdir -p /parent/child1/child2/child3

To create a directory by setting permission

mkdir -m 777 directory

Linux Touch Command

One of the easiest ways to create a new and empty file is the touch command. Use below command to create an empty file

touch firstfile

Create multiple empty files

touch secondfile thirdfile fourthfile fifthfile

Linux Files and Directory Permissions

There are three main permissions that you assign to a file or directory

  • Read - 4

  • Write - 2

  • Execute - 1

And, there are three main levels of permissions

  • Owner of the file

  • Other members of the group which owner belongs to

  • All other users on the server

To give full permissions to owner (read, write and execute), give read and execute permission to other group members and no permissions to all other users

chmod 750 test_file

Here

  • 7 = Read(4) + Write(2) + Execute(1)

  • 5 = Read(4) + Execute(1)

  • 0 = No permissions

To give full permissions on /u01 and also to all the contents inside the folder

chmod -R 777 /u01

Linux WC Command

The wc command allows you to count the number of lines, characters, and words in a text. To count the number of lines in a file

wc -l my_file

To count the number of words in a file

wc -w my_file

To count the number of characters in a file

wc -c my_file

Linux SORT Command

To sort and display the contents of a file

sort my_file

To sort a file in reverse order

sort -r sort_examples

To sort a file and remove the duplicate values

sort -u salary

Linux GREP Command

GREP command is used to search anytime inside a file or output. Linux grep is different from Linux find command. Linux find command is used for searching files and directories. Linux grep is powerful and can be used for searching a pattern inside a file.


To print each line containing the word oracle. The command is case sensitive

grep oracle /tmp/installer.txt

If you want to ignore the case, use -i

grep -i ORAcle /tmp/installer.txt

To search a specific phrase inside a file

grep -i 'oracle database' /tmp/installer.txt

To count total number of lines that contain a specific word

grep -c oracle /tmp/installer.txt

To print line number along the exact line that contains the word oracle

grep -n oracle /tmp/installer.txt

To get the name of the files that contain a specific string (oracle) inside

grep -l oracle file1.txt file2.txt file3.txt file4.txt

To match the lines which start with the given string or pattern

grep '^oracle' /tmp/installer.txt

To match the lines which end with the given string or pattern

grep 'oracle$' /tmp/installer.txt

Linux Find Command

To find files older than 35 days in a specific directory path and save the ouput in backupfiles.log. Here the directory we are searching is /backup/logs and -mtime specifies the modified time of a file. We are saving the list of all the files which are older than 35 days in backupfiles.log

find /backup/logs -type f -mtime +35 -print > backupfiles.log &

To print files older than 7 days on screen and do not want to save it into a file, use below command

find /backup/logs -type f -mtime +7 -print

To find all the files under current location (as we have specified . dot), search file name starting with arch and ending with log. check file create time with -ctime older than 28 days and then remove those files using rm -f

find . -name arch\*log -ctime +28 -exec rm -f {} \;

Force User to Change Password

To force user to change password on next login, first of all the password must have expired

passwd --expire oracle

Run below command which will force user to change password on next login

chage -d 0 oracle

To check the status of a user's password

chage -l oracle

Managing Groups in Linux

Create a new group in Linux

groupadd grid

Add users to a group

usermod -a -G grid oracle

List all users that belong to a group

grep grid /etc/group

List all the groups that a user belongs to

groups oracle

List all the groups on your Linux server

getent group

Delete a group

groupdel grid

Managing Users in Linux

To create a new user with a home directory having the same username

"/home/arun" useradd -m oracle

Add a password to the created user

passwd arun

Create a user with password in the same command

useradd -m oracle -p oracle123

Create a user and assign it to an already existing group

useradd -m oracle -p oracle123 -G oinstall

Add user to multiple groups

usermod -a -G oinstall,dba,grid oracle

Remove users from the group

gpasswd -d oracle grid

To lock the user password and prevent the user from logging in using a password

passwd -l oracle

To unlock a user password on Linux

passwd -u oracle

To delete a user in Linux

userdel -r oracle

205 views

Recent Posts

See All

Manage Linux Firewall with iptables and firewalld

In this article will be covering details regarding iptables and firewalld which helps in Linux firewall management. We will also be looking at how to enable specific ports (1521 for Oracle) inside ipt

Setup Passwordless Connectivity in Linux

Every time you ssh from one Linux server to another, you need to enter target server user's password. We can also setup password less ssh login between two Linux servers so that you don't have to ente

Mount Hard Disk in Linux

In this article we will be looking at adding a new hard disk to a Linux virtual machine and mount it on a directory. This can be achieved by adding a hard disk to virtual box machine under settings an

  • Facebook
  • Twitter
  • YouTube
  • Instagram

© 2020 by DBA Genesis

Feedback