Handling Spaces in File Names on Linux

Using ls to parse file names is not recommended for multiple reasons

https://mywiki.wooledge.org/ParsingLs

Let’s say we have a directory with two files in it.

Helloworld.txt
Hello, world.txt

Now we want to loop over the files. If we use ls in our for loop,

for file in $(ls); do echo "$file" ; done

We receive the following output

Hello,
world.txt
Helloworld.txt

The space in “Hello, world.txt” is translated as a new line. This could break our script.

Here is a better way

for file in * ; do echo "$file" ; done

Helpful links

https://mywiki.wooledge.org/BashPitfalls

More space needed on the /boot filesystem. RHEL / Fedora / Alma / Rocky

Error Summary
-------------
Disk Requirements:
   At least 28MB more space needed on the /boot filesystem.

The above error is due to the /boot partition being out of space. We can fix this issue by removing older unused Linux kernels. You could also increase the disk space, but that is a little more involved.

First we need to list which kernels we have installed.

rpm -qa | grep kernel

Example output

[incredigeek@apache ~]$ rpm -qa | grep kernel
kernel-core-4.18.0-522.el8.x86_64
kernel-tools-4.18.0-529.el8.x86_64
kernel-modules-4.18.0-526.el8.x86_64
kernel-4.18.0-526.el8.x86_64
kernel-modules-4.18.0-529.el8.x86_64
kernel-4.18.0-522.el8.x86_64
kernel-4.18.0-529.el8.x86_64
kernel-core-4.18.0-529.el8.x86_64
kernel-devel-4.18.0-522.el8.x86_64
kernel-core-4.18.0-526.el8.x86_64
kernel-devel-4.18.0-529.el8.x86_64
kernel-tools-libs-4.18.0-529.el8.x86_64
kernel-devel-4.18.0-526.el8.x86_64
kernel-headers-4.18.0-529.el8.x86_64
kernel-modules-4.18.0-522.el8.x86_64

The kernel in bold is the one we will remove.

Next we remove erase the old kernel(s)/items.

sudo rpm -e kernel-4.18.0-522.el8.x86_64 kernel-core-4.18.0-522.el8.x86_64 kernel-devel-4.18.0-522.el8.x86_64 kernel-modules-4.18.0-522.el8.x86_64

And now we continue with our update

sudo dnf update

Helpful links.

https://www.cyberciti.biz/faq/installing-kernel-2-6-32-131-2-1-el6-x86_64-needs-8mb-on-boot-filesystem/

Bob lost sudo access on Fedora

Bob has a computer running Fedora. When he installed Fedora he didn’t setup the root password and locked the root account. That is best practice. Right? Then one day he goes to upgrade to the latest version of Fedora and types in

sudo dnf update

and is greeted with

sudoers. This incident will be reported.

What happened? I had access before? Bob thinks to himself. Seems like I am not in the wheel group anymore. Bob being a smart person decides to attempt recovery mode. He’ll boot up and just readd his user to the wheel group.

Recovery mode starts up and then fails due to the root account being locked. What?!

Bob then starts talking to himself as he is in need of some expert advice. What other options do I have. I know! He runs to find his handy dandy Live Fedora pen drive. Plugs it in and boots up into a live version of Fedora. Now I can mount and access the main drive.

But wait, I can’t run “usermod -G wheel bob” because that will only affect the Live System. I could chroot into the drive. That would require mounting some extra mount points. Is there a faster way? We could maybe edit the /etc/group and add “wheel:x:10:bob”. That should add bob back to the wheel group. Right?

Wait, what about the sudoers file. We are normally supposed to use “sudo visudo” command to modify the file. Let’s check the file and see if we can just manually edit it.

$ stat -c "%n %a" /etc/sudoers
/etc/sudoers 440
$
Permissions on /etc/sudoers file

Hmm, okay I am going to need to change permissions to save the file. Let’s chmod that to 644 temporarily

$ sudo chmod 644 /etc/sudoers 

Alright now I should be able to edit it.

$ sudo vi /etc/sudoers

Okay, now I need to explicitly give myself permission to use sudo. Where is that line. Ah-ha!

root    ALL=(ALL)       ALL

Lets duplicate that with yy and p, replace root with my username.

root    ALL=(ALL)       ALL
bob     ALL=(ALL)       ALL

Save that with esc then :wq enter

Now change the file permissions back

sudo chmod 400 /etc/sudoers

Reboot the system and now lets login and test sudo.

$ sudo whoami 
root

Success!

Bob, satisfied that the problem is resolved, rewards himself by getting a sandwich.

sudo make me a sandwich

