linux-unix

recent posts related to linux and unix

Linux: Recursive file searching with grep -r (like grep + find)

Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Solution: find + grep

For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, “Search all files in all subdirectories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify.

Linux Mint: How to change the disk encryption password alvin August 17, 2017 - 10:26am

If you ever need to change the password you used to encrypt your Linux Mint hard drive — the full disk encryption of the entire hard disk you used when you installed Mint — I just found that the commands at this linuxmint.com page worked as desired.

In short, I used this command to see how my hard drive was encrypted:

A collection of Unix/Linux ‘grep’ command examples

Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name grep means "general regular expression parser", but you can think of the grep command as a "search" command for Unix and Linux systems: it's used to search for text strings and more-complicated "regular expressions" within one or more files.

I think it's easiest to learn how to use the grep command by showing examples, so let's dive right in.

Ubuntu ‘apt-get’ list of commands (cheat sheet)

I have a couple of Ubuntu Linux systems, including Raspberry Pi systems, test servers, and production servers. It seems like every time I have to use an apt-get or other apt command, I always have to search for the command I need. To put an end to that, I’m creating this “apt-get reference page.” It’s very terse, as I’ve just written it for myself, but I hope it’s also helpful for others.

How to list all services on an Ubuntu 16.04 system alvin July 23, 2017 - 1:13pm

Ubuntu FAQ: How do I list all of the services on my Ubuntu 16.04 system from the Linux command line?

Answer: Use this command:

Sorting ‘ls’ command output by filesize alvin July 14, 2017 - 11:15am

I just noticed that some of the MySQL files on this website had grown very large, so I wanted to be able to list all of the files in the MySQL data directory and sort them by filesize, with the largest files shown at the end of the listing. This ls command did the trick, resulting in the output shown in the image:

ls -Slhr

The -S option is the key, telling the ls command to sort the file listing by size. The -h option tells ls to make the output human readable, and -r tells it to reverse the output, so in this case the largest files are shown at the end of the output.

A Linux shell script to find large files alvin July 14, 2017 - 11:14am

I made a mistake in configuring logrotate on a new Linux system, and almost ran into a problem because of that. Fortunately I saw the problem before it became a BIG problem, but as a result, I decided to add a script to my Linux system to check for large files, typically log files that have grown out of control for one reason or another.

Here then is a simple Linux shell script I named LargeFileCheck.sh, which searches the filesystem for files that are larger than 1GB in size:

A Linux shell script to rename files with a counter and copy them

As a quick note, I used this shell script to copy many files with the same name into a directory named tmpdir, giving them all new names during the copy process:

count=1
for i in `cat myfiles`
do
    fname=`basename $i`
    cp $i tmpdir/${count}-${fname}
    count=`expr $count + 1`
done

How to use curl to get headers from a URL

Curl FAQ: How do I use curl to get the headers from a website URL?

Short answer: Use curl's -I option, like this:

$ curl -I URL

Here's a specific example, including a real URL and results:

Free Unix/Linux and vi/vim cheat sheets alvin June 14, 2017 - 3:14pm

Way back in the 1990s I created some “cheat sheets” for Unix training classes that I taught. Somewhere in the 2000s I updated them to make sure they worked with Linux as well, Here then are two Unix/Linux cheat sheets I created (way back when) that you can print out if you’re just learning Linux and the vi/vim editor: