top of page

Top Commands for Linux Beginners


We will not cover much theory. Instead, we simply provide top commands and their use.





1. Expand and Unexpanded Command


expand demo.txt

unexpand –a demo.txt



2. Join AND Split Command


Join Command- Suppose you want to join two files named cat and dog or file1.txt and file2.txt


Join cat dog OR join file1.txt file2.txt


But if you want to join the contents of the file, you have to specify which fields you are joining.


File 1 File 2

Alpha 1 Theta 1

Omega 2 Beta 2


join -1 2 -2 1 File1.txt File2.txt

-1 refers to File1.txt and -2 refers to File2.txt


Split Command- files will be split into different sections, by default splitting happens once fie reaches a 1000 line limit.

Split anyfile



3. Sorting Command


i. To sort any lines in file1.txt


Sort file1.txt

ii. To reverse sort any lines in file1.txt


Sort – r file1.txt

iii. To numeric sort any lines in file1.txt


Sort –n file1.txt


4. Translate(tr) Command

This command allows you to translate character sets to another set of characters like uppercase to lowercase or vice-versa


Tr –a-z A-Z

It translates lowercase to uppercase characters.


5. Unique(uniq) Command


i. -This command helps to removes duplicates.

Uniq reading.txt

reading.txt may be any text file with numbers of contents. The used command removes all duplicate files within reading.txt


ii. To count the number of occurrences of a line

Uniq –c reading.txt


iii. To get only duplicate values

Uniq –d reading.txt


Sometimes uniq command doe not duplicate lines unless they are adjacent so you need to use sort reading.txt | uniq command

6. Grep command

This command allows you to search files for characters that match a certain pattern.

i. If you want to directly search some string from a text file

grep hello textfile.txt


ii. To search some file in any directory

ls /somedir | grep ‘.exe$’

It searches all .exe files in somedir



7. Search using regular expressions


1. Beginning of the Line with ^

Suppose we have phrase:

The computer is scanned

By antivirus

^by: this would match the line “by antivirus”


2. End of the Line with $

Scanned$ would match the line “The computer is scanned”


3. Bracket notation with []

R[aiu]ng

Would match: rang, ring, rung


4. Bracket notation with [^]

R[^i]ng

Would match rang and rung but not ring.


5. Brackets to use a range of cases

R[a-c]ng

Would match Rang, Rbng and Rcng but not RAng, RBng, RCng. You have to use R[A-C] to overcome this.




8. USING VIM COmmand


If you want to search any string use /key and search for your key string. To go forward press”n” and “N” to go backward in your search results.


Suppose we have a string. "The computer is scanned by Antivirus".


/scanned

Will find a string named “scanned” in a text file

Or you can use the search command

?scanned for backward search

It Will also give the same result as /scanned


A. Appending text using vim

i - insert text before the cursor

O - insert text on the previous line

o - insert text on the next line

a - append text after the cursor

A - append text at the end of the line

Esc- to go back to the command


B. Editing using vim

x - used to cut the selected text also used for deleting characters

dd - used to delete the current entire line

y - yank or copy whatever is selected

yy - yank or copy the current line

p - paste the copied text before the cursor



C. Vim Saving and Exiting

:w - writes or saves the file

:q - quit out of vim

:wq - write and then quit

:q! - quit out of vim without saving the file

ZZ - equivalent of :wq, but one character faster

u - undo your last action

Ctrl-r - redo your last action


9. Explaining /etc/passwd

This file shows you a list of users and detailed information about them.

In most cases it looks like this

root:x:0:0:root:/root:/bin/bash

1. Username

2. User's password - the password is not really stored in this file, it's usually stored in the /etc/shadow file. You can see many different symbols that are in this field if you see an "x" that means the password is stored in the /etc/shadow file, a "*" means the user doesn't have login access, and if there is a blank field that means the user doesn't have a password.

3. The user ID - as you can see root has the UID of 0

4. The group ID

5. GECOS field - This is used to generally leave comments about the user or account such as their real name or phone number, it is comma-delimited.

6. User's home directory

7. User's shell - you'll probably see a lot of user's defaulting to bash for their shell



10. Explaining /etc/shadow

