Basic Linux Commands
A beginner's guide to essential Linux commands.
This article describes basic Linux commands that you must know as a system or database administrator. Below commands in Linux help you manage any Linux server fast and smoothly.
Linux Command List
Check the article to install Oracle Linux 7 on VirtualBox step by step.
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 -lnproc --allgetconf _NPROCESSORS_ONLNLinux Create Directory
To create a single directory in Linux
mkdir LinuxForBeginnersTo create multiple directory in Linux
mkdir -p /parent/child1/child2/child3To create a directory by setting permission
mkdir -m 777 directoryTo delete directory in Linux
rm -rf /path/to/directoryTo copy directory in Linux
cp -R <source directory> <destination 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 firstfileCreate multiple empty files
touch secondfile thirdfile fourthfile fifthfileLinux 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_fileHere
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 /u01Linux 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_fileTo count the number of words in a file
wc -w my_fileTo count the number of characters in a file
wc -c my_fileLinux SORT Command
To sort and display the contents of a file
sort my_fileTo sort a file in reverse order
sort -r sort_examplesTo sort a file and remove the duplicate values
sort -u salaryLinux 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.txtIf you want to ignore the case, use -i
grep -i ORAcle /tmp/installer.txtTo search a specific phrase inside a file
grep -i 'oracle database' /tmp/installer.txtTo count total number of lines that contain a specific word
grep -c oracle /tmp/installer.txtTo print line number along the exact line that contains the word oracle
grep -n oracle /tmp/installer.txtTo get the name of the files that contain a specific string (oracle) inside
grep -l oracle file1.txt file2.txt file3.txt file4.txtTo match the lines which start with the given string or pattern
grep '^oracle' /tmp/installer.txtTo match the lines which end with the given string or pattern
grep 'oracle$' /tmp/installer.txtLinux Find Command
To find files older than 35 days in a specific directory path and save the output 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 using Linux FIND command
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 -printTo 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 oracleRun below command which will force user to change password on next login
chage -d 0 oracleTo check the status of a user's password
chage -l oracleManaging Groups in Linux
Create a new group in Linux
groupadd gridAdd users to a group
usermod -a -G grid oracleList all users that belong to a group
grep grid /etc/groupList all the groups that a user belongs to
groups oracleList all the groups on your Linux server
getent groupDelete a group
groupdel gridManaging Users in Linux
To list all the users in Linux
cat /etc/passwdTo create a new user with a home directory having the same username
"/home/arun" useradd -m oracleAdd a password to the created user
passwd arunCreate a user with password in the same command
useradd -m oracle -p oracle123Create a user and assign it to an already existing group
useradd -m oracle -p oracle123 -G oinstallAdd user to multiple groups
usermod -a -G oinstall,dba,grid oracleRemove users from the group
gpasswd -d oracle gridTo lock the user password and prevent the user from logging in using a password
passwd -l oracleTo unlock a user password on Linux
passwd -u oracleTo delete a user in Linux
userdel -r oracle
