Skip to main content

Posts

Showing posts with the label Bash Linux Noob

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...

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,...

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...

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...

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...