The /etc/shadow file is used to store information about user authentication. It requires superuser read permissions.

Suppose we run this command

sudo cat /etc/shadow

root:MyEPTEa$6Nonsense:15000:0:99999:7:::


Each field are separated by colons and below are field included in this file.

1. Username

2. Encrypted Password

3. Date of last password changed

4. Minimum password age

5. Maximum password age

6. Password warning period

7. Password inactivity period

8. Account expiration date

9. Reserved field for future use


11. Explaining /etc/group

This file allows for different groups with different permissions.

Run command

Cat /etc/group

Root:*:0:omega


It includes fields as follows

1. Group name

2. Group password-“*” is the default value for the password.

3. Group ID

4. List of users


A. Adding users

Sudo useradd omega


B. Removing users

Sudo userdel omega


C. Changing passwords

Passwd alpha

Remember if you are root, you can change the password for yourself or another guest user.


12. File Permissions Command

Drwxr-xr

d- directory

r- readable

w- writable

x- executable

-: empty


i. Modifying permissions

Using command chmod helps to change permissions easily


ii. Addding permission bit on a file

Chmod u+x myfile1


This means change permission on myfile1 by adding an executable permission bit on the user set. Now, file1 is executable.


iii. Removing permission bit on a file

Chmod u-x myfile1


Another way to change permissions is using the numerical format. Below are numerical representations you need to understand

4: read permission

2: write permission

1: execute permission


Now let's look example

Chmod 755 myfile1

Which represents as explained. The First 7 means user permissions, the second number 5 represents group permission and the final 5 represents other permissions. But you might be wondering there is no permission for 7 and 5. So let’s understand some mathematics

4+2+1=7 which is user permissions and it has read, write and execute permissions.

4+1=5 which is group permissions and has read and execute permission

4+1=5 which is other permissions and have read and execute permissions.

Remember do not change permissions for fun if you don’t know their actual implications and purpose, otherwise, you may end up exposing potential sensitive files to modify, update or delete for illegitimate users.


13. Ownership permissions


i. Modify user ownership

Sudo chown alpha omega

It changes the owner of an omega to alpha


ii. Modify group ownership

Sudo chgrp theta alpha

It changes the group of alpha to theta


iii. Combining both: changing user and group ownership at the same time

Sudo chown omega:theta alpha



iv. To change the default permission

Umask 022

022 means all user access but no write access for the group and other users.



14. SETUID AND SETGID Command

The Set User ID (SUID) allows a user to run a program as the owner of the program file rather than as themselves. It is used to access the protected files.


i. As normal command

Sudo chmod u+s myfile


ii. As numerical command

Sudo chmod 4755 myfile

4 denotes SUID.

Set group ID (SGID) allows a program to run as if it was a member of that group.


i. As normal command

Sudo chmod g+s myfile

ii. As numerical command

Sudo chmod 2555 myfile

2 represents SGID.



15. Install a package

Debian: $ dpkg -i some_deb_package.deb

RPM: $ rpm -i some_rpm_package.rpm


16. Remove a package

Debian: $ dpkg -r some_deb_package.deb

RPM: $ rpm -e some_rpm_package.rpm


17. List installed packages

Debian: $ dpkg -l

RPM: $ rpm –qa


18. Install a package from a repository

Debian: $ apt install package_name

RPM: $ yum install package_name


19. Remove a package

Debian: $ apt remove package_name

RPM: $ yum erase package_name


20. Get information about an installed package

Debian: apt show package_name

RPM: yum info package_name


21. Installing any tar.gz file

1. Install tools that will allow to compile source code

sudo apt install build-essential


2. Extract the contents of the package file

tar -xzvf package.tar.gz


3. Before you dive further, always look README or INSTALL file from the package. There will be some instructions regarding installing the package. After you read it, you need to configure the script.

./configure where ./ allows to execute script in current directory


4. Now build the software package contents

Make


5. Now the actual installation of the package

Sudo make install



6. To uninstall the package

Sudo make uninstall


You can use sudo checkinstall to make installation and uninstallation easy and efficient.





Comments


Drop us a Line, Let us Know What You Think

Thanks for submitting!

© AbridgedUp 2021 

bottom of page