files | LinuxHostSupport Linux Tutorials and Guides Sat, 09 Jul 2022 06:15:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 How to Search Files on the Linux Terminal https://linuxhostsupport.com/blog/how-to-search-files-on-the-linux-terminal/ https://linuxhostsupport.com/blog/how-to-search-files-on-the-linux-terminal/#comments Wed, 15 Dec 2021 18:30:00 +0000 https://linuxhostsupport.com/blog/?p=1606 Finding files and directories in Linux is a very difficult task compared to Windows operating system. Especially if you are working on the server operating system without a Desktop interface. There are several ways to search files and directories in Linux. The simple and easiest way is to use the Linux terminal to search or […]

The post How to Search Files on the Linux Terminal appeared first on LinuxHostSupport.

]]>
Finding files and directories in Linux is a very difficult task compared to Windows operating system. Especially if you are working on the server operating system without a Desktop interface. There are several ways to search files and directories in Linux. The simple and easiest way is to use the Linux terminal to search or locate files.

The find and locate are the most popular command-line tool used to search files and directories in Linux. The find command allows you to search for files and directories based on an expression. This way you can search files and directories based on their size, date, type, and ownership.

In this post, we will show you how to search files on the Linux terminal using the find and locate commands.

Prerequisites

  • A Linux VPS with root access enabled, or a user with sudo privileges.

Log in and Update Packages

First, we’re going to need to log into our server using SSH. You can do that by entering this command:

ssh root@IP_Address -p Port_Number

Remember to replace root with your username if you are not using the root user. Change IP_Address and Port_Number according to your server’s IP address and SSH port number.

Once you are logged in, you should update all of your packages to their latest available versions.

apt-get update -y
apt-get upgrade -y

Once all the packages are up-to-date, restart your server to apply the configuration changes.

Search Files by Name

The find command allows you to search a specific file by its name. You can use the find command with -name option followed by the file name that you want to search.

For example, to search a file named file1.txt in the /etc directory, run the following command:

find /etc -type f -name file1.txt

If you want to ignore the case during the file search, use the -i option as shown below:

find /etc -type f -iname file1.txt

You can use the following option if you want to search for a specific file type:

  • f – regular file
  • d – directory
  • l – symbolic link
  • c – character devices
  • b – block devices

Search Files by Extension

You can use the asterisk * symbol before any extension to find all files that end with specific extensions including, .txt, .png, .pdf, .mp4, etc.

For example, to search all files with extensions .png in the /opt directory, run the following command:

find /opt -type f -name '*.png'

This command will search all PNG files located inside /opt directory.

Search Files by Size

You can search a file based on their size using the -size option followed by size criteria.

For example, to search all files less than 2 MB inside /home directory, run the following command:

find /home -type f -size -2M

You can use the + symbol before the size if you want to search files greater than 2 MB:

find /home -type f +size -2M

You can also specify the size range during the file search.

For example, to search all files between 2 MB and 10 MB, run the following command:

find /home -type f -size +2M -size 10M

Use the following options to specify the size in KB, MB, GB, and more.

  • c – bytes
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes
  • b – 512-byte

Search Files by Modification Time

The find command can search a file based on its modification, access, and change time.

For example, to search PHP files inside /home directory that have been modified in the last 10 days, run the following command:

find /home -name "*.php" -mtime 10

You can use the + symbol if you want to find all files that have been modified more than 10 days.

find /home -name "*.php" -mtime +10

Search Files by Permissions

You can use the find command with the -perm option to search a file based on their permissions.

For example, to search all files inside the /etc directory with permissions of exactly 764, run the following command:

find /etc -perm 764

Search Files Using the Locate Command

The locate is another command-line tool that allows you to search for a file in Linux. The locate command is much faster than other tools because it uses a database file to perform the search instead of searching your local hard disks.

For example, to search a file named rc.local, run the following command:

locate rc.local

If you want to count the number of search keywords is matched, use the -c option.

locate -c rc.local

Conclusion

In the above post, we explained how to search for a file using the find and locate command in Linux. I hope you have now enough understanding of how to search a file in your Linux system using the terminal.

PS. If you liked this post, on how to search files on the Linux terminal, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

The post How to Search Files on the Linux Terminal appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-search-files-on-the-linux-terminal/feed/ 6
How to Efficiently Manage Large Text Files in Linux https://linuxhostsupport.com/blog/how-to-efficiently-manage-large-text-files-in-linux/ https://linuxhostsupport.com/blog/how-to-efficiently-manage-large-text-files-in-linux/#respond Wed, 13 Mar 2019 16:06:19 +0000 https://linuxhostsupport.com/blog/?p=831   Linux is an operating system of text files. Unlike Windows, the Linux philosophy and core concept is that “everything is a file”. Sure, there are databases and binary structures, but nothing like Windows’ “Registry” exists. Even devices, partitions, and sockets are represented either by real or virtual files. Given all of this, some text […]

