Cool Bash Commands

Remove the ABCD– prefix from a filename:
$ s='ABCD --11212239.pdf'
$ mv "$s" "${s#ABCD--}"


Remove the prefix from all files in current directory recursively:
while IFS= read -r -d '' filename; do
mv "$filename" "${filename#ABCD--}"
done < <(find . -type f -name 'ABCD--*' -print0)

Remove the .pdf suffix from a filename:
$ s='ABCD --11212239.pdf'
$ mv "$s" "${s%.pdf}"


Remove the suffix from all the files in the current directory recursively:
while IFS= read -r -d '' filename; do
mv "$filename" "${filename%.pdf}"
done < <(find . -type f -name '*.pdf' -print0)

Posted in Linux | Comments Off on Cool Bash Commands

Resize a Volume Group in Linux

#Check size of Volume Group
df -h -T | grep vg
#Display volume group info. Check for free space
sudo vgdisplay
#Display logical volume info
sudo lvdisplay
#Test run
sudo lvextend -t -v -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
#Actual run
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
#Resize logical volume
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
#Verify
df -h -T | grep vg

Posted in Linux | Comments Off on Resize a Volume Group in Linux

Booting into Single User Mode from Grub

To boot into single user mode from the Grub menu:

In Ubuntu, the Grub Menu should appear if you press and hold Shift while Grub is loading, if you boot using BIOS. If your Ubuntu system boots using UEFI, press Esc instead. You might have to press the key multiple times.

When you get to the Grub Menu, edit the line that starts with linux by adding any one of these to the end of the line:
s (lower case s)
S
(upper case s)
1
(number 1)
If the system is using systemd, add one of these to the end of the line that starts with linux and use the key sequence that is listed at the bottom to boot the new configuration (usually ,<ctrl>X or F10):
systemd.target=rescue.target
rescue

Since single user mode gives you root access to the system, it is a good idea to require the root password when booting into it. To do this,

If the system is using systemd, edit the ExecStart line in both /lib/systemd/system/rescue.service and /lib/systemd/system/emergency.service by changing sushell to sulogin. After saving these changes, when you edit the Grub menu and boot into single user mode, you will be required to enter the root password for access.

Posted in Linux | Comments Off on Booting into Single User Mode from Grub

No space left on device

  1. Log into system: ssh <system>
  2. Check for full filesystem: df -h
  3. Detemine the largest files on the filesystem:
    du -ah / --exclude="/proc" | sort -h -r | head -n 5 2>/dev/null
  4. Check the output for something that can be deleted.
  5. If the du command doesn’t show anything, consider whether something that was running might have created a temporary file that exceeded the remaining space on the filesystem.
  6. If you find temporary files created at run-time that exceed the remaining drive space, you have 3 choices:
    A. add more drive space
    B. spread out the run times of jobs so that concurrent jobs don’t exceed the remaining drive space
    C. consider adding compression to the temporary files before writing them to the filesystem
  7. Check the inode usage with this: df -i
  8. If you find the issue is inode usage, you have 2 choices:
    A. delete file s of directories that use a large number of inodes:
    1. identify which files and directories are using the most inodes by running this:
    for i in /*; do echo $i; find $i | wc -l; done

    2. examine the output for toplevel directories that are using a lot of inodes
    3. re-run the command in step 1. replacing /* with the toplevel directory to check
    4. Once you determine where the issue is: delete expendable files or directories
    B. increase the number of available inodes:
    1. unmount the full filesystem: umount <filesystem mountpoint>
    2. update the number of inodes: mkfs.ext3 /dev/<device> -N <desired num of inodes>
    3. re-mount the filesystem: mount -a
Posted in Linux | Comments Off on No space left on device

Calculate MySQL usage

Calculate MySQL Memory Usage – Quick Stored Procedure

Posted in Linux | Comments Off on Calculate MySQL usage

Useful Nmap Commands

To see what systems on the 192.168.11.0 network are listening on port 22:
nmap -sV -p 22 192.168.11.0/24

To do an nmap ping scan on 192.168.11.0 network:
nmap -sp 192.168.11.0/24

To scan the top five ports on 192.168.11.7:
nmap --top-ports 5 192.168.11.7

Posted in Linux | Comments Off on Useful Nmap Commands

How to determine the Ubuntu version

Use the Linux Standard Base command as follows:
lsb__release -a

Posted in Linux | Comments Off on How to determine the Ubuntu version

Useful du commands

The du command is very useful when you are looking at disk space usage. Here are some of the most commonly used parameters:

-h human-readable Displays sizes in human-readable format, using units such as KB, MB, GB, etc
-a, –all Lists all files with their sizes (combine with h to get the best result)
-d, –max-depth=N
–time Lists all files with their sizes and the timestamp of its last modification
-X, –exclude=<pattern> Lists all files except those that match <pattern>

To find the disk usage of all files in the directory except the tarred files:

du -ah --exclude=".tgz" --exclude=".tar.gz"

To find the 10 largest files in the current directory ordered by file size:
(This gets both directories and files by size)
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
or
du -ah . | sort -n -r | head -n 10

(This gets only files ordered by size)
du -hsx * | sort -rh | head -10
or
du -ah --max-depth=1 | sort -h

Posted in Linux | Comments Off on Useful du commands

Track cpu stats

#Run iostat to get the average cpu usage every 10 seconds for 10 times:
iostat -x 10 10 >> /home/<user>/iostat.out.$(date +"%Y-%m-%d")

#Run dstat to get cpu stats for cpus 1, 3 and the total:
dstat -C 0,3,total

Posted in Linux | Comments Off on Track cpu stats

The top/htop command(s)

top and htop are free CLI process viewers. They list the top resource using processes being run.The information includes:

  • PID — Process Id The task’s unique process ID
  • PPID — Parent Process PID (Process ID) of a task’s parent
  • RUSER — Real User NameThe real user name of the task’s owner 
  • UID — User IdThe effective user ID of the task’s owner
  • USER — User NameThe effective user name of the task’s owner
  • GROUP — Group NameThe effective group name of the task’s owner.
  • NI — Nice value The nice value of the task. A negative nice value means higher priority, whereas a positive nice value means lower priority. Zero in this field simply means priority will not be adjusted.

top options:
a : Sort by memory usage This switch makes top to sort the processes by allocated memory
d : Delay time update interval as: -d ss.tt (seconds.tenths)
p : Monitor PIDs as: -pN1 -pN2 … or -pN1, N2 [,…]
u : <somebody> Monitor only processes with an effective UID or user name matching <somebody>.
htop options:
-u –user=USERNAME Show only the processes of a given user
-d –delay=DELAY Delay between updates, in tenths of seconds

Examples:

To run top in batch mode (updating every 3 seconds) and write it to a file in my directory
top -b -d 10 -n 3 >> /home/<user>/top-file

To run htop to get only processes for my user:
htop -u <user>

Posted in Linux | Comments Off on The top/htop command(s)