10 Basic Linux Commands for Beginners


    Just getting started on Linux? Making yourself comfortable with the command line is essential. If you’re already familiar with command line utilities, you’ll find that Linux and Mac share much in common, but Windows commands have fewer similarities. Let’s take a look.

    The Linux Command Line

    Newcomers to Linux are often taken aback at just how much time seasoned Linux users spend on the command line. There are many good reasons why the terminal window is so popular. For system administration, it is by far the most flexible and powerful way to issue commands to your computer. You have more commands at your fingertips—and with more options—than your desktop environment and applications can provide, and the command line is much faster. You can also script repetitive tasks, create aliases, and write shell functions to crank up the efficiency.

    If you are thinking of moving to Linux from Windows, or just want to play around with the Windows Subsystem for Linux, you’ll find the command line a rich and versatile playground. There are a lot of commands available. So many in fact, that it is overwhelming for people starting out with Linux.

    It doesn’t help that many Linux commands have obscure, two-letter names. But there are other commands with names that you might recognize if you’ve gathered any experience in a Windows command window. Commands like ping and netstat for example, have the same name on Windows and Linux.

    Discovering and memorizing commands for tasks you commonly perform on your usual computing platform is a great way to start to feel at home on a new platform. We’ve gathered a collection of 10 Windows commands and we’ve provided the Linux equivalent. We’ve skipped the very basic, simple commands.

    RELATED: 37 Important Linux Commands You Should Know

    Changing directory with cd is the same on both platforms, and ls on Linux does what dir does in Windows. That’s two things less to worry about but they don’t really move you closer to anything that’s genuinely productive. The Linux commands we’ve selected are useful and they have a direct Windows equivalent you’ll already be familiar with if you’ve used the Windows command prompt.

    1. Display the Contents of a File: cat

    The cat command is the counterpart to the Windows type command. It displays the contents of a file in the terminal window. You can also concatenate multiple files into a single file. It’s the “cat” in the middle of “concatenate” that gives this command its name.

    We’ve got a verse of a poem in a file called “verse-1.txt.” We can examine its contents with the cat command by passing the filename as a command-line parameter.

    cat verse-1.text

    To see the contents of another file we simply pass in the name of the other file:

    cat verse-2.txt

    using cat to display file contents

    To see both files at once with a single command, we pass both filenames to cat:

    cat verse-1.txt verse-2.txt

    Using cat to displayu the contents of two files at once

    To create a new file containing the contents of the two files, we can use redirection to send the output from cat into a new file.

    cat verse-1.txt verse-2.txt > newfile.text

    Using cat to add two files into a new file

    2. Associate Actions to File Types: mimeopen

    The mimeopen command acts like the Windows assoc command. It associates a default program with a file type.

    Windows uses file extensions to identify file types. Linux does things differently. It determines the type of file by looking at the contents of text files or the digital signature contained in the first few bytes of digital files.

    To establish a file association, use mimeopen with the -d (ask for default) option, and pass in the name of a file of the type you wish to set an association for.

    mimeopen -d kernel-article.mm

    If the application you want to use is listed, enter its number. In this example, our desired application isn’t listed. We can enter “6” and then type the command that launches the application. We want to open this type of file with FreeMind, a mind-mapping application.

    providing an application mae to the mimeopen menu option 6

    The application is launched for you, opening the file you passed on the command line.

    Application opened by mimeopen

    That application will now be used to open files of that type.

    3. Set File Attributes: chmod

    The chmod command sets file attributes, much like the Windows attrib command does. On Linux you can set permissions for reading files, writing to files, and executing files, with a different set of attributes for the file owner, the user group the file belongs to, and everyone else. These attributes can also be applied to directories.

    Using the -l (long format) option with the ls command shows a list of characters for each file, that looks like this.

    -rwxrwxrwx

    If the first character is a hyphen “-” it means the listing represents a file. If the first character is a “d” the listing represents a directory.

    The rest of the string is made up of three groups of three characters. From the left, the first three show the file permissions of the owner, the middle three show the file permissions of the group, and the rightmost three characters show the permissions for others.

    In each group, from left to right, the characters represent the read, write, and execute permissions. If an “r”, “w”, or “x” is present that attribute has been set. If a letter has been replaced by a hyphen “-” that permission is not set.

    An easy way to use chmod is representing each set of three permissions with a digit. By giving a three-digit number to chmod you can set the permissions for the owner, group, and others. The leftmost digit represents the owner. The middle digit represents the group. The rightmost digit represents the others. The digits range from zero to seven.

    • 0: No permission
    • 1: Execute permission
    • 2: Write permission
    • 3: Write and execute permissions
    • 4: Read permission
    • 5: Read and execute permissions
    • 6: Read and write permissions
    • 7: Read, write and execute permissions

    The file “howtogeek.txt” has full permissions set for everyone. We’ll change that to full permissions for the owner (7), read and write (6) for the group, and read (4) for all others.

    ls -l howtogeek.text
    chmod 764 howtogeek.txt
    ls -l howtogeek.text

    Setting the permissions ona file with chmod

    4. Find a String: grep

    Windows has the find command. It searches text files for matching strings. The Linux equivalent is grep. The flexibility and sophistication of grep can be overwhelming, but its basic principles are simple. It scans through text looking for matching strings.

    To search through the “/etc/passwd” file for entries matching “dave” we’d use this command:

    grep dave /etc/passwd

    Using grep to search a text file

    The string need not be a whole word. In fact, you can search using a rich set of wildcards and regular expressions.

    grep ove verse-1.txt

    Using grep to search for a partial word

    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 pipe this into grep and look for processes with “naut” in their name.

    ps -e | grep naut

    Piping the output of ps into grep

    To see a more detailed description of grep and its regular expressions, check out our full article on grep.

    RELATED: How to Use the grep Command on Linux

    5. Find File Differences: diff

    The diff command is to Linux what the fc command is to Windows. It compares two files and highlights the differences between them. This is particularly useful when you’re comparing newer and older versions of program source code, but it is equally useful when you’re checking two versions of any sizeable text file.

    To compare two files, pass their names on the command line:

    diff core.c old-core.c

    Using diff to compare two files

    The changes are either lines that have been added, lines that have been removed, or lines that have been modified. Each change is described in a form of shorthand and then displayed. The shorthand lists the line number (or range of line) in the first file, a letter, and then the line number or numbers in the second file. The letter 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 deleted to match the second file.
    • a: Extra content must be added to the first file to make it match the second file.

    To see a side-by-side comparison, use the -y (sideways) option. It’s often useful to use the -W (width) option to limit the width of the output otherwise wrapped lines can make the output difficult to interpret.

    diff -y -W 70 core.c old-core.c

    Using diff with a side by side view

    The lines are shown side by side. Changed, added or deleted lines are indicated by a symbol in the middle of the display. The symbols can be:

    • |: A line that has been changed in the second file.
    • <: A line that has been deleted from the second file.
    • >: A line that has been added to the second file that is not in the first file.

    6. Find Your IP address: ip addr

    The Windows ipconfig command displays information about your network connection and your IP address. To accomplish the same thing on Linux, use the ip command. It takes many objects and options that alter its behavior, such as addr, which displays information about your IP address.

    ip addr

    Using ip addr to find an ip address

    You’ll find your IP address in the output. In this example, the IP address is displayed as 192.168.1.40/24. That means the IP address is 192.168.1.40 and the network mask is 255.255.255.0. The “/24” is the Classless Inter-Domain Routing notation for a network mask with three sets of 8 bits set to 1.

    There’s a wealth of information available through the ip command. Refer to our full article to learn more.

    RELATED: How to Use the ip Command on Linux

    7. Uncover Network Information: netstat

    The counterpart to the Windows netstat command has the same name on Linux. The Linux netstat command displays information about your network connections, including sockets and other data structures. If netstat isn’t already installed on your computer you can install it using the package manager for your distribution.

    To see the listening TCP/IP sockets, use the -l (listening) and -t (TCP/IP) options:

    netstat -lt

    Using netstat to look at listening TCP/IP sockets

    RELATED: How to Use netstat on Linux

    8. Troubleshoot Connectivity Issues: ping

    Another command that has the same name as its Windows equivalent, ping, probably needs little introduction. It’s a great tool to test network connections and to see whether there is a valid route between networked devices.

    It sends ICMP ECHO_REQUEST packets to the remote device and listens for a response. It then tells you whether a connection could be made, and the average round-time trip in milliseconds.

    You can use ping with IP addresses or domain and network names.

    ping www.howtogeek.com

    Using ping to verify a remote device is responding

    To send a specific number of ping requests, use the -c (count) option.

    ping -c 4 www.howtogeek.com

    Using ping to send a specific number of packets

    9. Discover Hardware Details: lshw

    Windows command line users will be familiar with the systeminfo command. The Linux lshw command provides the same kind of functionality. You might need to install this command on some distributions.

    There’s a lot of output from this command. It’s usually more convenient to pipe the output into less . Also, use sudo with this command so that it has permission to access system files and streams.

    sudo lshw | less

    To get a condensed overview, use the -short option.

    sudo lshow -short

    There are many different utilities that show different subsets of hardware information. Check out our full article for a description of several of them.

    RELATED: How to List Your Computer’s Devices From the Linux Terminal

    10. Determine a Packet’s Route: traceroute

    The Linux traceroute command is the analog of the Windows tracert command. It’s another one you might need to install on your distribution. It counts the hops from router to router as packets make their way from your computer to the remote device. Some devices don’t reveal much about themselves. These secretive devices are shown as a line of asterisks “*” in the output.

    You can use traceroute with IP addresses or domain and device names.

    traceroute www.blarneycastle.ie

    Countin g routing hops with traceroute

    A World of Differences

    Windows and Linux are worlds apart and yet they share some common commands—and even command names. That’s not too surprising. Windows sockets came from Unix, so there’s bound to be some overlap in that terminology in the two operating systems.

    And some utilities are so useful they’re bound they appear on all platforms. The ping command, for example, is found on almost every platform you can think of.

    Coming to Linux from Windows is a culture shock, there’s no escaping that. But that’s kind of the point. If it was just the same you might as well stick with Windows. But a few familiar faces can definitely help you ease into the Linux world.





    Source link

    Previous articleLBX Token – A Blockchain Technology Built to Achieve Environmental and Social Progress
    Next articleApple Finally Falls Victim to Never-Ending Supply Chain Crisis