Learn Some LINUX Commands

In this tutorial, we will learn some LINUX commands; what they are used for and how to use them.

Let’s Dig In

Today we will create a text file and input the following contents.

To create text file, open terminal and type;

cat > example.txt

NOTE: You are not returned to the prompt after pressing Enter. Instead the cursor is placed on the next line for you to enter text into the file you have created. Type your lines of text , separating each line with Enter. Once you are done, press Ctrl + D to exit the file and return to the prompt.

Now To The Commands

A) cut

This command is used to remove selected fields of each line of a file. It processes the file vertically, that is, the output of the cut command is a single or multiple columns. Simply put, cut command is used to process a file in order to extract either a column of characters or some fields.

Syntax

cut OPTION… [FILE]…

Let’s see how cut command is used.

  1. EXTRACTING A COLUMN(S) OF CHARACTERS

To extract a column(s) of characters the following syntax is used:

cut -c [RANGE] [FILENAME]

cut command is selecting the specific characters specified by RANGE from the file FILENAME.

a) Displaying a column of characters

Let’s display the second character of each line from the file example.txt.

cut -c 2 example.txt

b) Displaying a group of columns of characters

To extract a group of columns, the cut command needs to specify a range, Start to End. Let’s give it a try by displaying the first three characters of each line of the file.

cut -c 1–3 example.txt

Another example is when we want to display characters from a particular column to the last column. For example, we may want to display from the 7th column to the end.

cut -c 7- example.txt

NOTE: A whitespace is considered as a character

We can also get the first 7 characters of each line as indicated in the following example.

cut -c -7 example.txt

What do you think happens when we fail to state the start and the end of the range? Let’s see.

cut -c — example.txt

The error above shows that there has to be a valid range.

2. EXTRACTING A FIELD(S) FROM A FILE

To extract a field from a file, we would need a delimiter also known as a column separator, based on which the file will be divided into columns and we can extract any of them.

Syntax

cut -d [DELIMITER] -f [RANGE] [FILENAME]

cut command is instructed to use a particular delimiter with option -d and then extract certain fields using option -f. In this case, we will use the following csv file as an example:

a) Displaying a specific field from a file

In the above csv file, the delimiter will be a comma (,). Let us now list the names of the students enrolled in our school, in this case, field number 2.

cut -d ‘,’ -f 2 students.txt

b) Displaying multiple fields from a file

We can now confidently display more than one field. For example, let us include ‘Gender’ and ‘Age’ fields to the student names.

NOTE: We must specify the range; the start and the end.

Cut -d ‘,’ -f 2–4 students.txt

We can now conclude that the Input Delimiter = Output Delimiter

Let us now look at a variation. What if we need to choose the columns ‘Student ID’, ‘Student Name’, ‘Class’ and ‘Grade’? How will we go about it? We will need to display two ranges.

cut -d ‘,’ -f 1–2,5–6 students.txt

c) Changing the delimiter in the output

We have seen in the above examples that Input Delimiter = Output Delimiter. What if we change the output delimiter from ‘,’ to ‘|’?

cut -d ‘,’ -f 2–4 — output-delimiter=’|’ students.txt

d) Not displaying certain columns

cut command will display all the fields except the specified field, if we use — complement as an option.

cut -d ‘,’ — complement -f 6 students.txt

B) netstat

The netstat command, which means network statistics, can show you information about your network including statistics on connections to and from others on the network, used network interfaces, services, ports, and routing tables. It also helps network administrators to keep an eye on the invalid or suspicious network connections.

Syntax

netstat [options]…

Below are examples of what you can use netstat command to do.

  1. SHOWING ADDRESSES THAT ARE CONNECTED TO YOUR SYSTEM

netstat shows who is connected to your system either through an incoming or outgoing connection, that is, whether it is your system which initiated the connection or the other system.

netstat -a

The above command shows all connections from different protocols like TCP, UDP and UNIX sockets.

“Foreign Address” column shows where the connection is coming from, and “Local Address” shows what on the local machine is it connected to.

The next command shows all the TCP (-t) and UDP (-u) connections.

netstat -tua

If you want to display only IP numbers and turn off domain names (hostnames), add the -n option. This is as shown below:

netstat -tuan

If you want it to display this continuously to see as connections come and go, add the -c option.

Netstat -tuanc