https://docs.fedoraproject.org/en-US/quick-docs/root-account-locked/

Linux commands for CPU, RAM and GPU info

Some helpful commands for showing hardware information on Linux.

Show memory speed

sudo dmidecode --type 17

Show CPU Frequency in MHz

cat /proc/cpuinfo | grep MHz

Show a bunch of system info with inxi, may need to install it.

inxi 

Another cool program is screenfetch. Gives a nice overview of system specs

sudo dnf install screenfetch

and run with

screenfetch

Using sed to format a phone number

Formatting an unformated “phone” number using sed.

There may be a different and easier way to do this, but the main thing to learn here is the ^, $, and [[:digit:]] options.

^ refers to the first part of a line
& which is our searched for pattern
$ refers to an end part of the line
[[:digit:]] searches for, you guessed it. Digits!

The following command reads the incoming 10 digit number form echo and does the following.

the ^ tells it that the pattern needs to match at the beginning of the line
[[:digit:]] repeated tells it to search for three consecutive digits
(&) tells it to put brackets around the & which is our searched for pattern in the first part.
We then pipe that to another sed command which
searches for 4 consecutive digits
the $ tells it that it needs to be at the end of the line.

echo "1234567890" | sed -e 's/^[[:digit:]][[:digit:]][[:digit:]]/(&) /g' | sed -e 's/[[:digit:]][[:digit:]][[:digit:]][[:digit:]]$/-&/g'

Resulting output is

(123) 456-7890

The following link was helpful while searching what the ^ and $ options do.

https://www.computerhope.com/unix/used.htm

Linux Screen – Create, Connect, Disconnect, Terminate Sessions

Screen is a handy tool that can help you run scripts on servers remotely without having to worry about the session getting terminated. It seems to operate kinda like a virtual console.

Create Screen Session

Create a new session with a specified name

screen -S SessionName

Example output below. Create session named testsession and print screen sessions.

[bob@localhost imapsync]$ screen -S testsession
[bob@localhost imapsync]$ screen -ls
There are screens on:
3313.testsession (Attached)
1 Sockets in /var/run/screen/S-bob.
[bob@localhost imapsync]$

Disconnect from Screen Session

You can disconnect from a screen session by hitting ctrl + a and then ctrl +d

“ctrl + a” then “ctrl + d”

List Screen Sessions

You can list the screen sessions with

screen -ls

Example

[bob@localhost imapsync]$ screen -ls
There are screens on:
3212.testsession (Detached)
2556.xap (Detached)

2 Sockets in /var/run/screen/S-bob.
[bob@localhost imapsync]$

Connect to screen Session

You can reconnect to a screen session with

screen -r testsession 

Terminate Screen Session

To terminate a screen session, connect to that session and then on a clear line hit ctrl + d

Same way as if you were closing a remote ssh connection.

Bash Loop Examples

For i in 1-100 do

Basically count to 100 and perform an operation each time i increases.

for ((i=1; i<=100;i++))
do 
  echo $i
done

for loop 1 liner

for ((i=1; i<=100;i++)) do echo $i ; done

While true (Execute forever)

Handy if you just want a script to run and repeat the same thing over and over again. Doesn't stop till you kill it.

while true
do
  echo "Repeat till infinity"
  sleep 1
done

While command is true

The following will execute the loop as long as the command in the () returns true. Once it returns false, it'll stop the loop

while (fping incredigeek.com | grep alive); 
do
  echo alive
  sleep 1
done

Bash array example

#!/bin/bash
array=(one two three)
echo "Printing first object in array."  #Replace 0 with the place number of the array item
echo ${array[0]}

echo ""

echo "Whole array"
echo ${array[*]} 

echo "" 

echo "Array indexes" 
echo ${!array[*]}

Output

Printing first object in array. 
one

Whole array
one two three

Array indexes
0 1 2

https://www.linuxjournal.com/content/bash-arrays

Delete files older than x days – Linux


You can use find command to find and delete files older than the specified days. In this case 30.

find /backup/* -mtime +30 -exec rm {} \;

Non recursive example. The -prune option should limit find to only look for files in the /backup directory. So it won’t check any subdirectories.

find /backup/* -prune -mtime +30 -exec rm {} \; 

Setting up Proxy over SSH on Linux

Initiate a ssh connection to the server or device you want to use as a proxy. You can change the port to something else if so desired.

ssh username@ipaddress -D 1880

Log in and leave the session running

You can now setup your computer or browser to use the Proxy.
Specify SOCKS Host, hostname is either localhost or 127.0.0.1, the port is 1880.

Firefox example below.