crop cyber spy hacking system while typing on laptop

Useful Linux Commands

🌱 Seedling

I created this page to share some ‘beyond basic’ Linux commands that I have needed to devise to solve specific problems. Partly this is documentation for my own purposes (though I have them stored elsewhere as well) but mostly it’s here because if I had to figure them out, then I couldn’t find a solution online (or deemed it too complex to search for).

Find useful commands in your command history.

This command was created for the purpose of populating this page! I wanted to look back in my command buffer, using the history command, but there is lots of really basic stuff in there. Depending on your settings you may also have duplicates.

history | sed 's/^ *[0-9]* *//' | sort | uniq | \
 egrep -v '^(sudo )?(cd|cp|crontab|ls|man|mv|nano|rm)' | \
 egrep -v '^(sudo )?[a-z]+ ?$' > history.txt

The first line pipes the history into sed to remove the line numbers, then sorts and de-duplicates it.

The second line gets rid of the basic commands I did not want to see, and copes with sudo versions of the same. You should add any other commands in here that you do not want to see. I initially included cat in this list, but discovered I have some fairly interesting commands that were piped sequences beginning with cat.

The third line gets rid of ‘bare’ commands, also allowing for sudo versions. A ‘bare’ command is a command with no parameters. These are commands you either know or you don’t! Finally, the results are redirected to the history.txt file in your home directory.

List a specific range of lines in a file.

This one I did technically find online, but it came in so useful that I decided to include it here. My use case was finding out what tripped up the mysql command when running an import to a database. The error would explain what the problem was, such as ‘primary key must be specified’, and would then list the line number. To ascertain what the problematic statement was, I needed to know what was at that line!

head -n 14 export.sql | tail -n +10

The command above displays lines 10 through 14 inclusive of the file export.sql. Typically this is how I would find out why there was an error at line 12, where going a little back and forth from that spot helps with context. Obviously this example could trivially have been done with a simple head -n 14 but in reality I was getting error line numbers like 671.

Find WordPress configuration values across multiple sites.

I have multiple WordPress sites running on my server and was migrating their databases to a different server. When I thought I was done, I wanted to check that I had changed every wp-config.php file to specify the correct host name.

sudo find -L /var/www -type f -name wp-config.php \
 -exec grep -H 'DB_HOST' {} \; | \
 grep -v '://'

The first line uses the find command to locate all wp-config.php files in any subdirectories of /var/www. The -L option allows for a couple of symbolic links I have in there pointing to some WordPress directories located elsewhere. It runs as sudo as my user does not have permission to some directories.

The second line is still part of find and specifies what to do with the found files, specifically here to execute a command. The command in question is a simple grep for the configuration line I am seeking, with the -H option added to include the file name in each line of output so I can make sense of where each matched line has come from.

The third line then strips out commented lines. I needed this as I had commented out the old host specification when adding the new one. The :// matches on the : that appears after printing the file name, followed by the initial two characters of the PHP line — the // that denotes a comment line.