Simplify your daily tasks with Linux command line tools
Linux provides many command line tools to simplify your daily tasks. One such tool is the wc command.
wc is your go-to command when you need to know the number of words in a file or how many files there are in a specific directory. But that's not all the wc command can do. Read on to learn what the wc command is and how to use it effectively on Linux.
The wc command prints out the number of lines, words, characters, or bytes in a file or output. Here's how to use it to your advantage.
What is the wc command?
The wc command is the abbreviation of word count. It is a command line tool that counts the number of words, lines, characters and bytes in the output. It comes pre-installed on every Unix and Linux-based operating system, so you don't need to install it manually.
wc command syntax
To use wc, you need to specify a file or text output and the command options to use. The basic syntax of the wc command is:
wc [OPTION] [FILE]
There are many options available for use with the command, all of which we will discuss later. For command line help on the wc command, check its man page by running the following command:
man wc
“
How to use wc command
For this example, create a file: linuxmi.txt. In this file, paste the following text:
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it.[a] Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now.[b] If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea – let's do more of those!
“
This is the Zen of Python, a set of 19 guiding principles written by Tim Peters for writing simple, elegant, and concise Python code.
If you use the cat command to create the file, leave a blank line before pasting the text.
Use default wc command
By default, when you use the wc command on a file or output, it prints out the number of lines, words, and bytes present in the output.
Try using linuxmi.txt by executing the following command in the terminal:
linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt
result:
19 137 830 linuxmi.txt

You will see that it outputs four columns containing the number of lines, the number of words, the number of bytes, and the file name.
Print the number of lines present in the file
To count the number of lines present in a file or output, use the -l or –lines option. The syntax is as follows:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -l linuxmi.txt
result:
19 linuxmi.txt

It shows that there are 19 lines in the file and also prints out the name of the text file.
Print the number of words in the file
To count the number of words in a file, use the -w or –words option. Try it:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -w linuxmi.txt
result:
137 linuxmi.txt

Display number of bytes
You can use the wc command with the -c or -****-bytes option to determine the exact number of bytes in the file. Execute the following command to try it out:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -c linuxmi.txt
result:
830 linuxmi.txt

打印文件中的字符数
要打印出文件中的字符数,请使用 -m 或 –chars 选项。语法如下所示:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -m linuxmi.txt
结果:
824 linuxmi.txt

打印最长行的长度
如果需要知道文件中最长行的长度(该行中的字符数),请将 -L 或 –max 行长度选项与 wc 命令配合使用。它看起来像这样:
linuxmi@linuxmi:~/www.linuxmi.com$ wc -l linuxmi.txt
结果:
70 linuxmi.txt

对多个文件使用 wc 命令
您可以将 wc 命令用于多个文件或输入。为此,您需要再创建两个文件。第一个文件是 zimu**.txt,其中包含字母表的列表,而第二个文件是shuzi.txt**,包含从 1 到 10 的数字列表。
或者,您可以使用任意两个文本文件。让我们来试试吧:
linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt zimu.txt shuzi.txt
结果如下图:

前三行包含每个文件的行数、字数和字节数,最后一行包含每列的总和。
将 wc 命令与其他 Linux 命令一起使用
您可以通过管道命令将 wc 与其他命令一起使用。管道符号将一个命令的输出作为输入重定向到另一个命令。
计算目录中的文件或文件夹数
为此,您可以使用 ls 命令列出目录中的文件数,然后将输入通过管道传输到 wc 命令中。例如,要打印某一目录上的文件数,请执行以下命令:
linuxmi@linuxmi:~/www.linuxmi.com$ ls www.linuxmi.com | wc -l

计算系统上正在运行的进程数
进程是您的计算机正在处理或当前正在运行的任务或程序。执行命令或打开应用程序时,该应用程序将注册为进程。
要计算进程数,请使用带有 wc 的 ps 命令。在这里,尝试一下:
linuxmi@linuxmi:~/www.linuxmi.com$ ps | wc -l

使用 wc 尝试其他 Linux 命令
Linux 上有很多可用的命令,它们具有非常独特的功能,并使整体 Linux 体验无缝衔接。您只需要知道它们是什么以及如何使用它们!现在就开始你的 Linux 命令之旅吧!
The above is the detailed content of Simplify your daily tasks with Linux command line tools. For more information, please follow other related articles on the PHP Chinese website!

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.