To list out only TCP connections use the t options.

Netstat –at

To list out only UDP connections use the u option.

Netstat -au

2. DISPLAYING LISTENING PORTS

Use the -l option If you’d like to see which services are actually listening for incoming connections.

netstat -l

To limit this to only TCP, use the t option

netstat -lt

… and u option for UDP.

netstat -lu

3. DISABLE REVERSE DNS LOOKUP FOR FASTER OUTPUT

The netstat command by default tries to find out the hostname of each IP address in the connection by doing a reverse dns lookup. This slows down the output. If you need to know the IP address but not the host name, suppress the hostname lookup with the n option.

netstat -ant

4. FINDING THE PORT USED BY A PROGRAM

We can combine the netstat command with another command called grep,to make it easier to find which port is used by a program.

netstat -ap | grep zn

5. PRINTING STATISTICS

The netstat command can also print out network statistics. Just run the netstat command with the -s option.

netstat -s

6. DISPLAYING THE ROUTING TABLE

The netstat command enables you to see the kernel IP routing table being used on your system using the -r option.

netstat -r

7. DISPLAYING MULTICAST GROUP MEMBERSHIP INFORMATION

This information is displayed for both IPv4 and IPv6 and can be retrieved using -g option with this command.

Netstat -g

8. LISTING OUT ONLY LISTENING CONNECTIONS

A network daemon or service keeps an open port to listen for incoming connections. To view only listening ports use the l options.

netstat -tnl

9. GETTING PROCESS NAME/PID AND USER ID

When viewing the open ports and connections, its often useful to know the process name/pid which has opened that port or connection. The process details are made available by the ‘p’ option. When using the p option, netstat must be run with root privileges, otherwise it cannot detect the pids of processes running with root privileges and most services like http and ftp often run with root privileges.

sudo netstat -nlpt

Use the e option along with the p option to get the username/uid owning that particular process.

sudo netstat -ltpe

10. PRINTING NETWORK INTERFACES

The netstat command can also print out the information about the network interface by use of the i option.

netstat -i

C) top

top command is used to monitor system usage and to make any change for improving system performance. The top command provides a dynamic real-time view of a running system. It displays system summary information and also a list of tasks currently being managed by the Linux kernel. The types of system summary information shown and the types, order and size of information displayed for tasks are all user configurable and that configuration can be made persistent across restarts.

Let us take a look at the below examples of usage of top command.

  1. WITHOUT ANY ARGUMENTS

The following are the various system-related features of top command.

You can enable/disable the first line by pressing “l” once top is running. This line tells you about the uptime of your system along with load average value.

You can enable/disable the 2nd and 3rd lines by pressing “t”. These lines gives us a brief detail of all the tasks running/sleeping/stopped currently in the system along with the CPU usage.

You can enable/disable the 4th and 5th lines by pressing “m”. These lines shows your memory(RAM and swap) usage and capacity.

2. ARRANGE TASKS WITH HIGH TO LOW CPU USAGE

Press “P” or “shift+p” once top is running to arrange all the tasks with High to Low CPU Usage as shown below:

3. ARRANGE TASKS WITH HIGH TO LOW MEMORY USAGE

Press “M” or “shift+m”once top is running to arrange all the tasks with High to Low Memory Usage as shown below:

D) ssh

SSH (Secure Shell or Secure Socket Shell) client utility in UNIX or LINUX server is used to logging into a remote host and execute commands on the remote machine. The ssh command provides a secure connection between two hosts over a insecure network. SSH is a suite of three utilities — slogin, ssh, and scp — that are secure versions of the earlier UNIX utilities, rlogin, rsh, and rcp. Both ends of the client/server connection are authenticated using a digital certificate, and passwords are protected by being encrypted.

SSH uses RSA public key cryptography for both connection and authentication. Encryption algorithms include Blowfish, DES, and IDEA. IDEA is the default.

Syntax

ssh [-l username] hostname | user@remote-hostname [command]

SSH commands can be used for the following:

1. IDENTIFYING SSH CLIENT VERSION

This is necessary if you want to identify the SSH client that you are currently running and its corresponding version number, which can be identified as shown below.

ssh -V

NOTE: Linux comes with OpenSSH.

2. LOGGING INTO A REMOTE SERVER

The following syntax is used to log into a remote server from a local host.

