Skip to main content

Posts

Linux vim/vi switch from 1st line to the last line

In Linux vim or vi is a must tool to learn on the command line. Editing .conf or configuration file, checking .log files or editing scripts and other tasks that requires editing will need to a vi or vim to read and edit. However, nano text editor is another version that is quite straight forward to use as well for text editing.  It is still good to have a vi/vim on a skill set of sysadmins, since vim or vi was already in Linux or Unix system before nano was introduced. Editing, checking or viewing a large file requires navigation. Of course, arrow keys are their tom move forward, upward or whatever direction is needed. Viewing 1000 or more lines and using arrow keys to move from 1st line to the last line will take sometimes if only arrow up and arrow down will be used. In vi / vim to quickly navigate from 1st line to last line no matter how big the file is can easily be done using some commands. Open a text file with number of lines using vim. Using :1, or :$ is the command to swit...

How to move files in Linux using ls?

Moving files to organize or to simply archive is part of a sysadmin life. In Linux, ls -1 (number one not small letter L) command means list the files but only show the filenames. The output of ls -1 can be utilized as an input to mv to move the files, since mv requires a filename to be move plus a destination where to move the files. Here's a simple command to do it with a few parameters of course:          ls -1 | xargs -d '\n' -I {} mv -- {} /path/to/destination/ Above move whatever files it can find on the path; where the command is run and it's a command that will also move files even though it has space or tab on the filename of the files. Below is a revised command to move all .txt files on the current directory or on the path where the command is executed.          ls -1 *.txt | xargs -d '\n' -I {} mv -- {} ./archive_folder/ The mv --, the -- is a safety precaution or a mechanism in order not to misinterpret any filenames wi...

Linux Top check a single process

Monitor a single process in Linux, to check if the process is the culprit,.or just simply monitor the resources consume by the process in real time. This can be done easily using Linux, top and pgrep commands Example: Check MySQL daemon. Open terminal and type this:    top -p $(pgrep -f mysqld )  It will display top with only mysqld displayed on the screen. That's it you can monitor the process, observe what it's doing. Till next time, enjoy exploring the command line, Happy Linuxing... Prayer is free, brings comfort to your life. Trust Jesus He will guide you to righteousness. https://soulrefresh.netlify.app

Linux quickly check MAC or IP

How to view or check MAC address or IP address in Linux VM or Server? On the the Terminal or CLI, to view MAC address: Type: ip -4 neigh show  This will show all IPV4 addresses plus the MAC address linked to the IP and will also show the status whether it's Reachable or Stale. Or just simple type: ip neigh The legacy command is: arp -an To check for IP via CLi, one way is to type: ip r It will show the IP and whether there is an assigned gateway. To view all IP addresses assigned to an interface is to type: ip addr show Checking MAC addresses is quite important especially if there's some network flapping issues, since MAC address is at Layer 2 on the OSI model, if it's not working properly then all other layers will be affected. Of course, IP is also important or else it can't connect to the network, or reach the internet That's it till next time. Keep learning. Always Pray no matter what circumstances is happening in your life, being connected to Jesus all the time...

View files in Linux from old to latest aka chronological order

Viewing files in Linux terminal from old to latest, is one of the common tasks a Linux sysadmin will do. And this can be done simply with the good ls command. Plus add a few parameters to make the output more readable and friendly. ls -lhtra this command will view all the files, hidden or not hidden from old to latest Example image below, shows all files; arrange in chronological order. ls -1  the command is ls - plus the number one not the small letter "L"; this is quite useful if need to get the filenames only on a specific directory and feed to xargs command or as an input on any bash script for further processing. Example, typing ls - 1 will output only the filenames on a specific directory. That's it till next time. Enjoy Linux and keep learning. Keep praying and stay open to God's messages. Fiat Voluntas Tua | Catholic Prayers & Quotes

