Everything You Need To Know About WC Linux

The roots of Unix go all the way back to the 1970s and Bell Labs. Originally intended for use at Bell and AT&T, Unix was licensed for use with outside parties starting in the late 70s, and soon it was in use at UC-Berkeley, Sun Microsystems, Microsoft and IBM. Over the years, the rights to Unix changed hands among Novell, Santa Cruz Operation and The Open Group. Today, the single biggest application for Unix is with Apple’s macOS.

Unix was originally intended as a platform for programmers who were developing Unix-compatible software. It began to catch on in academic circles and grew larger as users adapted their own tools into the system. Unix uses plain text for storing data, with a hierarchical file system and the capacity for treating devices and interprocess communication as separate files. By the early 80s, users began to regard Unix as an operating system that could be suitable for computers of all sizes.

Understanding Unix

Unix can be thought of as the link between the user and the computer, in the form of a set of programs. These programs allocate and manage the computer’s resources, with the shell translating commands from the user and converting them into terms that are understood by the “kernel” or OS, which is Unix.

The system also runs on commands and utilities, which initiate everyday tasks for the system and are usually delivered through third-party software. Its information and data are all organized into files, which are further organized into directories. This tree-like design is called the filesystem.

WC for Linux

linux

In Linux and Unix operating systems, the WC (word count) command is what’s used to determine the number of newline count, word count, bytes and characters in a file, as defined by the file arguments. Let’s break down these definitions a little bit, as they pertain to Linux and Unix:

  • Newline: a control character or a series of control characters in a character encoding setup such as ASCII or EBCDIC. Newline marks the end of a line of text and the beginning of a new one.
  • Byte: a sequence of a certain number of bits, which can be used as a unit of memory or storage. An eight-bit byte is used for instructions execution
  • Word : a “word” is considered to be any sequence of characters, followed by a white space.

Counts are always assembled in the following order: newline, word, character, byte, maximum line length. So, these are a few examples:

WC -1: Prints the number of lines in a file

WC – w: Prints the number of words in a file

WC – c: Displays the count of bytes in a given file

WC – m: Prints the count of characters in a file

WC – L: Prints only the length of the longest line in a file

WC In Use

WC logo

Obviously, WC is really handy when there are instances where you need to know characters, line counts, bytes, words or longest lines in a given file. So, let’s take a look at how it’s actually done.

A basic command could be:

$ wc/etc/passwd

65 185 3667/etc/passwd

In a case such as this, 65 is the lines count, 185 the word count and 3667 the characters count. Now, should you just need to know the number of lines in a given file, add the -1 argument:

$ wc -1 etc/passwd

         65/etc/passwd

Determining the number of words in a file, add the -w argument:

$ wc -w etc/passwd

          185 etc/passwd

Using Pipes in Linux WC Commands

Pipelines, in Unix and Linux systems, can be thought of as a series of processes that are linked by standard streams, with the output of one process fed directly as input to the next process. The shell syntax used for pipelines will list a series of commands, each separated by a vertical bar or “pipe,” in Unix/Linux verbiage. Pipes and pipelines are also used in other operating systems, such as Microsoft windows, DOS, OS/2 and BeOs.

WC as a command follows the process of reading input from STDIN and writing output to STD out. Thus, it can be used in Linux command pipelines, for instance:

Here’s a command that shows the number of people who are logged into your Linux system at any given time:

who | wc -1

In this instance, the output of the “who” command is piped into the input of the “wc” command, which, in this case, is being used to tabulate the number of lines in the output of the “who” command.  Along the same lines, this example shows how to use pipelines and WC to indicate the number of processes running on a Linux system at a given time:

ps -3 | wc -1

This is achieved the same way as the previous example. The “ps” command generates output, and the “wc” -1 command indicates the number of lines of output associated with that command. In other words, piping is a useful way to connect the streams and connections between programs and files, directing and redirecting data in accessible ways.

Redirecting From a File

Let’s look at this example:

[email protected]: wc -1 myoutput

8 myoutput

[email protected] 2c -1 ¸myoutput

8

[email protected]:

