xargs


Git: Delete all local branches

The following command will:

  • print all branches that were merged to master
  • then filter out the branch named master and the branch you are currently switched to
  • and finally, it will delete the rest (one branch at a time).
git branch --merged master | grep -v -e "\*" -e "master" | xargs git branch -D

Tip:

To cleanup any remote-tracking references that no longer exist on the remote use the following:

git fetch --prune

Pull all Git repositories you have access to


ssh [email protected] info | cut -f 2 | tail -n +3 | xargs -I {} -n 1 -I_repository -- sh -c 'cd _repository; git pull; cd ..;'

The above command will connect to the git server (git.bytefreaks.net) using  gitolite and get a list of all the repositories you have access to using ssh [email protected] info

The command should return a list similar to this:

hello bytefreaks, this is git@git running gitolite3 v3.5.3.1-1-gf8776f5 on git 1.7.1

 R W	Repo1
 R W	Repo2
 R W	Repo3
 R  	Repo4

From the results, we remove the first 3 lines as they contain no useful information to cloning all the repositories. From the rest of the lines, where each line contains the information for a repository we have access to, we keep the third column only as it is the one that holds the repository name as it is stored on the server.

Afterwards it will remove all columns except the second to filter the column with the repository names and will remove the first 3 lines to keep only the data we are interested in.

On the last stage of the pipe we have a list of the names of the repositories, using xargs, we assign each repository name to the _repository variable and using one result at a time, we navigate into the folder of the repository using cd and call the pull command.

Note: We assume that all repositories are in the current folder as children and each one is in a sub-folder of its own which is named as the repository is.


Clone all repositories you have access to over ssh


ssh [email protected] info | cut -f 2 | tail -n +3 | xargs -I {} -n 1 git clone ssh://[email protected]/{}

The above command will connect to the git server (git.bytefreaks.net) that is using  gitolite and get a list of all the repositories you have access to using ssh [email protected] info

The command should return a list similar to this:

hello bytefreaks, this is git@git running gitolite3 v3.5.3.1-1-gf8776f5 on git 1.7.1

 R W	Repo1
 R W	Repo2
 R W	Repo3
 R  	Repo4

From the results, we remove the first 3 lines as they contain no useful information to cloning all the repositories. From the rest of the lines, where each line contains the information for a repository we have access to, we keep the third column only as it is the one that holds the repository name as it is stored on the server.

On the last stage of the pipe we have a list of the names of the repositories, using xargs, we assign each repository name to the special variable {} and processing one result at a time we clone the git repository to the current directory under the folder that is named as the repository.


Find all files that contain a string (Get filenames) 5

Assuming you have a lot of files and you need to find all files that contain a certain string value, you can check them all automatically using the following command.

Method 1 – Using find -exec

Using the exec flag of find, you can process all files even those that their filenames contain single or double quotes or other special characters.
The following command will return a list, the list of files that contain the value ‘string’.

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

The above command breaks down as follows:

  • find . -type f Find all files in current directory.
  • -exec For each match execute the following.
  • grep 'string' '{}' Search the matched file '{}' if it contains the value ‘string’.
  • -s Suppress error messages about nonexistent or unreadable files.
  • -l (lambda lower case) or --files-with-matches Suppress normal output, instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

To exclude binary files from matching, add the -I parameter on grep.

Method 2 – Using xargs

This method will not work for filenames that contain single quotes as it will confuse xargs.

find . -type f | xargs grep 'string' -s -l;

To exclude binary files from matching, add the -I parameter on grep.

Note: WordPress might change/replace the apostrophe to another ASCII apostrophe punctuation symbol! Make sure you use the straight apostrophe symbol or else you will not get the result expected since the behavior of grep will change. See Apostrophe on WikiPedia Here.