Linux Command Line useful scripts and tricks

Milo Lamar's picture

This thread is for useful command line Linux tricks. The Bash big list thread contains a big list of all the basic commands and their manual page links.  This thread is for taking them a step further.  Combine commands with pipes "|", useful regular expression search and replace tools, and more!  Think of the aforementioned thread as a vocabulary and this thread as language - the combining of vocabulary with linux operators to do amazing things that will (hopefully) save you time and have already changed my hacker life.

Anonymous's picture

Give group write permissions to all files in a directory

$ find . -exec chmod g+w {} \;

Milo Lamar's picture

Bulk rename files that match a pattern

I had a ton of .jpg files that had automatically appended numbers to the end of them because they had the same file name after downloading a bunch of jpeg images from Library of Congress. Here is a generalized example of the list I saw and how I fixed it so they all ended in the jpg extension.
$ ls
filename.jpg filename.jpg.1 filename.jpg.2 filename.jpg.3
$

I used some shell script commands to loop over all the file names matching a given pattern and replace them as needed. When doing something like this, ALWAYS do a printed output version of the command first so you don't go overwriting a bunch of files unintentionally.
$ for x in filename.jpg.*;do echo $x will be moved to: ${x//.jpg}.jpg;done
filename.jpg.1 will be moved to: filename.1.jpg
filename.jpg.2 will be moved to: filename.2.jpg
filename.jpg.3 will be moved to: filename.3.jpg
$

This prints out all the file names and what they will become. The result was to remove the .jpg from the file names then tack it on the end. If the result looks good, then just replace echo with mv and get rid of the extra text. Be extra careful you aren't going to replace files you'll miss! Make backups first if you're a paranoid hacker like myself.
$ for x in filename.jpg.*;do mv $x ${x//.jpg}.jpg;done
$ ls
filename.jpg filename.1.jpg filename.2.jpg filename.3.jpg
$

Viola!

Milo Lamar's picture

Find files larger than a certain size

$ find /path/to/files -type f -size +1024k
This lists all files bigger than 1MB (1024KB). /path/to/files is the directory you want to search in. The search is recursive by default, so it will search all sub-directories as well.

Milo Lamar's picture

Just print the number of files in a directory

$ find /path/to/files -type f | wc -l
This recursively lists all files in a directory in the background and sends the output to wc with the "pipe" operator | wc -l. The command "wc -l" is short for word count, just print the number of lines. So it just counts the number of lines find /path/to/files -type f outputs, which is the number of files in /path/to/files.