Saturday 5 June 2021

Basic Linux Commands

Basic Linux Commands, Linux Exam Prep, Linux Preparation, Linux Certification, LPI Preparation, LPI Guides, LPI Career

Basic Linux Commands

Now, let us look at the most important 20 commands in Linux. Linux commands are case sensitive hence you need to be careful about what you are keying in.

1. ls –

List directory contents. If you know windows you would know that the command dir is used to list the contents in a directory. In Linux, the ls command is used to list out files and directories. Some versions may support color-coding. The names in blue represent the names of directories.

More Info: LPIC-3 300: Linux Enterprise Professional Mixed Environment

ls -l | more – this helps to paginate the output so you can view page by page. Otherwise the listing scrolls down rapidly. You can always use ctrl c to go back to the command line.

$ ls -l filename

2. cd /var/log –

Change the current directory. The forward slash is to be used in Linux. The example is a Linux directory that comes with all versions of Linux.

When you use ls –I you will be able to see more details of the contents in the directory

It will list down the

◉ Permissions associated with the file

◉ The owner of the file

◉ The group associated with the file

◉ The size of the file

◉ The time stamp

◉ The name of the file

$ cd /var/log

3. grep –

Find text in a file. The grep command searches through many files at a time to find a piece of text you are looking for.

grep PATTERN [FILE]

grep failed transaction.log

The above command will find all of the words in the files that matched the word ‘failed’.

$ grep ‘failed’ transaction.log

4. su / sudo command –

There are some commands that need elevated rights to run on a Linux system. So you run them as a System Administrator which normal users cannot do.

su command changes the shell to be used as a super user and until you use the exit command you can continue to be the super user

sudo – if you just need to run something as a super user, you can use the sudo command. This will allow you to run the command in elevated rights and once the command is executed you will be back to your normal rights and permissions.

Example – shutdown command the shutdown command safely turns off the computer system.

◉ sudo shutdown 2 – shutdown and turns of the computer after 2 minutes

◉ sudo shutdown –r 2 – shuts down and reboots in 2 minutes

◉ Using ctrl C or shutdown –c helps in stopping the shutdown process.

$ sudo shutdown 2

$ sudo shutdown –r 2

5. pwd – Print Working Directory

One way to identify the directory you are working in is the pwd command

It displays the current working directory path and is useful when directory changes are often

$ pwd

6. passwd –

Though looks similar to the pwd command the role it plays is different.

This command is used to change the user account password. You could change your password or the password of other users. Note that the normal system users may only change their own password, while root may modify the password for any account.

passwd [username] - changes the password for the user.

$ passwd admin

7. mv – Move a file

To move a file or rename a file you would use the mv command.

Here the file name gets changed from first.txt to second.txt.

Type ls to view the change

$ mv first.txt second.txt

8. cp – Copy a file

cp source file destination file. In case you need a copy of the file second.txt in the same directory you have to use the cp command

$ cp second.txt third.txt

You can use ls – l to see the new file created. The two files will be exactly of the same size.

9. rm –

This command is used to remove files in a directory or the directory itself. A directory cannot be removed if it is not empty.

rm [name of the file]

rm –r removes all the contents in a directory and the directory as well.

$ rm file1

$ rm -r myproject

10. mkdir – to make a directory.

mkdir [directory name] if you would like to create a directory in the name ‘myproject’ type

mkdir myproject

$ mkdir myproject

11. chmod –

To change mode of a file system object. Files can have r – read, w- write and x-execute permissions.

For example:

◉ chmod mode FILE

◉ chmod 744 script.sh

◉ The first number stands for the user who is associated with the file

◉ The second number is for the group associated with the file

◉ The third number is associated with everyone else who is not a part of the user or group

$ chmod 744 script.sh

Octal Notation Octal Notation   Symbolic Representation 
0 No Permission --- 
Execute Permission Only  --x 
Write Permission Only  -w- 
Write and Execute Permissions (1+2)=3  -wx 
Read Permission Only  r-- 
Read and Execute Permissions (1+4)=5  r-x 
Read and Write Permissions (2+4)=6  rw- 
Read, Write and Execute Permissions, Means Full Permissions (1+2+4)=7  rwx 

From the table you will see that the rights given will be as

◉ -rwxr- - r- - rwx for user
◉ r - - for the group (read only)
◉ r - - for others (read only)

Other ways of using chmod are

◉ chmod a-w first.txt

This means all users have no write access to the file first.txt.

◉ chmod u + x script.sh

The owner of script.sh can execute the file

12. chown –

This command is used to change the ownership of a file/folder or even multiple files/folders for a specified user/group.

Basic Linux Commands, Linux Exam Prep, Linux Preparation, Linux Certification, LPI Preparation, LPI Guides, LPI Career

chown owner_name file_name

$ chown user1 script.sh

Assume that if you are a user named user1 and you want to change ownership to root use “sudo” before syntax.

$ sudo chown root script.sh

13. cat -

The cat command (short for “concatenate “) is one of the most frequently used commands in Linux. cat command allows you to create single or multiple files, view contents of file, concatenate files and redirect output in terminal or files.

$ cat file.txt
$ cat file1.txt file2.txt

Output will show the entire contents of the file(s).

14. echo –

This command is used to display a text or a string to the standard output or a file.

$ echo “This is an article on basic linux commands”

This is an article on basic linux commands

The echo –e option acts as an interpretation of escape characters that are back-slashed.

$ echo –e “This is an article is for beginners. \nIt is on basic linux commands

Will display the output as

This is an article is for beginners.
It is on basic linux commands

\n the newline character is interpreted by the echo –e command

15. wc -

The wc (word count) command in Linux operating system is used to find out the number of new lines, word count, byte and characters count in a file specified by the file arguments.

wc [options] filenames.

$ wc –l readme.txt

Shows the output as - 120 readme.txt

◉ wc -l : Prints the number of lines in a file.
◉ wc -w : prints the number of words in a file.
◉ wc -c : Displays the count of bytes in a file.
◉ wc -m : prints the count of characters from a file.
◉ wc -L : prints only the length of the longest line in a file.

16. man –

This command is used to view the on-line reference manual pages for commands/programs.

$ man grep
$ man mkdir

17. history –

This command is used to show previously used commands or to get information about the commands executed by a user.

$ history

18. clear –

This command lets you clear the terminal screen.

$ clear

19. apt –get

apt -get is a powerful and free front-end package manager for Debian/Ubuntu systems. It is used to install new software packages, remove available software packages, upgrade existing software packages as well as upgrade the entire operating system. apt – stands for advanced packaging tool.

$ sudo apt-get update

20. reboot –

This command may be used to halt, power-off or reboot a system as follows.

$ reboot

Source: hackr.io

Related Posts

0 comments:

Post a Comment