DEV Community

Cover image for Linux basic commands
Srinivas karnati
Srinivas karnati

Posted on

Linux basic commands

Linux is an extremely common operating system. 90% of all cloud computing systems run on Linux. Most servers and technology-embedded devices run on Linux. It’s the Linux that manages and orchestrates the computing power in most of the cloud.

Linux

Linux is an operating system's kernel. It was actually created by Linus Torvalds from Scratch. Linux is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name! There are several Linux Distributions, commonly called “distros”.

  • Ubuntu Linux

  • Red Hat enterprise Linux

  • Linux Mint

  • Debian

  • Fedora

*Linux Shell * is basically a shell is a program that receives commands from the user and gives it to the OS to process, and it shows the output.

Basic Commands

  • man - The man command is a built-in manual for using Linux commands (short for manual page) . The man page includes a command description, applicable options, flags, examples, and other informative sections. man ls gives info about ls command

  • clear- It is a simple yet frequently used command which basically clears the screen.

  • sudo - Just like we have run as administrator in Windows, sudo allows you to run a command as Super User ( admin privileges).

  • mkdir- this will allow us to create a folder in our system. With the following command we can create a folder in our home directory called LinuxCommands mkdir LinuxCommands.

  • cd- allows us to change directory, so for us to move into our newly created directory we can do this with cd LinuxCommands. If we want to get back to where we started we can use cd .. ( one Level back)

  • pwd- print working directory, pwd gives us the print out of the current directory.

  • touch- We can create files using the touch command if we were to run touch srinivas.htmlthis would create a file named srinivas.html

  • ls- This is going to list the all the files and folders in the current directory. ls -a lists all files along the hidden files.

  • mv- This is going to allow you to move your files. mv LinuxCommands Basics will move your file to the Basics folder.

  • cp- If I just want to copy files from one folder to another, simply put its very similar to the mv command but we use cp .

  • rm-simply rm Srinivas.html will remove the file. We will also use quite a bit rm -R which will recursively work through a folder or location.

  • rmdir - allows for us to remove the directory, if we run rmdir LinuxCommands , the folder will be removed.

  • echo- The echo command prints out arguments as the standard output. echo "Hello World" prints out - Hello world on screen

  • cat- We can use cat to see the contents inside the file.

  • grep-If we have a large file and we don't want to read every line then grep is your friend, this will allow us to search your file for a specific word using cat main.go | grep "Println" . It outputs all the lines that contain "println".

  • history-used to find out all those commands we ran previously.

  • useradd- If we want to add new users to our system, we can do this using useradd as a sudo operation. we can add a new user with sudo useradd NewUser

  • groupadd- Creating a group also a sudo operation and we can use sudo groupadd newgroup

  • usermod- is used to change the properties of a user. If we want to add our new user to the group we created, we can do this by running sudo NewUser usermod -a -G newgroup. -a is add and -G is group name.

Permissions

  • ls -l = Print files in a long listing format, you can see ownership and permissions of the file

Ownership:

  • sudo chown [username]:[groupname] [filename] = Change ownership
  • sudo chown tom:admin test.txt = Change ownership of 'test.txt' file to 'tom' and group 'admin'
  • sudo chown admin test.txt = Change ownership of 'test.txt' 'admin' user
  • sudo chgrp devops test.txt = Make 'devops' group owner of test.txt file

Possible File Permissions (Symbolic):

  • r = Read
  • w = Write
  • x = Execute
  • '-' = No permission

Change File Permissions for different owners

File Permissions can be changed for:

  • u = Owner
  • g = Group
  • o = Other (all other users)

Minus (-) removes the permission

  • sudo chmod -x api = Takes 'execute' permission away for 'api' folder from all owners
  • sudo chmod g-w config.yaml = Takes 'write' permission away for 'config.yaml' file from the group

Plus (+) adds permission

  • sudo chmod g+x config.yaml = Add 'execute' permission for 'config.yaml' file to the group
  • sudo chmod u+x script.sh = Add 'execute' permission for 'script.sh' file to the user
  • sudo chmod o+x script.sh = Add 'execute' permission for 'script.sh' file to other

Change multiple permissions for an owner

  • sudo chmod g=rwx config.yaml = Assign 'read write execute' permissions to the group
  • sudo chmod g=r-- config.yaml = Assign only 'read' permission to the group

Changing permissions with numeric values

Set permissions for all owners with 3 digits, 1 digit for each owner Absolute vs Symbolic Mode

  • 0 = No permission
  • 1 = Execute
  • 2 = Write
  • 3 = Execute + Write
  • 4 = Read
  • 5 = Read + Execute
  • 6 = Read + Write
  • 7 = Read + Write + Execute <!-- -->
  • sudo chmod 777 script.sh = rwx (Read, Write and Execute) permission for everyone for file 'script.sh'
  • sudo chmod 740 script.sh = Give user all permissions (7), give group only read permission (4), give other no permission (0)

For more commands on Linux

Top comments (2)

Collapse
 
exoclb profile image
Muhammad Mukhlis

Thanks

Collapse
 
smkeller14 profile image
Sydney

Good read. Thank you for taking the time to write this article. I use many of these commands daily but the file permission commands are very useful!