Simple Linux commands for beginners

Simple Linux commands for beginners

Linux is a widely used Operating system by developers especially in production environments


Linux is a family of open-source Unix-like operating systems based on the Linux kernel,an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution.

The user interface, also known as the shell, is either a command-line interface (CLI), a graphical user interface (GUI), or controls attached to the associated hardware, which is common for embedded systems. For desktop systems, the default user interface is usually graphical, although the CLI is commonly available through terminal emulator windows or on a separate virtual console.

CLI shells are text-based user interfaces, which use text for both input and output. The dominant shell used in Linux is the Bourne-Again Shell (bash), originally developed for the GNU project.

Let's learn some important commands for linux

ls 

Use the "ls" command to know what files are in the directory you are in. You can see all the hidden files by using the command “ls -a”.

ls
Lists all files and directories in the present working directory
ls -R
Lists files in subdirectories as well
ls -a
Lists hidden files as well
ls -al
Lists files and directories with detailed information like permissions,size, owner, etc.





cd

Use the "cd" command to go to a directory. For example, if you are in the home folder, and you want to go to the downloads folder, then you can type in “cd Downloads”. 


NOTE

Remember, this command is case-sensitive, and you have to type in the name of the folder exactly as it is. But there is a problem with these commands. Imagine you have a folder named “Linux Commands”. In this case, when you type in “cd Linux Commands”, the shell will take the second argument of the command as a different one, so you will get an error saying that the directory does not exist. Here, you can use a backward slash. That is, you can use “cd Linux\ Commands” in this case. Spaces are denoted like this: If you just type “cd” and press enter, it takes you to the home directory. To go back from a folder to the folder before that, you can type “cd ..” . The two dots represent back.

pwd

When you first open the terminal, you are in the home directory of your user. To know which directory you are in, you can use the “pwd” command. It gives us the absolute path, which means the path that starts from the root. The root is the base of the Linux file system. It is denoted by a forward slash( / ). The user directory is usually something like "/home/username".


touch

The touch command is used to create a file. It can be anything, from an empty txt file to an empty zip file. For example, “touch new.txt”.

mkdir

Use the mkdir command when you need to create a folder or a directory. For example, if you want to make a directory called “Linux”, then you can type “mkdir Linux”. Remember, as told before, if you want to create a directory named “Linux Commands”, then you can type “mkdir Linux\ Commands”.

rm

Use the rm command to delete files and directories.  Use "rm -r" to delete just the directory. It deletes both the folder and the files it contains when using only the rm command.

rmdir 

Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm.


mv

Use the mv command to move files through the command line. We can also use the mv command to rename a file. For example, if we want to rename the file “old” to “new”, we can use “mv old new”. It takes the two arguments, just like the cp command.

cp

Use the cp command to copy files through the command line. It takes two arguments: The first is the location of the file to be copied, the second is where to copy.

eg. “cp /home/username/oldfile /home/seconduser/”


cat

Use the cat command to display the contents of a file. It is usually used to easily view programs.

grep

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). 

Eg: “grep [options] pattern [files]”

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

-A n : Prints searched line and n lines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.


type

The type command is used to describe how its argument would be translated if used as commands. It is also used to find out whether it is a built-in or external binary file.


which

which command in Linux is a command which is used to locate the executable file associated with the given command by searching it in the path environment variable. It has 3 return status as follows:
0 : If all specified commands are found and executable.
1 : If one or more specified commands is nonexistent or not executable.
2 : If an invalid option is specified.


help

help command just displays information about shell built-in commands. 


man

To know more about command and how to use it, use the man command. It shows the manual pages of the command. For example, “man cd” shows the manual pages of the cd command.


echo

echo command in linux is used to display lines of text/string that are passed as an argument . This is a built-in command that is mostly used in shell scripts and batch files to output status text to the screen or a file. 


chmod

Use chmod to make a file executable and to change the permissions granted to it in Linux. Imagine you have a python code named numbers.py in your computer. You'll need to run “python numbers.py” every time you need to run it. Instead of that, when you make it executable, you'll just need to run “numbers.py” in the terminal to run the file. To make a file executable, you can use the command “chmod +x numbers.py” in this case. You can use “chmod 755 numbers.py” to give it root permissions or “sudo chmod +x numbers.py” for root executable.

sudo

A widely used command in the Linux command line, sudo stands for "SuperUser Do". So, if you want any command to be done with administrative or root privileges, you can use the sudo command.

chown

chown command is used to change the file Owner or group. Whenever you want to change ownership you can use chown command. 

tar

Use tar to work with tarballs (or files compressed in a tarball archive) in the Linux command line. It has a long list of uses. It can be used to compress and uncompress different types of tar archives like .tar, .tar.gz, .tar.bz2,etc. It works on the basis of the arguments given to it. For example, "tar -cvf" for creating a .tar archive, -xvf to untar a tar archive, -tvf to list the contents of the archive, etc. 

Different users in the operating system have ownership and permission to ensure that the files are secure and put restrictions on who can modify the contents of the files. In Linux there are different users who use the system:  

Each user has some properties associated with them, such as a user ID and a home directory. We can add users into a group to make the process of managing users easier.

A group can have zero or more users. A specified user can be associated with a “default group”. It can also be a member of other groups on the system as well.

Ownership and Permissions: To protect and secure files and directory in Linux we use permissions to control what a user can do with a file or directory. Linux uses three types of permissions:  

  • Read: This permission allows the user to read files and in directories, it lets the user read directories and subdirectories stores in it.
  • Write: This permission allows a user to modify and delete a file. Also it allows a user to modify its contents (create, delete and rename files in it) for the directories. Unless the execute permission is not given to directories changes does do affect them.
  • Execute: This permission on a file allows it to get executed. For example, if we have a file named php.sh so unless we don’t give it execute permission it won’t run.

Types of file Permissions:  

  • User: These type of file permission affect the owner of the file.
  • Group: These type of file permission affect the group which owns the file. Instead of the group permissions, the user permissions will apply if the owner user is in this group.
  • Other: These type of file permission affect all other users on the system.

That's it for this quick tutoial. I hope it was helpful to you.