Moving from subdomain to top level, link/image paths incorrect after manual edit TL;DR Filepaths contain old subdomain prefix after find replace database and site code, can't determine cause.

I have a hosted site on mywebsite.com/drupal, I want to move it to my computer localhost. 

I have a LAMP stack and I compress and download the contents of mywebsite.com/drupal and export its mysql database, mydb.sql, using phpmyadmin. 

I uncompress and place all the files from mywebsite.com/drupal in /var/www/html/, I create a new database, local.db, locally and import mydb.sql. I edit /var/www/html/sites/default/settings.php $databases array and connect it to local.db.

I ensure all files permissions are correct by running

chown user:user -R /var/www/html/
chmod -R 755 /var/www/html/


I can then access my site using a browser and going to localhost, however the images are missing none of the links work. 

Initially I thought the cause was that there are many instances in the database and my site code still containing filepaths with the subdomain /drupal/ prefix, images for example: 

src="/drupal/sites/default/files/inline-images..."


When I move the site to localhost, it should just be:

src="/sites/default/files/inline-images..."


To solve this, I did a find /drupal/ and replace / in my database, reimported it and then used grep -rl '/drupal/' ./ | xargs sed -i 's_/drupal/_/_g' on /var/www/html/.

I thought this would have resolved the problem, however when I access localhost, the problem persists, images and links still contain the /drupal/ prefix in their filepaths. I've read on a few migration guides that I should flush all caches on the drupal backend before moving  (which I did) and also potentially flush the mysql cache using phpmyadmin, however I get an error #1146 - Table 'mydb.cache' doesn't exist when I try to execute the truncate commands.

Other guides mention .htaccess, which I copied over from mysite.com/drupal, admittedly I don't understand everything inside it deeply, but I can't find any code which is prefixing filepaths with /drupal/.

As a sanity check, I created /var/www/html/drupal/ and copied my site and then everything works fine.

I don't understand, how can the filepaths remain the same when I have changed all instances in the database and site code? I think there is something easy I'm missing but I can't figure it out.
Is this Drupal 7 or 8? Not sure that it matters, but take a look at around line 102 of your .htaccess file, should look like this:

# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
# RewriteBase /drupal


The issue you describes sounds like the RewriteBase is incorrect. What does your .htaccess say?

Can you also tell me what your settings.php file has for the base URL value?

# $base_url = 'http://www.example.com';  // NO trailing slash!