localhost:[~]> ssh -l username remote-server
username@remote-server password:
remote-server:[~]>

OR alternatively,

localhost:[~]> ssh username@remote-server
username@remote-server password:
remote-server:[~]>

NOTE: If you are logging for the first time, it will prints a message that ‘host key not found’ and you can give ‘yes’ to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. For subsequent logins, you just need to enter the password.

3. LOGGING OUT OF A REMOTE SERVER

To log out of a remote server, simply enter the exit command on the terminal to close the connection as shown in the syntax below.

remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>

4. RUNNING REMOTE COMMANDS FROM LOCAL HOST

You can run the UNIX commands on the remote server from the local host. This is as shown in the syntax below:

localhost:[~]> ssh user@remote-host “ls test”
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

5. DEBUGGING THE SSH CLIENT

This is used when we are not able to connect to the remote host thus it is good to debug and find the exact error messages that ARE causing the issue. Use the -v option for debugging the ssh client.

ssh -v user@remote-host

6. COPYING FILES BETWEEN REMOTE HOST AND LOCAL HOST

The scp command is used to copy the files securely between the local host and remote host using the ssh authentication.

To copy the file from local host to remote hosts /var/tmp/ directory, the below scp command is used.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, the below scp command is used.

scp user@remote-host:/usr/local/bin/add.sh

E) uptime

This command tells you how long your system has been running.

Syntax

uptime [options]

It gives a one line display of the following information.

  • The current time
  • how long the system has been running
  • how many users are currently logged on
  • the system load averages for the past 1, 5, and 15 minutes.

In the above output:

  • the current time (01:49:46)
  • how long the system has been running since the last boot (5 hrs 22 min)
  • how many users are currently logged on (2)
  • load average (0.11, 0.36, 0.38)

This is the same information contained in the header line displayed by the w and top commands.

  1. w command

2. top command

NOTE: The w command displays who is logged on and what they are doing while top command provides a dynamic real-time view of a running Linux/UNIX/BSD operating systems.

F) tar

The tar command (also known as tape archive) is used to create, maintain, modify, and extract files that are archived in the tar format.

Syntax

tar option(s) archive_name file_name(s)

tar files are created by using both the -c and -f options.

-c option instructs tar to create an archive and -f option indicates that the next argument (that is, piece of input data in a command) will be the name of the new archive file.

Thus the following would create an archive file called file.tar from the three files named inputfile1, inputfile2 and inputfile3 that are located in the current directory (the directory in which the user is currently working):

tar -cvf file.tar inputfile1 inputfile2 inputfile3

The -v (verbose) option is used together with the -c and -f options in order to display a list of the files that are included in the archive.

tar can also be used to make archives from the contents of one or more directories. The result is recursive — it includes all objects like directories and files, within each level of directories. For the below example, the contents of two directories named dir1 and dir2 could be archived into a file named dir.tar as shown:

tar -cvf dir.tar dir1 dir2

tar can be used with a wildcard. A wild card is a character which can represent some specific class of characters or sequence of characters. The following example uses the star wildcard (an asterisk), which represents any character or sequence of characters, to create an archive of every object in the current directory:

tar -cf *

By default, tar creates an archive of copies of the original files and/or directories, and the originals are retained. However, they can be removed when using tar by adding the — remove-files option.

It cannot compress and decompress on its own, thus tar is commonly used in combination with an external compression utility. A very handy feature of the GNU version (which is standard on Linux) is the availability of options that will cause standard compression programs to compress a new archive file as soon as it has been created.

They are:

  • -j (for bzip2)
  • -z (for gzip)
  • -Z (for compress).

Thus, for example, the following would create an archive named files.tar.bz2 of the files file4, file5 and file6 that is compressed using bzip2:

tar -cvjf files.tar.bz2 file4 file5 file6

tar can also be used for unpacking tar files, but note that there are several steps to be undertaken.

I) Confirm that sufficient space is available on the hard disk drive (HDD).

II) Move to an empty directory (create one with an appropriate name) to prevent the reconstituted files from cluttering up the current directory and overwriting any files or directories with same names that are in it.

III) A compressed archive must first be decompressed using the appropriate decompression program (which can usually be determined by the filename extension).

