Skip to main content

Posts

Showing posts with the label Linux

Find command: How to find and delete 0 bytes files in Linux

In Linux, "find" command is a powerful command that we can use not only to find files. I've used it for different requirements, so thought of posting some of the convenient usages of find commands in a series of posts. For some application issue, I've got one of my directories in Ubuntu system swarmed with empty files with 0 bytes. Some of the files got data in them but some of them were empty. I just wanted to delete the empty files and it wasn't possible to identify them by file names or dates. So find command comes to rescue. find . -size 0 -type f  If I needed to isolate the files by file extension, we can use something like this. find . -size 0 -type f -name "*.tar" You can get the file count by piping it to wc command. find . -size 0 -type f -name "*.tar" | wc -l The awesome thing about the find command is you can use it for deleting files just by adding the -delete flag to the command. find . -size 0 -type f -name "*.tar" -dele...

First few commands to run on an unknown Linux server

 If you get to troubleshoot some unknown Linux server, running these few commands will be useful. 1. Check who's logged in w This is a cool one-letter command to see who else logged into the server. It will list the usernames and even you can find the IP and the connection type. In the following example, I've logged in as the "user". If you see, some other users, watch out for unauthorized access. 2. Identify the OS There are different commands to identify what kind of Linux distribution that you are dealing with. Knowing this is crucial to decide what commands to use later. cat /etc/os-release Usually, in Linux distributions, you have a file /etc/os-release . Basically, you can see everything you need to know about the OS in this file.  3. See running processes top This basically lists the processes running with the CPU and memory consumption. With this, we can get an idea about what kind of apps running on the server and if any of them uses too much CPU or memory. 4...

Linux deleted log file doesn't free up the disk space

TL DR; Kill or restart the process which writes the log file. Even though the file is deleted, it won't free up the disk space if the process is still writing to the log file. Only after stopping the process, it will actually be deleted. ---------------------------------------------------------------------------------------- Usually, disk spaces of servers are getting cluttered and filled by application log files when a proper log rotation mechanism is not implemented.  In case if you find such a log file causing the disk space to fill up 100%, it will probably impact any application which trying to use the disk.  You can see the disk space by the following command. df -h If the root dir ("/") usage is 100% the impact may be high for the system. Or else, if there is more than one disk attached then any application that uses that disk can be impacted.  In the above image, mine is not filled yet. I have only used 25% of the disk. How to find what files are using the disk sp...

How to format huge Json files

Recently I had to deal with some huge JSON backup files and needed to do a simple text search. But the JSON file is compact and so huge like 5Mb. None of my text editors could handle that size of a compact JSON file. The thing is when the JSON is in a compact, it's just one line. So when I open it up with a text editor or an IDE, it just crashes. It can't handle that much of a lengthy line. I could run a grep command in the command line but since it is just a single line, its not really useful to know the line number. The trick is to format the JSON file with tabbed indentation, but online Json formatters weren't good enough for this task. same as the text editor it just crashes. Or they don't really allow you to format huge JSON files. The answer The answer is Python! Python comes really handy at this work. Python comes with built-in JSON manipulation libraries, and you can run a python function in the command line even if you don't know how to code...

How to enable Linux wifi hotspot for android devices

This is some trouble I faced. When I create a wifi hotspot from my laptop, I couldn't connect my phone to it. Normally it allows when you create an open wifi hotspot, but with a password protected one you can't. This is because Ubuntu based OS's create ad hoc wifi networks, and they are not compatible with connecting to other devices. We can fix this with a simple trick. 1. Create a new wifi connection This is nothing big, most of the Ubuntu based OS's have a GUI to do this via a network icon the top etc. Remember create it as WPA & WPA2 Personal network. This is the type of the network that we're gonna create, and it allows you to set a password for that. 2. Make it accessible to other devices After you creating the wifi network it will create a file in the  /etc/NetworkManager/system-connections/ directory I created mine as mynetwork  so here is the file I got. You might need sudo permission to access that. The file is there, so I just wan...