Linux Commands
Linux Commands
SED command in LINUX stands for stream editor and it can perform lots of functions on file like searching, find and replace,
insertion or deletion. Though most common use of SED command in LINUX is for substitution or for find and replace. By using SED
you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening
that file in VI Editor and then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
• SED command in linux supports regular expression which allows it perform complex pattern matching.
Here the “s” specifies the substitution operation. The “/” are delimiters. The “unix” is the search pattern and the “linux” is the
replacement string. By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace the
second, third…occurrence in the line.
Replacing nth occurrence of the string in the file: (Assuming n=2)
$sed 's/unix/linux/2' file1.txt
This will replace the 2nd occurrence of the word ‘unix’ in each line as we assumed n=2. Similarly we can replace 1st,3rd,4th…Nth.
Replacing all occurrences of the string in the file:
$sed 's/unix/linux/g' file1.txt
This will replace all the occurrences of the word ‘unix’ in each line. ‘g’ stands for Global here which basically means all patterns.
Replacing from nth to all rest of the occurrences of the string in the file: (Assuming n=2)
$sed 's/unix/linux/2g' file1.txt
This will start replacing from the 2nd occurrence of the word ‘unix’ in each line till all the occurrences after it are replaced.
Parenthesize first character of each word: (you will need to understand Regex for this)
$ echo "My name is Mayank" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Replacing string on a specific line number: (Assuming you want to replace the string on line 3)
$sed '3 s/unix/linux/' file1.txt
Replacing string in a specific range of lines: (Assuming you want to perform replacement only between line 1 to 3)
$sed '1,3 s/unix/linux/2g' file1.txt
You can use $ instead of 3 in the above example to replace the lines till the last line as the $ represents last line
Deleting lines from a particular file without opening it:
SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation
without even opening the file.
To delete from Nth to last line, say from 3rd to last line:
$sed '3,$d' file1.txt
To delete the line that has a matching pattern: (say the pattern we are searching for is abc)
$sed '/abc/d' file1.txt
SORT
SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the
contents are ASCII. Using options in the sort command can also be used to sort numerically.
SORT command sorts the contents of a text file, line by line.
sort is a standard command-line program that prints the lines of its input or concatenation of all files listed in its argument list in
sorted order.
The sort command is a command-line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by
number, by month, and can also remove duplicates.
The sort command can also sort by items not at the beginning of the line, ignore case sensitivity, and return whether a file is sorted or
not. Sorting is done based on one or more sort keys extracted from each line of input.
By default, the entire input is taken as the sort key. Blank space is the default field separator.
Sorting it:
$ sort file.txt
Output:
abhishek
chitransh
divyam
harsh
naveen
rajan
satish
Sort function with mix file i.e. uppercase and lower case:
$ sort file.txt
This will first sort the uppercase letters and then lowercase letters
Writing the sorted output to a new file: (directly redirecting the output or using -o option)
$ sort file.txt > output.txt
$ sort -o output.txt file.txt
$ cat output.txt
Sorting in reverse order:
$ sort -r file.txt
Sorting based on any column number: (create another file with 2 columns, one has names(string) and other has numerical data)
$ sort -k 2n nameNumber.txt
Here 2n refers to sorting using 2nd column number and n means sorting numerically. If we use only -2, it will sort using second column
as per the ascii values.
Sorting and removing duplicates (Create another file which has duplicate lines and then try this command)
$ sort -u file.txt
$ cat file.txt
Sorting using the Month of the year (Create a file with multiple columns and keep the month names in one of the column to use it)
$ sort -M file.txt
$ cat file.txt
UNIQ
The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file.
In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. uniq filters out
the adjacent matching lines from the input file(that is required as an argument) and writes the filtered data to the output file.
Thanks.
Thanks.
This can be helpful when your records are numbered (say the records are written as 1. Chitkara 2. Hitbullseye), So here we are
skipping 2 fields from the start (one that has the numbers and 2nd that has the dot(.)) and then we started comparing the strings
Similarly we can skip characters instead of fields using -s (N can be any number)
$uniq -s N kt.txt
Limiting the uniq to compare only few characters from the starting:
$uniq -w 3 kt.txt
Here we are expecting the uniq to compare only first 3 characters of each string.
Doing case-insensitive comparison (case means capital and small letters, small is lowercase, capital is uppercase)
$uniq -i kt.txt
CUT
Please refer the following link for practicing all the variations of the CUT command:
https://www.geeksforgeeks.org/cut-command-linux-examples/?ref=lbp
Sudo and SU
Providing root access to all users is not a good practice. Nonetheless, normal users might need to execute some commands as
superuser occasionally. This is where the sudo (Super User DO) access comes into the picture. The sudo command provides an
efficient way to grant users the special rights to execute commands at the system (root) level. With sudo, we’ll be able to execute
administrative tasks without switching users. This is the equivalent of “run as administrator” option in Windows. By default, sudo
requires that users authenticate themselves with a password which is the user’s password, not the root password itself.
Refer the following link for su and using sudo with su:
https://linuxize.com/post/su-command-in-linux/
useradd
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in
Linux and the difference between both of them is that useradd is a native binary compiled with system whereas adduser is a Perl script
which uses useradd binary in the background.
To set an unencrypted password for the user: (unencrypted means not hidden)
$ sudo useradd -p test_password test_user
To verify after the execution of the above command use cat /etc/shadow | grep test_user
To display help:
$ sudo useradd --help
userdel
userdel command in Linux system is used to delete a user account and related files. This command basically modifies the system
account files, deleting all the entries which refer to the username LOGIN. It is a low-level utility for removing the users.
userdel -f: This option forces the removal of the specified user account. It doesn’t matter that the user is still logged in. It also forces
the userdel to remove the user’s home directory and mail spool, even if another user is using the same home directory or even if the
mail spool is not owned by the specified user. (neuser is the username)
$ sudo userdel -f neuser
userdel -r: Whenever we are deleting a user using this option then the files in the user’s home directory will be removed along with the
home directory itself and the user’s mail spool. All the files located in other file systems will have to be searched for and deleted
manually.
$ sudo userdel -r newuser2
userdel -R: This option apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
$ sudo userdel -R newuser2
userdel -Z : This option remove any SELinux(Security-Enhanced Linux) user mapping for the user’s login.
$ sudo userdel -Z newuser2
Usermod
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the
command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to
do that we use the Usermod command.
To set an unencrypted password for the user: (unencrypted means not hidden)
$ sudo usermod -p test_password test_user
To verify after the execution of the above command use cat /etc/shadow | grep test_user
To lock a user:
$ sudo usermod -L test_user
To unlock a user:
$ sudo usermod -U test_user
groupadd
Please refer the following link for practicing the variations of the GROUPADD command:
https://www.geeksforgeeks.org/groupadd-command-in-linux-with-examples/
groupdel
Please refer the following link for practicing the variations of the GROUPDEL command:
https://www.geeksforgeeks.org/groupdel-command-in-linux-with-examples/