Viewing files with line numbers in Linux terminal

 Reading files is basic and a must when working on the terminal in Linux or command line. There are a lot of ways to do this, a simple task but quite a few ways to accomplish it. Nano, vi/vim the most common editor in Linux that is part of the package in most Linux flavors if not all. In nano editor, while the file is open pressing "alt + n", will display the line numbering on the left side of the screen. Example image: In vi or vim, opening the file and typing the command: "set nu" or "set number" will also view the lines on the left side of the screen. Example image:   Or from the command line itself using "cat -n any_file_name.txt" will also view line numbers the -n is a parameter on cat to view or include the line numbers when displaying the file. Example image: less -N, command will also produce same result. Or just use nl , "man nl" shows:  NAME        nl - number lines of files It comes pre-installed in almost any Linux flavors,...

Display GUI Boxes for Linux bash script

 Displaying GUI Boxes in Linux Bash scripts can be done easily using whiptail. In Windows OS, msgbox, inptubox is quite helpful on VBA, Visual Basic and even in .NET world. Displaying Graphical boxes in Linux bash scripting is quite simple to do. To navigate on this GUI boxes, used "Tabs","Arrow Keys" and "Space Bar" as enter key. Here's a simple example of Whiptail that display GUI Boxes and ping the sites that are selected. #!/bin/bash # Step 1: Checkbox URL menu CHOICES=$(whiptail --title "Checkbox Ping URLs Example" \   --checklist "Select one or more options:" 15 50 5 \   "ping -c 2 bing.com;" "Ping bing" OFF \   "ping -c 2 duckduckgo.com;" "Ping ddg" OFF \   "ping -c 2 search.com;" "Ping search" OFF \   "ping -c 2 google.com" "Ping google" OFF \   3>&1 1>&2 2>&3) # Exit gracefully if nothing was selected or user pressed Cancel ...

Linux find empty directories and files

 How to find or identify empty directories or files in Linux? It can be done easily with find command itself. Here's the syntax and how to test. mkdir test_empty cd test_empty create some dummy directories: mkdir {dummydir1,dummydir2,dummydir3} then create some empty files: touch {file1empty,file2empty,file3empty} To find empty directories: find . -type d -empty To find empty files: find . -type f -empty All directories and files will be shown since they are empty. Add some text to the file: echo "Test" > file1empty Run again the command: find . -type f -empty file1empty will not be on the list since the file has some text. Move the file1empty to dummydir1. mv file1empty dummydir1 Run the command to find empty directories: find . -type d -empty dummydir1 will not be displayed since file1empty is inside of it. How to remove or delete empty directories or files? In production or any environment, make sure you know what you are doing. In a testing environment, it can be d...

Hide a file in Windows and set to read only

How to hide a file in Windows? How to set a read only file in Windows? Or set a read only file in Windows and hide it. Setting a file to read only or hiding a file in Windows, or doing both at the same time, can be done using Command Prompt or using PowerShell. Example commands: Hiding a file in Windows using PowerShell: (Get-Item "C:\file_example.txt").Attributes = 'Hidden' Setting a read-only file: (Get-Item "C:\file_example.txt").Attributes = 'ReadOnly' Set both read-only and hidden attributes to a file. (Get-Item "C:\file_example.txt").Attributes = 'Hidden, ReadOnly' How to reset the attributes or remove the attributes that was set? (Get-Item "C:\file_example.txt").Attributes = 'Normal' Hiding a file using command Prompt: c:\attrib +h "file_example.txt" Setting a read-only file: c:\attrib +h "file_example.txt" Set both read-only and hidden attributes to a file. c:\attrib +h +r "file_exa...

Bash script safely remove or delete files

Automation is a buzzword in today's technology; it makes things faster, effortless, and most of all ease the burden of system administrators. However, automation should be tested thoroughly to avoid any unforeseen circumstances or else instead of making life easier it might deliver some nightmares. For example, removing log files to save space or remove any unwanted or old log files to make room for new files; this task can be done manually or using some automation via a script. In Linux, cron jobs that fires automatically as per schedule is quite ideal to automate removing or deleting log files. This can also be done manually, by simply logging in to the server and delete files. There is nothing wrong in deleting manually but would not be ideal if the operation of the server is operating day and night.  There might be a chance that the operation will halt if there is no more room to write the log files. Or simply when there is some issue and wanted to troubleshoot; if log files ar...