The -x (for extract) and -f options are required in order to unpack a tar file. It is also common to add the -v option to provide a running listing of the files being unpacked. Therefore to unpack the archive file.tar created in a previous example the following would be used:

tar -xvf file.tar

Just as options are available to allow three compression programs to automatically compress newly created tar files, the same options can be used to have the compression programs automatically decompress tar files prior to extraction. Thus, for instance, the following would decompress and extract the contents of the compressed archive files.tar.bz2 that was created in an above example:

tar -xjvf files.tar.bz2

You can add files to an existing archive using the -r option. It is also necessary to use the -f option to indicate that the following string (that is, the sequence of characters) is the name of the archive. For example, the following would append a file named file7 to file.tar:

tar -rf file.tar file7

The — delete option allows specified files to be completely removed from a tar file (except when the tar file is on magnetic tape). This is different from an extraction because complete removal of files does not allow for copies of the removed files to be made and placed in the current directory.

For example, the files file1 and file2 can be removed from file.tar with the following:

tar -f file.tar — delete file1 file2

The -t option tells tar to list the contents of an uncompressed archive without performing an extraction. The following would list the contents of file.tar:

tar -tf file.tar

One of the few options that can be used alone with tar is — help, which provides a compact listing of the numerous options that are available. Another is — version, which shows the version number for the installed tar program as well as its copyright information.

G) zip

The zip command is used to package and compress files. The unzip command is used to list, test and extract compressed files in a ZIP archive.

The zip command is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.

Syntax

zip file.zip file1 file2
zip file.zip dir1
zip -option file.zip dir1
  1. COMPRESSING A DIRECTORY

To zip a directory called books in your home directory (/home/mypc/books), type the following command:

zip -r inspirationalbooks.zip /home/mypc/books/

or

zip -r inspirationalbooks /home/mypc/books/

NOTE: The extension .zip is not mandatory and this is useful only to identify the file zip file.

The -r option recurse into directories (all files and directories inside books) to produced zip file called inspirational.zip. You can go through the directory structure recursively starting at the current directory.

For example, all the files matching *.c in the tree starting at the current directory are stored into a zip archive named mydata.zip.

zip -R mydata.zip "*.c"

2. COMPRESSING A FILE

To create a zip file:

zip random.zip a.txt b.txt resume.doc pic1.jpg

3. DECOMPRESSING A ZIP FILE

To decompress a zip file:

unzip name
unzip name.zip

H) fg

the fg command continues a stopped job by running it in the foreground. It moves a background job in the current shell environment into the foreground.

Syntax

fg [%jobID]

Or

fg jobID1 jobID2 … jobIDN

Typing fg will resume the most recently suspended or backgrounded job.

%jobID specifies the job that you want to run in the foreground. It can also be a process ID (PID) number, or you can use one of the following symbol combinations:

  • %Number : Use the job number such as %1 or %2.
  • %String : Use the string whose name begins with suspended command such as%commandNameHere or %ping.
  • %+ OR %% : Refers to the current job.
  • %- : Refers to the previous job.

I) scp

scp command copies files over a secure, encrypted network connection.

scp means ‘secure copy’. It copies files securely between hosts on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh.

The difference it has from rcp is that scp will ask for passwords or passphrases if they are needed for authentication.

Syntax

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

The location of the source file is specified by username1@source_host:directory1/filename1, which includes the:

  • Name of the account on the host computer (username1)
  • Hostname of the computer on which the source file resides (source_host)
  • Name of the directory containing the source file (directory1)
  • Filename of the source file (filename1)

The location to which the source file will be copied is specified byusername2@destination_host:directory2/filename2, which includes the:

  • Name of the account on the destination computer (username2)
  • Hostname of the computer to which the source file will be copied (destination_host)
  • Name of the directory to which the source file will be copied (directory2)
  • Filename of the copy (filename2)

scp can be used in various ways:

  • scp is forced to use protocol 1.
scp -1 
  • scp is forced to use protocol 2.
scp -2
  • Copies between 2 remote hosts are transferred through the local host. Without this option the data is copied directly between the remote hosts.
scp -3
  • scp is forced to use IPv4 addresses only
scp -4
  • scp is forced to use IPv6 addresses only
scp -6
  • Selects in batch mode and avoids asking for passwords.
scp -B

Passes the -C flag to ssh(1) to enable compression.

I hope this article has increased your knowledge of the command line.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.