Introduction
In the previous blog, we compared two of the most popular operating systems, Linux and Windows which are widely used operating systems globally. In this blog, we will look at how Windows and Linux handle text processing and file management so that we can use the relevant tools correctly during our daily jobs. Specifically, when it comes to this use case, Windows and Linux have commands and utilities in common, but Windows’ commands are more verbose than Linux’s.
Text Processing Tools
Linux
In this part, we will be discussing a few important commands that we often use for displaying, searching, and doing necessary tasks when we need to process texts.
The most used commands include cat, grep, head, tail, less, and cut. To run this command, we need to run the terminal from the Linux machine or via SSH using Putty or any other tool.
- cat: Used to concatenate and display file content.
$ cat test
$ cat passwd
- echo: Used displays a line of text.
$ echo hello > test # Write 'hello' to test file
$ echo hi > test # Replace content with 'hi'
$ echo hello world ! >> test # Append to test file
- grep: Used to search for keywords within files.
$ grep -n root passwd # Find lines containing 'root'
- head and tail: Used to display the beginning and end of files.
$ head passwd # First 10 lines
$ tail passwd # Last 10 lines
$ tail -n 5 passwd # Last 5 lines
$ head -n 5 passwd # First 5 lines
- less and more: Used to paginate through files.
$ less passwd
$ more passwd
- cut: Used to extract sections from each line of files.
$ cut -d ":" -f 1 passwd # Extract first field
Windows
There is a set of commands in Windows PowerShell that we can use for text processing. Though Windows is usually popular for its graphical interface that is user-friendly rather than working in the PowerShell or the command prompts. But we will discuss a few similar commands like Linux that we can use for Windows as well.
- type: Used to display the content of a file.
Using => Command Prompt
C:\> type test.txt
- echo: Similar to Linux, but there are a few differences in syntax.
Using => Command Prompt
C:\> echo hello > test.txt
C:\> echo hi > test.txt
C:\> echo hello world ! >> test.txt
- find and findstr: Used to search for strings in files.
Using => Command Prompt
C:\> find "root" passwd.txt
C:\> findstr "root" passwd.txt
- head and tail: Not natively available but can be replicated using PowerShell.
Using => PowerShell
PS C:\> Get-Content -Path passwd.txt -TotalCount 10 # Equivalent to head
PS C:\> Get-Content -Path passwd.txt | Select-Object -Last 10
# Equivalent to tail
- more: Used to paginate through files.
Using => Command Prompt
C:\> more passwd.txt
- cut: No direct equivalent but can be achieved using PowerShell.
Using => PowerShell
PS C:\> Get-Content -Path passwd.txt | ForEach-Object { $_.Split(':')[0] }
File Management
Linux
The file management process of Linux is quite impressive. There are a huge number of commands that we can use to manage files. These include cp, mv, rm, and find.
- cp: Used to copy files and directories.
$ cp mycv myresume # Copy file
$ cp -r /etc . # Copy directory with contents
- mv: Used to move or rename files and directories.
$ mv myresume biodata # Rename file
- rm and rmdir: Used to remove files and directories.
$ rm mycv # Remove file
$ rmdir linux1 # Remove empty directory
$ rm -r linux3 # Remove directory with contents
- find: Used to search for files and directories.
$ find / -name root # Find files named 'root'
$ find / -size +10M # Find files larger than 10MB
Windows
Using PowerShell and Command Prompt this is quite easy to manage but sometimes it needs to run commands for doing any high-level work in Windows Core Servers. Windows also has a variety of file management commands, though many users prefer using graphical interfaces.
- copy and xcopy: Used to copy files and directories.
C:\> copy mycv.txt myresume.txt
C:\> xcopy /E /I C:\Source C:\Destination
- move: Used to move or rename files and directories.
C:\> move myresume.txt biodata.txt
- del and rmdir: Used to delete files and directories.
C:\> del mycv.txt
C:\> rmdir /S /Q linux1
- dir: Used to list files and directories.
C:\> dir
Search and Locate
Linux
Searching files is very necessary both in Linux and Windows. There are a few important commands in Linux to locate files, including locate and find.
- locate: Used to find files by name, using a database.
$ locate hosts
$ updatedb # Update database
- find: Used to search for files and directories based on various criteria.
$ find / -name root
$ find / -type f -name passwd
Windows
Using the command dir, it is possible to search files and directories. By using PowerShell, there you will be able to get more capabilities of searching using specific names or types.
- File Explorer: Used to provide a graphical search interface.
- PowerShell: Used for advanced search capabilities.
PS C:\> Get-ChildItem -Path C:\ -Filter "root*"
PS C:\> Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue
| Where-Object { $_.Length -gt 10MB }
Conclusion
Despite having a significant difference in capabilities and effectiveness, both Linux and Windows have a huge number of powerful commands for text processing. For IT professionals Linux commands are more powerful than the Windows operating system due to its optimization speed. Developers or security engineers who are used to doing complex tasks can perform them very efficiently without any complicity. As an IT engineer, you cannot leave any of these two operating systems, though for complex troubleshooting works you might choose Linux, but these necessary commands will help you to enrich your technical skills. In the next blog, we’ll be discussing Windows and Linux-based text processing editors.