Linux find up interfaces

If a Linux box VM, WSL or a physical server is unable to connect to Network, one possible issue is the network interface is not UP. Typing:   ip address show   Or:   ip a s  The output will show which interfaces are UP. If the output shows like:  eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 UP after multicast means that the interface is not disabled on the OS or administratively enabled LOWER_UP means that a cable is connected  and is able and is able to find an active switch port  Running this command below: tcpdump -D The output will also show that the interface is Up, Running and connected. Example: 1. eth0 [Up, Running, Connected] Basic commands that will help to save the day if ever there is some network or connectivity issues. zenity, dialog, can also be used to generate GUI boxes. However, whiptail still the best for minimal footprint and might come as a default for Server Distros OS. That's...

HAProxy cannot bind port not listening

HAProxy is an open-software used as a high-performance load balancer and reverse proxy for TCP. To check whether the proxy config before reloading or restarting the HAProxy service can be done via this command:  haproxy -c -f /etc/haproxy/haproxy.cfg It will show 'configuration file is valid' if everything is okay or the config file syntax is valid. However, the command cannot check whether it can bind the port, or bind the socket. Example, typing: systemctl reload haproxy to reload the new config file. And to check whether the reload is successful or not status of the service can be checked. Typing, systemctl status haproxy to check the status and if it shows Reload: failed then binding issues or other errors causes reloading to fail. Or some alert is shown as Binding like /etc/haproxy/haproxy.cfg cannot bind socket or cannot assign requested address for 192.168.x.x. IP Technically, the error is already giving some hints that the IP on the HAProxy config file cannot be used fo...

Practice Linux vim in Windows using gVim

Vim is a cool and powerful editor in Linux system. It is also available for Windows via this download page: https://www.vim.org/download.php Choose: gvim_9.2.0000_x64.exe (64bit installer) or any format of your choice. After downloading and installing, the installer it will create two desktop icons; something similar to the image shown below. Why there are two icons? As the name suggests "gVim Easy 9.2", gVim Easy is the filename suggests it's "easy", upon opening or clicking the icon it will open gVim in "insert" mode and start typing immediately. While the other icon "gVim Read only 9.2", upon clicking or opening this icon. This will open gVim on "read only" as it names suggest. It's quite good if you only need to review or read the file. However, it says "read only" it doesn't mean that you cannot edit, modify or add text to the file. While inside "gVim Read only", press "insert" key or pre...

PowerShell/Bash Linux get MAC address

Example of PowerShell get MAC address: getmac /s 127.0.0.1 | findstr /I TCPIP Sample output: C4-75-CB-37-BB-BF \Device\Tcpip_{7F29EDBA-C045-4C74-834E-F026BCE8169B} For Linux: ip a | grep -i "link/ether" | awk '{print $1, $2}' Sample output: link/ether 00:15:5d:98:16:36 That's it a quick way, to find MAC address. Ideal for network switch troubleshooting. Pray, hope and don't worry. Always Be Still, God is always in control. Consult God by praying.

Newly built Linux box cannot SSH

Fully functional and brand new built VM, Physical server, Container or any boxes with Linux distro, however, SSH is not working. Newly built Linux boxes with Server OS, the SSH service or remote access to the box won't start functioning even though the server is up and running. For the SSH to start it needs to have the required SSH keys for the service to start. The solution is to run this command: ssh-keygen -A After typing the command, try systemctl start ssh or systemctl start sahd Also check the status by: systemctl status ssh / systemctl status sshd And also good idea to start SSH once the system is restarted. Type: systemctl enable ssh / systemctl enable sshd Once the service or status of the SSH is confirmed running, then the system or device can connect remotely or other devices can connect to the local system as well. That's it, till next time. Enjoy exploring the Linux World! Prayer connect us to God, line is never congested! Have Faith and Trust Jesus, He always do g...

Linux check internet connection

