If you’re new to Linux, I recommend you get started with the basic Linux commands on the terminal. Once you’re familiar with the commands, you’ll find that Linux and Mac have a lot in common, but the commands on Windows have less in common. We will find that out in the next article.
Basic linux commands
Linux novices are often surprised at the amount of time longtime Linux users spend on the command line. There are many good reasons why terminals are so popular. For system administrators, this is by far the most flexible and powerful way to give commands to your computer. You can type more commands with more options, your desktop environment and applications are controllable via commands, and the command line is much faster than GUI. You can also write repetitive tasks, create Alias, and write shell functions to improve performance.
If you are thinking of switching from Windows to Linux or just want to play with Windows Subsystem for Linux, you will find the command line as a very refreshing and useful environment. There are many commands available. Therefore, there are also many new people who feel overwhelmed when using Linux.
Many commands on Linux have two-letter names that are quite confusing and difficult to remember. But there are also commands that you might recognize if you have ever used cmd on Windows. For example, commands like ping and netstat have the same name on Windows and Linux.
Exploring and memorizing commands for tasks you normally do is often a great way to get used to Linux. But in this article, I have filtered out the 10 most commonly used commands on Linux, basic and simple commands will not be mentioned.
Changing directories with cd is the same on both platforms, and ls on Linux is the same as dir in Windows. The Linux commands I have chosen are very useful and similar to Windows that you will be familiar with if you have used the Windows cmd.
1. Display the contents of the file: cat
The cat command is a copy of the command type
on Windows. It shows the contents of the file in the terminal. You can also join multiple files into a single file.
In the file “verse-1.txt” there is a poem. We can check the contents of the file with the command cat
:
cat verse-1.text
To see the contents of another file, we just need to type the name of that file:
cat verse-2.txt
To view both files at once with a single command, we pass both filenames to cat
:
cat verse-1.txt verse-2.txt
To create a new file containing the contents of two files, we can use the following command:
cat verse-1.txt verse-2.txt > newfile.text
2. Associate the software with the file types: mimeopen
The mimeopen command works like the Windows assoc command. It associates a default program with a file type.
Windows uses file extensions to identify file types. Linux does it differently. It determines the file type by looking at the contents of the text file or the digital signature contained in the first few bytes of the digital file.
To establish a file association, use the command mimeopen
with the -d option (default required) and enter the name of the file type you want to link.
mimeopen -d kernel-article.mm
If the app you want to use is listed, enter its number. In this example, the app we want is not listed. So we can enter “6” and then type the command to launch the application. I want to open this type of file with FreeMind, a mind mapping application.
The application will be launched and open the file you are working on in the terminal.
This application will be used to open those types of files.
3. Set file properties: chmod
Command chmod
set the properties for the file, just like the command attrib
on Windows. On Linux, you can set permissions to read files, write files, and execute files. These attributes can also be applied to directories.
Using the -l (long format) option with the ls command displays a list of characters for each file, which looks like this.
-rwxrwxrwx
If the first character is a hyphen “-” then the list represents a file. If the first character is “d”, the list represents a directory.
The rest of the string is made up of three groups of three characters. From the left, the first three characters show the owner’s file permissions, the middle three show the group’s file permissions, and the rightmost three characters show the permissions for other users .
In each group, from left to right, the characters represent read, write, and execute permissions. If there is “r”, “w”, or “x”, that attribute is enabled. If a letter has been replaced with a dash “-” then that permission is not enabled.
An easy way to use chmod
is to represent each of the right triples by one digit. By providing a three-digit number for chmod
, you can set permissions for owners, groups, and other users. The leftmost digit represents the owner. The middle digit represents the group. The rightmost digit represents the other digits. The digits range from 0 to 7.
- 0: No permissions
- 1: Enforce permissions
- 2: Permission to write
- 3: Write and execute permissions
- 4: Permission to read
- 5: Read and execute permissions
- 6: Read and write permission
- 7: Read, write and execute permissions
The file “howtogeek.txt” has full permissions set up for everyone. We will change to full permissions for owner (7), read and write (6) for the group, and read (4) for all other users.
ls -l howtogeek.text
chmod 764 howtogeek.txt
ls -l howtogeek.text
4. Find string: grep
Windows has a command find
. It searches text files for matching strings. The Linux equivalent is grep
. The versatility and sophistication of grep may surprise you, but its fundamentals are simple. It scans through the text for matching strings.
To search in the file “/etc/passwd” for string entries with “dave”, we use the following command:
grep dave /etc/passwd
The string doesn’t need to be a complete word. In fact, you can search using a set of wildcards and regular expressions.
grep ove verse-1.txt
You can also use grep to search through the output from another command. The ps command lists running processes. The -e (everything) option lists all processes. We can convert it to grep and look for processes with “naut” in the name.
ps -e | grep naut
5. Find file diff: diff
Command diff
on Linux, same as command fc
on Windows. It compares two files and finds the difference between them. This is especially useful when you are comparing newer and older versions of source code, but it is also useful when you are testing two versions of any large text file.
To compare two files, enter the names of the two files as follows:
diff core.c old-core.c
Changes are lines that have been added, lines that have been removed, or lines that have been modified. Each change is described in shorthand. The shorthand lists the line number (or line range) in the first file, a letter, then the line number or numbers in the second file. Letters can be:
- c: The line in the first file needs to be changed to match the line in the second file.
- d: The line in the first file must be removed to match the second file.
- a: Additional content must be added to the first file to match the second file.
To see a side-by-side comparison, use the -y (sideways) option. Using the -W (width) option is often more useful to limit the width of the output.
diff -y -W 70 core.c old-core.c
The lines are displayed side by side. Lines changed, added, or removed are displayed with a symbol in the center of the screen. The symbols can be:
- |: One line has been changed in the second file.
- <: A line has been removed from the second file.
- >: The line that was added to the second file is not present in the first file.
6. Find your IP address: ip addr
Command ipconfig
of Windows displays information about your network connection and IP address. On Linux, there is a similar command as ip
or ifconfig
. It has more objects and parameters, such as addr, that display information about your IP address.
ip addr
You will find your IP address in the output. In this example, the IP address shown is 192.168.1.40/24. That means, the IP address is 192.168.1.40 and the mask is 255.255.255.0. “/24” is a classless interdomain routing symbol for a network mask with three sets of 8 bits set to 1.
There is a lot of information available through the command ip
.
7. Discover network information: netstat
Command netstat
of Windows with the same name on Linux. Command netstat
of Linux displays information about your network connections, including sockets and other data structures. If netstat
is not installed on your computer, you can install it using the distribution’s package manager.
To see which TCP/IP sockets are listening, use the -l (listening) and -t (TCP/IP) options:
netstat -lt
8. Troubleshoot connection: ping
Another command with the same name on Windows is ping
. This is a great tool to check network connections and see if there is a valid connection between networked devices.
It sends ICMP ECHO_REQUEST packets to the remote device and listens for the response. It then tells you whether a connection can be made and the average packet time in milliseconds.
You can use ping with IP address or domain and network.
ping www.howtogeek.com
To send a specific number of ping requests, use the -c (count) option.
ping -c 4 www.howtogeek.com
9. Exploring hardware details: lshw
Windows command line users will be familiar with the command systeminfo
. Command lshw
of Linux provides the same feature. You may need to install this command on some distributions.
There is a lot of output from this command. Also, use sudo with this command so it has access to system files and streams.
sudo lshw | less
For an overview, use the -short option.
sudo lshow -short
10. Determine Package Route: traceroute
Command traceroute
in Linux is similar to the command tracert
of Windows. This command counts hops from the router as packets travel from your computer to the remote device. Some devices do not reveal much information about these packages. These secret devices are displayed as a single line of asterisks “*” in the output.
You can use traceroute
with IP address or domain name and device.
traceroute www.blarneycastle.ie
A world of difference
Windows and Linux are two different platforms, but they have some commands in common — and even command names. That’s not too surprising. Windows sockets come from Unix, so there is bound to be some overlap in terminology in the two operating systems.