repair a broken Ext4 Superblock

January 18, 2017 - Reading time: 8 minutes

First, figure out what partition we’re dealing with.

sudo fdisk -l

The above will list all the partitions on all the drives in your computer. To recover a lost partition, your going to need Testdisk. Testdisk is included in Parted Magic, and there’s a great guide on their site. For this though, we just need the partition number, such as /dev/sda3 or /dev/hdb1. Now, make sure your superblock is the problem, by starting a filesystem check, replacing xxx with your partition name. Here, you can change ext4 to ext3, or ext2 to suit the filesystem.

1
sudo fsck.ext4 -v /dev/xxx

If your superblock is corrupt, the output will look like this

1
2
3
4
5
6
7
8
9
10
11
fsck /dev/sda5
fsck 1.41.4 (27-Jan-2009)
e2fsck 1.41.4 (27-Jan-2009)
fsck.ext4: Group descriptors look bad... trying backup blocks...
fsck.ext4: Bad magic number in super-block while trying to open /dev/sda5
 
The superblock could not be read or does not describe a correct ext4
filesystem.  If the device is valid and it really contains an ext4
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193 <device>

Now lets find where your superblock backups are kept.

1
sudo mke2fs -n /dev/xxx

Down at the bottom of this output, should be a list of the backups

 

1
2
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Your almost there. Finally, restore the superblock from the backup, again replacing the x’s with your partition name, and block_number with the first backup superblock.

 

1
sudo e2fsck -b block_number /dev/xxx

connecting to wireless networks from command line

December 16, 2016 - Reading time: 4 minutes

You can create a wireless network configuration that will be managed by Network Manager by creating files in /etc/NetworkManager/system-connections. Look at existing files to see how the format looks like.

Once the connection is created, you can use the nmcli command to manage NetworkManager from the command line, doing things such as enabling, disabling and querying connections.

Incidentally, the System Testing tool (checkbox) has a script that does exactly this: creates a connection and enables it, with parameters that you supply on the command line.

For instance, this creates a connection to the open "duck" network:

sudo /usr/share/checkbox/scripts/create_connection duck

This will create a connection to a network using WPA2 security, with "wings" password:

sudo /usr/share/checkbox/scripts/create_connection -S wpa -K wings duck

The script is written in Python so it should be easy for you to look at and adapt to your needs.

The script's help says this:

Usage: create_connection [options] SSID

Options:
  -h, --help            show this help message and exit
  -S SECURITY, --security=SECURITY
                        The type of security to be used by the connection.
                        One of wpa and wep. No security will be used if
                        nothing is specified.
  -K KEY, --key=KEY     The encryption key required by the router.
  -U UUID, --uuid=UUID  The uuid to assign to the connection for use by
                        NetworkManager. One will be generated if not
                        specified here.
  -R RETRIES, --retries=RETRIES
                        The number of times to attempt bringing up the
                        connection until it is confirmed as active.
  -I INTERVAL, --interval=INTERVAL
                        The time to wait between attempts to detect the
                        registration of the connection.

Option 1

Just edit /etc/network/interfaces and write:

auto wlan0
iface wlan0 inet dhcp 
                wpa-ssid {ssid}
                wpa-psk  {password}

After that write and close file and use command:

sudo dhclient wlan0

Replace {ssid} and {password} with your respective WiFi SSID and password.


Option 2

Provided you replace your Wireless network card, Wi-Fi Network name, and Wi-FI Password this should also work.

I am using: - Wireless network card is wlan0 - Wireless network is "Wifi2Home" - Wireless network key is ASCII code ABCDE12345

First, get your WiFi card up and running:

sudo ifconfig wlan0 up

Now scan for a list of WiFi networks in range:

sudo iwlist wlan0 scan

This will show you a list of wireless networks, pick yours from the list:

sudo iwconfig wlan0 essid Wifi2Home key s:ABCDE12345

To obtain the IP address, now request it with the Dynamic Host Client:

sudo dhclient wlan0

You should then be connected to the WiFi network. The first option is better, because it will be able to run as a cron job to start up the wifi whenever you need it going. If you need to turn off your WiFi for whatever reason, just type:

sudo ifconfig wlan0 down

FYI

I have also seen people using alternative commands. I use Debian, Solaris and OSX, so I'm not 100% sure if they are the same on Ubuntu. But here they are:

sudo ifup wlan0 is the same as sudo ifconfig wlan0 up
sudo ifdown wlan0 is the same as sudo ifconfig wlan down


preview GRUB menu without rebooting

November 18, 2016 - Reading time: ~1 minute
sudo apt-get install grub-emu

Then in terminal execute

grub-emu

download all links from a site and save to a text file

June 11, 2016 - Reading time: ~1 minute

wget is not designed for this. You can however parse its output to get what you want:

$ wget http://aligajani.com -O - 2>/dev/null | grep -oP 'href="\Khttp:.+?"' | sed 's/"//' | grep -v facebook > file.txt

You could use lynx for this:

lynx -dump -listonly http://aligajani.com | grep -v facebook.com > file.txt

This command dumps the links of a single page. To do this recursively:

      wget -r -p -k http://website 

or

wget -r -p -k --wait=#SECONDS http://website

The second one is for websites that may flag you if downloading too quickly; may also cause a loss of service, so use second one for most circumstances to be courteous. Everything will be placed in a folder named the same as website in your root folder directory or whatever directory you have terminal in at time of executing command.


mount a disk image from the command line

January 27, 2016 - Reading time: 2 minutes

If it was a hard-drive image with a MBR partition table, I would fdisk the image to find the offset for the partition I need to mount.

fdisk -lu /path/disk.img

Then I would mount it passing the offset.

mount -o loop,offset=xxxx /path/disk.img /mnt/disk.img.partition

The offset value is in bytes, whereas fdisk shows a block count, so you should multiply the value from the "Begin" or "Start" column of the fdisk output by 512 (or whatever the block size is) to obtain the offset to mount at.

On most modern GNU system the mount command can handle that:

mount -o loop file.iso /mnt/dir

to unmount you can just use the umount command

umount /mnt/dir

If your OS doesn't have this option you can create a loop device:

losetup -f # this will print the first available loop device ex:/dev/loop0
losetup /dev/loop0 /path/file.iso #associate loop0 with the specified file
mount /dev/loop0 /mnt/dir #It may be necessary specify the type (-t iso9660)

to umount you can use -d:

umount /mnt/dir
losetup -d /dev/loop0

If the file have partitions, example a HD image, you can use the -P parameter (depending on you OS), it will map the partitions in the file content:

losetup -P /dev/loop0 /path/file.iso # will create /dev/loop0 
ls /dev/loop0p* #the partitions in the format /dev/loop0pX

how to recursively delete empty directories in home directory

December 11, 2015 - Reading time: ~1 minute

The find command is the primary tool for recursive file system operations. Use the -type d expression to tell find you're interested in finding directories only (and not plain files). The GNU version of find supports the -empty test, so

$ find . -type d -empty -print

will print all empty directories below your current directory.

Use find ~ -… or find "$HOME" -… to base the search on your home directory (if it isn't your current directory).

After you've verified that this is selecting the correct directories, use -delete to delete all matches:

$ find . -type d -empty -delete

I would add -mindepth 1 here, to prevent from deleting the starting directory itself, if it would be empty. It's not really probable case for $HOME but if you would use this on any other directory..