Setting up any system, laptop, desktop, server, virtual machines or containers one of the requirements is for the device or machine to be able to connect to other systems or other networks like the internet. And having a working gateway, as it.is called in networking is the key that the machine is able to connect to ther internet or other devices. In Linux, a simple command type on the terminal like: - ip r  - or type the whole command: ip route This will show a message something like: default via 192.168.1.1 dev eth0 proto kernel 192.168.1.1 is the gateway IP address and if.everything is in place correctly then the system can connect to internet. Other way to verify via command line is to type: $ ip route get 8.8.8.8 Which will show an output like: 8.8.8.8 via 192.168.1.1 dev eth0 sec 192.168.1.24 uid 1000    cache Shows that packet destined for 8.8.8.8 will be routed via the gateway 192.168.1.1 Source IP will be 192.168.1.24 and UID 1000 is the id of the user executing ...

Linux variables to get UID/GID

User ID(UID), GID (Group ID) in Linux are numerical identifiers to set access to files, directories and system resources, just like rwx which in octal equivalent is r=4, w=2, x=1 and the desperate permission which will equal to 777.  777 is quite useful when nothing is working and an easy way out but yes use sparingly. UID and GID can be used also to restrict permissions or access. Quite a few ways to get UID value for the current login user. At terminal typing: echo "$UID" id -u echo/run/user/$UID All commands above are the same, get the user id of the currently logged in user. To get the GID, or the group membership of the currently logged in user is quite straight forward, at terminal type: id The output will be the  group-id(name of the group) and those are the group membership of the currently logged in user. id -Gn will just list the groups without the GID. id -G will just list the GIDs without name group Another quite useful variable, if let's say someone is asking...

Move selected files in Linux

How to move selected files in Linux via a one liner.command? Moving files is quite straight forward with mv command, if need to move a hundred or more files is another story. However, a group of commands like piping the output of the command to another command will make life easier. First, do an ls or list the files before moving the files to make sure those are the files that needs to be moved. ls | egrep -iv "*2025-11*" | egrep -iv "*2025-11-template*" After checking or listing the files then do the actual move. Here's an example: ls | egrep -iv "*2025-11*" | egrep -iv "*2025-11-template*" | xargs -I {} mv {} /my backup/logfiles The -iv parameters tells egrep to filter case insensitively and only shows files that doesn't match the pattern. That's it make life easier, work smart not hard. Let the technology work for you. Trust in God, Pray and don't worry. God is in Control, Be still.

vim or vi searching for whole words or string

Search for a whole word or string in VI or VIM, can be done easily and a time savee so no need to keep pressing next or 'n' for the next match. How to search for a whole word or string in VI or VIM? Example open a file with a list of IP addresses. vim list-of-local-ips.txt Example contents: 192.168.13.130 192.168.13.131 192.168.13.1 192.168.13.135 192.168.13.137 192.168.13.23 192.168.13.13 192.168.13.100 If vi or vim is insert mode, press ESC until it's in command mode, If need to search for 192.168.13.1, or just 13.1 In comand-line mode type:  :/\<13.1\> and press enter  Or see picture below: After pressing enter it will go directly to 192.168.13.1,  if /13.1 is type then it will match also 192.168.13.135, 192.168.13.131 etc.. But with the /\<word or string\> syntax it will find the whole word. That's it, explore vi/vim for time saver tips. Cheers! Lift up your needs in Prayer; with a humble and contrite heart. God listens to the broken hearted....

Move specific log files via Bash

Log files are important, for audit purposes, to check what's going on and a file to consult when something doesn't work as expected. However, log files can fill up disk space easily especially if the application or server being monitored is quite buya, thus it will also record quite a few lines on the log file. Of course the easy way, is always to do an mv command and just move the file somewhere. This is not practical if you need to move hundreds of files. To make things easy and efficient is to use a group or series of commands. For example, if you need to move files other than the log files for the month of November, then ls and egrep can do it. Example: ls | egrep -iv "*2025-11*" | egrep -iv "2025-11-template"  Above will list all files except files for the month of November and the template for the month of November. -iv tells egrep insensitive case, and don't include the pattern specified and list all files Double check the output if the ls and egr...