Many programs allow a user to access a file as a command line argument, and will then read and process the contents of that file. Using WC to supply the file you need as a command line argument, the output from that program displays the name of the file that’s being processed. The subtle difference in this example is that when we want to redirect contents of the file using wc, the file name isn’t printed since in an occasion where redirection or piping are used, data is sent anonymously. In this example, WC refers some content to process, but can’t print this information since there’s no specific knowledge of where it came from. This is a mechanism that can be used to get access to ancillary data, which isn’t printed.

Now, we combine two different forms of redirection into a single command, still using WC:

 

[email protected]: wc -1 < barry.txt > myoutput

[email protected] bash: cat myoutput

7

[email protected]:

Let’s Recap

So we’ve talked about how to use the WC command in Linux and Unix. Just to reiterate:

Wc -1 : prints the number of lines in a file

Wc -w: prints the number of words in a file

Wc -c: displays the count of the bytes in a file

Wc -m: prints the count of characters in a file

Wc – L: prints ONLY the length of the longest line in a file

 

To find more information and get help for the WC command, run “wc -help” or “man wc” from the command line. Remember that more than one file name is called for in argument, the command will show a four-columnar output that displays all individual files. There will be another extra row that displays all files specified in argument, including the total number of characters, lines and words, along with the keyword “total.”

The WC command can also be used to get a total of the number of rows or records in CSV files, when used along with pipes. Here’s an example with five CSV files and the objective of finding the sum of records across all five files. Here, we’ll pipe the output of the “cat” command to WC:

cat *.csv | wc -1

1866

Thus, there are 1866 records counted across all five files.

WC can also be used to get a total of folders and files in a directory, when combined with the ls command. It involves passing the -1 option to ls to count each folder or line, piped to WC to give a count:

ls -1 | wc -1

21

And we see a total of 21 folders and files in that directory.

Learning your way around Unix and Linux is a lot like learning a new language, and the WC command is just another part of the syntax that you’ll need to grasp to do well with Unix.

What is Fuser & Is it Worth My Time? – Everything you Need To Know

Fuser is a command line utility for the UNIX and LINUX computer operating systems. For many system administrators and other users, Fuser is an invaluable utility because of the control it offers to its users, along with its versatility. There are many different reasons to use Fuser and all of them are worth your time, both as a LINUX and UNIX user and as a system administrator.

The History Of LINUX

The History Of LINUX

First of all, LINUX and UNIX should be explained for neophyte computer users. UNIX and LINUX are related and share some command and utilities(such as Fuser). UNIX came first in 1970 and is a complete operating system. It was originally developed by AT&T and was loaned out to other companies, such as Apple for its Macintosh computers. Unlike LINUX, companies that wish to use UNIX in their products must pay a fee, which can be substantial.

Meanwhile, LINUX is an open source kernel that is based on UNIX, but is considered to be separate in most respects. This means no one company owns LINUX, unlike Windows or DOS, for example. Any individual who wants to add to or modify LINUX may do so without paying a fee or being an employee of a corporation. This flexibility allows for innovations that might be impossible on a closed platform. Although LINUX is open-source, certain companies(such as Valve) still support it. Even though these companies don’t own the OS, they can use it like everyone else does. LINUX isn’t a full operating system yet, but it is very close to being a full OS, and can be used as one in a variety of ways.

How is UNIX different from Windows or Mac OS?

How is UNIX different from Windows or Mac OS

So Fuser is a command-line utility for both UNIX and LINUX. That explains a lot, but if you don’t know what a command line is, these words can still be confusing. With a command line operating system such as UNIX and LINUX, all commands for the operating system must be typed into the computer. An excellent example of a command line OS is MS-DOS. MS-DOS requires the user to type every command. To see a list of files, the user must type “dir”. In order to see a sorted list of files, the user must type in something like “dir /p”. The “/p” portion of the command is known as the parameter, and helps to specify the command that you’re typing into the computer. All computers used to work this way before mouse-based interfaces came into fashion.

But wait a second. There are other operating systems like Windows and Mac OS X that don’t require any typing at all, and are mouse-based. Are those related to these command-line operating systems? The simple answer to that question is yes. Windows and Mac OS X actually create a “shell” of sorts that basically translates the mouse movements you are making into commands on the command line! The command line is even visible in certain instances while you work in Windows or Mac OS X. For example, when you open the “Terminal” window in Mac OS X, you can actually see the UNIX command line and work with it! So most computers still run on a command line, even if computer companies spend a lot of time and energy to make it seem as if that’s not the case.