The post How to Efficiently Manage Large Text Files in Linux appeared first on LinuxHostSupport.

]]>
 

Linux is an operating system of text files. Unlike Windows, the Linux philosophy and core concept is that “everything is a file”. Sure, there are databases and binary structures, but nothing like Windows’ “Registry” exists. Even devices, partitions, and sockets are represented either by real or virtual files.

Given all of this, some text files can get pretty big. And often we’re not talking about dozens of MB, but possibly hundreds, or even a few gigabytes in size in rare occasions. And all of it can be text! In fact, something as innocuous as a log file can continue to grow if left unchecked. Let’s say you have a file recording every visit to your website, along with the date, IP, user-agent, etc. For even a medium-sized website, that file can grow pretty large if not dealt with.

In this article, we’ll look at how to view these large files without slowing down your system. Sometimes you might even need to edit these large files, which is far more difficult to deal with when the files are of this magnitude. Luckily, it’s quite unheard of for a configuration file to be large enough to make it problematic to edit! Let’s begin.

Listing the Largest Files on your Linux System

Let’s say you need to clean out some space on your hard drive and want a list of the biggest culprits eating up your disk space. To get a list of the top 10 largest files, use this command:

find / -type f -exec du -sh {} 2>/dev/null + | sort -rh | head -n 10

This will give you an output like this. Note that it may take a while for the results to appear onscreen:

Large Text Files
List of Large Text Files

As you can see, most of these are binary files or databases that you can’t open directly. But often you’ll find mail logs that keep a record of even spam attempts to access your server. And if your IP address is hit often, that mail spool will just keep getting bigger and bigger, reaching hundreds of MB in a matter of a few months.

Trying to Open Large Files the Traditional Way

For this example, we’re going to create an example text file and bloat its size using the “truncate” command like this:

truncate --size=20M testfile

Where “testfile” is the sample file. This command will set its size to 20MB and fill the extra space with null characters. We can get the size of all the files in the current directory in MB using this command:

ls -l --block-size=M

Here’s the output:

Creating 20MB Test File
Creating a 20MB Test File in the Directory

We will use the traditional text editor called “vi” to open the file and close it as soon as it’s loaded. To know how long the entire operation lasted, we use the “time” command, like this:

time vi testfile

And when we exit vi after “testfile” has displayed its contents, it gives us the duration of the entire operation like this:

Opening and Closing File Using vi
Opening and Closing the File Using vi

So in this case, it takes us almost 6 seconds for a file that’s just 20 MB! That’s an absurd amount of time for a powerful processor. What happens when we increase the size to 40 MB?

Execution Time Increases Non-Linearly
The Execution Time Increases Non-Linearly

It now takes us 23 seconds! So the time increases non-linearly. When the file is larger, it takes exponentially longer to open.

The “cat” command is even worse:

Cat Command Has Similar Performance
The Cat Command Offers Similar Performance

15 seconds just to open a 10MB file!

Using “Less” is More Efficient

Luckily for us, we have a command called “less” which reads the contents of the file one block at a time, and thus spares us the problem of loading it all at once. The syntax is simple:

less [filename]

Where [filename] is the name of the large file you want open. You can see in the screenshot below that it opens a huge file in less than a second:

Less Command Opens Files Instantly
The Less Command Opens Files Almost Instantaneously

Most of that time is used by having to manually quit the program. So it’s pretty much instantaneous! We can navigate to the next “block” in the file by pressing “f” and we can go back by pressing “b”. We can also use “j” and “k”, or the arrow keys, to navigate between lines.

So the next time you see a file of hundreds of MB and need to view it, use “less” instead of “vi” or “cat”. Your system resources will thank you for it, and you’ll save time as well!


Of course, you don’t have to do any of this if you use one of our Outsourced Server Support Services, in which case you can simply ask our expert Linux admins to manage your large text files for you. Just sit back, relax, and let our admins take care of the issue for you. They are available 24×7 to help you with your requests.

PS. If you liked this post on managing your text files in Linux, please share it with your friends on the social networks by using the share shortcut buttons, or simply leave a comment in the comments section below. Thanks.

The post How to Efficiently Manage Large Text Files in Linux appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-efficiently-manage-large-text-files-in-linux/feed/ 0