0% found this document useful (0 votes)
28 views

Linux Commands

The document discusses several Linux commands like SED, SORT, UNIQ, CUT, SUDO, and USERADD. SED is used for text stream editing like search, find, replace, insert, delete. SORT sorts the contents of a file. UNIQ filters out adjacent duplicate lines. CUT extracts parts of a line. SUDO allows users to run commands as root. USERADD adds new user accounts.

Uploaded by

Tanmay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Linux Commands

The document discusses several Linux commands like SED, SORT, UNIQ, CUT, SUDO, and USERADD. SED is used for text stream editing like search, find, replace, insert, delete. SORT sorts the contents of a file. UNIQ filters out adjacent duplicate lines. CUT extracts parts of a line. SUDO allows users to run commands as root. USERADD adds new user accounts.

Uploaded by

Tanmay Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SED

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.

Create a file named file1.txt with following data:


unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Replacing a string in the file:


$sed 's/unix/linux/' file1.txt

This will print:


linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

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'

The output will be as follows:


(M)y (n)ame (i)s (M)ayank

Replacing string on a specific line number: (Assuming you want to replace the string on line 3)
$sed '3 s/unix/linux/' file1.txt

Duplicating the replaced line with /p flag:


$sed 's/unix/linux/p' file1.txt
The /p print flag prints the replaced line (the line where replacement of unix to linux happened) twice on the terminal. If a line does
not have the search pattern and is not replaced, then the /p prints that line only once.
Printing only the replaced lines:
$sed -n 's/unix/linux/p' 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 a particular line, say 4th line in this example:


$sed '4d' file1.txt

To delete the last line


$sed '$d' file1.txt

To delete the lines in range, say 2nd to 4th line:


$sed '2,4d' file1.txt

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.

Create a file named file.txt with following data:


abhishek
chitransh
satish
rajan
naveen
divyam
harsh

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 numeric data:


$ sort -n file.txt

Sorting numeric data in reverse order:


$ sort -nr 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.

Check is the file is already sorted or not:


$ sort -c file.txt
if there is not output on the screen, it means the data is sorted, otherwise it will display the error ‘disorder’.

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.

Create a file named kt.txt with following data:


I love music.
I love music.
I love music.

I love music of Kartik.


I love music of Kartik.

Thanks.

Using the command:


$uniq kt.txt
I love music.

I love music of Kartik.

Thanks.

Printing the number of repetitions at the starting of each line:


$uniq -c kt.txt

Only printing the repeated lines, once:


$uniq -d kt.txt

Printing the repeated lines, all instances:


$uniq -D kt.txt
Printing only unique lines:
$uniq -u kt.txt

Only printing the repeated lines, once:


$uniq -d kt.txt

Skipping number of fields before comparing for uniqueness:


$uniq -f 2 kt.txt

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.

Adding a simple user:


$ sudo useradd test_user
To verify after the execution of the above command use cat /etc/passwd | grep test_user

Giving home directory path for the user:


$ sudo useradd -d /home/test_user test_user
To verify after the execution of the above command use cat /etc/passwd | grep test_user

Creating a new user with specific user id:


$ sudo useradd -u 1234 test_user
To verify after the execution of the above command use id test_user

Creating a new user with specific group id:


$ sudo useradd -g 1000 test_user
To verify after the execution of the above command use id test_user
Creating a user without home directory:
$ sudo useradd -M test_user

Creating a user with an expiry date:


$ sudo useradd -e 2020-05-30 test_user
To verify after the execution of the above command use sudo chage -l test_user

Creating a user with a short comment or description:


$ sudo useradd -c "This is a test user" test_user
To verify after the execution of the above command use cat /etc/passwd | grep test_user

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

userdel -h : This option display help message and exit.


$ userdel -h

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.

Changing home directory for the user:


$ sudo usermod -d /home/test_user test_user
To verify after the execution of the above command use cat /etc/passwd | grep test_user

Changing the user id of the user:


$ sudo usermod -u 1234 test_user
To verify after the execution of the above command use id test_user

Changing the group of the user:


$ sudo usermod -g mayank test_user
To verify after the execution of the above command use id test_user
Changing the expiry date of the user:
$ sudo usermod -e 2020-05-30 test_user
To verify after the execution of the above command use sudo chage -l test_user

Add a short comment or description:


$ sudo usermod -c "This is a test user" test_user
To verify after the execution of the above command use cat /etc/passwd | grep test_user

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

Changing the user login name:


$ sudo usermod -l test_account test_user
To verify after the execution of the above command use id test_account and then try running id 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/

You might also like