While most users are content working with their mice in an environment set up by computer manufacturers, some users prefer the control of a command line interface. UNIX is one of the most popular operating systems with this type of interface. Now that we understand what UNIX and LINUX are, we can more fully appreciate what Fuser is and the functionality it gives a knowledgeable user.

What is Fuser and How Does it Work?

What is Fuser and How Does it Work?

With Fuser, you can examine a computer file, see what processes(or running programs) are using the computer file in some way, and actually stop active processes from using a particular file! This ability gives users control over what a program is doing, while the program is working. It’s an exceptional ability to have and is well-worth your time to learn.

To use Fuser, first type the word “Fuser .” into the command line and press enter/return. Please note the period—it is important and tells the computer which directory to examine. The computer will return a series of numbers followed by a letter. According to Tecmint.com, the series of numbers is a PID, or process identification number, which is assigned to each process by the computer as the process is running. The letter that follows the PID is a code that tells the user what file is being executed(running) and which directory the file is in. For example, the letter “e” means that you are looking at an executable file that currently is running. Meanwhile, the letter “c” means that the process is happening to a file in the current directory. This information can be a big help in seeing exactly what’s going on in a network at the moment.

That’s great, but what if you need more information, or you don’t have access to a code key? Well, that isn’t a problem. Simply type “Fuser -v” and a verbose version of the information comes on screen. Fuser shows the name of the current directory, columns of the process owner (USER), the access type (ACCESS) and the command (COMMAND) along with the PID(PROCESS ID) again. This will give you a lot more information to track down exactly what’s going on inside your network. Just identifying the process owner alone could come in very handy. If, for instance, a user on your network inadvertently set up a an endless loop without realizing it, you as the administrator could easily use Fuser to identify the process along with the person responsible. Again, this ability allows a system to be much easier to maintain.

If all Fuser could do was to identify running processes and users, it still would be handy. But Fuser can do far more. As stated above, Fuser can actually stop processes from running. In order to kill all processes affecting a directory, for example, you can type “. Sudo fuser -k”, and then type enter/return. Sudo is a UNIX and LINUX program that grants you administrative access to a UNIX system. It originally stood for “superuser do”. We have already gone over “fuser” and the “-k” parameter stands for “kill”. This command will kill all processes occurring in a particular directory.

Suppose you just want to kill one or two processes? When that happens, simply type in “. Sudo fuser -k -i”. The “-i” parameter stands for “interactive”. When the command is received by the computer, it will ask you if you wish to kill each process, which will be referred to by its PID. It really is that easy and is useful in a number of situations. Take, for example, debugging. If you are aware of which directories/files a program is meant to access, and it keeps crashing, you can use Fuser to check and see if the program is accessing files that it shouldn’t be and whether or not that’s causing the crash.

Real World Examples of How Fuser Can Help With A Problem

man stretching while on work

Another example that is encountered often, in reality, is when a user comes across a file that she may want to delete (because it’s bugged, for example), but a number of processes are using that file, possibly hanging and slowing down the entire system. In that instance, with Fuser, you can just kill every process that is accessing that file and then easily delete it. Without Fuser or another way to stop processes, that file would be difficult to get rid of.

Another instance in which Fuser could come in handy is when you are working on TCP/IP sockets. Fuser can monitor all processes using those sockets by simply typing in “. Fuser n socket name”. So, you can see which programs (processes) are using the Internet at any time, simply by typing in an instruction, and you can stop any unusual or excessive internet traffic. Mobile drives, such as USB drives, can also be monitored this way by typing in the “-m” parameter. Not only is this ability useful for monitoring regular internet traffic, it is also useful for monitoring spyware and virus activity, although admittedly these are uncommon on the UNIX/LINUX platform.

To recap, Fuser is a UNIX and LINUX command line utility that allows you to monitor processes affecting files, as well as network traffic. It also allows you to stop those processes at anytime, simply by typing a command. This way, you, as a system administrator, can accomplish many goals more efficiently, including resource allocation.

Even better, according to digitalocean.com, Fuser allows you to monitor TCP/IP traffic as well, and even allows the same command over mobile drives, such as USB thumbsticks. Fuser is also relatively easy to learn and is available to every computer using LINUX and UNIX. In short, Fuser is a powerful command, and definitely is worth your time to learn.