Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Linux/Bash: Execute Variable for Quick and Dirty Command Line Shortcuts

Whilst in the process of moving my personal SVN repositories into a new home, I realised I had quite a few. Thought it'd be handy to write a short one liner to help myself out.

REPO="repository_name" ; CMD="svnadmin dump $REPO > $REPO.dump" ; eval $CMD

Replace repo each time you need to run it and you're run when you're ready!

541353_10151070649517596_613949486_n
A few days late, but happy Halloween!

Bash: Delete files listed in a text file

With so many unused py files in the project, I put together a few lines of code to get rid of them.

md5sum blank/tests.py
find -name "tests.py" -type f -exec md5sum {} + | grep 1fde9e174e80f7680d95dc905a3533a3 > empty_tests.txt

md5sum blank/models.py
find -name "models.py" -type f -exec md5sum {} + | grep 8c4eb991c6dca757bdd77f539092e29b > empty_models.txt

md5sum blank/views.py
find -name "views.py" -type f -exec md5sum {} + | grep ad656aaabae09dc285884ad38d889ef9 > empty_views.txt


# Delete any files listed in the text files
# awk splits up the md5 from the filename since md5sum output is "<hash> <filename>"
rm $(cat empty_*.txt | awk '{ print $2 }')

The first part collates a list of text files for any files with the common MD5 checksum.

In the second part:

  • cat reads in each empty_*.txt file
  • awk grabs the filename component of the checksum output
  • rm removes each filename returned by cat/awk

Sources

Linux: Quickly search for text within multiple file types

It's really easy to quickly search certain files in a large directory for some text.

find -name "*.html" -or -name "*.json" -or -name "*.js" | xargs grep -i "full_uri"

The "find" command searches out those specific files.

The "xargs grep" will search the files (-i makes it case insensitive).

130_Cats 

Now its much easier to find your stuff, even with a million lolcats swarming the place.

Vim: Stop Vim from reading standard input

wnguyen@europa:~$ d | vim -
Vim: Warning: Output is not to a terminal
Vim: Reading from stdin...

Ahh shit, I hate it when you accidently type something and then lock up the terminal.

Usually, pressing CTRL+C will unlock the console but sometimes it doesn't work.

Press CTRL+Z to get back to the console and then kill the processes manually.

Bash: List and kill a process

To list the processes you're running in the current terminal, type "ps".

Figure out which processes from the list you want to kill and select the process IDs (PIDs).

wnguyen@europa:~$ ps
  PID TTY          TIME CMD
22452 pts/16   00:00:00 bash
18595 pts/16   00:00:00 vim
18596 pts/16   00:00:00 vim
20490 pts/16   00:00:00 ps

wnguyen@europa:~$ kill -9 18595 18596

That will kill off both vim processes. Type ps again to make sure they're dead.

image
Dead like this.

Linux: Dump contents of all files matching certain filename

I had to look for a specific line of code across multiple branches of a project. There were over 30 or so branches, so it'd be terribly time consuming to look through them all one by one.

Instead of doing that, write up a little snippet that'll dump all the contents of those files into vim.

find -name "your_filename.py" -exec echo '-----' {} '-----' \; -exec cat {} \; | vim -

This will search any folder under the current working directory for "your_filename.py" and dump its contents into vim.

The contents of the files are seperated by "----- path/to/your_filename.py -----" for easy searching.

[ Source ]

Bash: Find the Nth character in a file

cat "filename.html" | head -c 39950 | less
  • Cat will read the file out
  • Head will truncate it to the Nth character you want.
  • Less will stop the output from flooding your console.

Bash Shell: Check for optional arguments

One day, I got sick of typing up the whole command over and over.

I needed to write a script that took in one mandatory argument and a second optional argument.

To make the second argument optional, use the "-z" operator checks if a string is empty. The "-n" operator checks for the string being not empty.

if [ -z "$1" ]; then
echo "You need at least 1 argument"
exit 1
fi

if [ -z "$2" ]; then
DOMAIN=$1
else
DOMAIN=$2
fi

CMD="command something $1 -some -flags $DOMAIN"
echo $CMD
$CMD

Remember to "chmod +x filename" to make it executable.

Now you can begin performing all crazy sorts of bash-fu.

PowerPunch

[ Source ]

Bash Shell: Passing in arguments to build a reusable command

Aliases are great, but it sucks when you need to mix arguments into certain parts of the command rather than after the alias itself.

There are some things we type on a regular basis which cant be put into aliases. An example would be the command I use to clone from git.

git clone git@git.server.com:git_project_name.git clone_name

I would of liked to use an alias in this way:

alias_name git_project_name clone_name

But when executed, it'll come out as:

git clone git@git.server.com:.git git_project_name clone_name

For this, I've resorted to writing a script called "clone_git.sh".

#! /bin/bash
# script to git clone

PROJECT=$1
shift
SEARCH="git clone git@git.server.com:$PROJECT.git $@"

echo $SEARCH
$SEARCH

Using the script with this syntax:

./clone_git.sh project_name clone_name arg1 arg2 arg3

Will produce:

git clone git@git.server.com:project_name.git clone_name arg1 arg2 arg3

The shift command removes the first argument off the list, so "project_name" will be removed from "$@".

Now if you dont require a variable number of arguments, you can simplify the script by removing the $PROJECT variable, removing shift and replacing "$@" with "$2".

The script will now look like:

#! /bin/bash
# script to git clone

SEARCH="git clone git@git.server.com:$1.git $2"

echo $SEARCH
$SEARCH

Calling the script with the same syntax will now produce:

git clone git@git.server.com:project_name.git clone_name

[ Sources: $@ and Shift ]

Setting aliases using Linux bash shell script with arguments

Its useful to use shortcut commands for common tasks, especially when it comes to repository management.

I like to pipe diff output into vim and logs into less, but at times I have to switch between multiple repositories such as git, svn and bzr.

To use the same alias between the repositories, set up a script and give it an argument.

Create a script file to manage your aliases.

touch workwith.sh
chmod +x workwith.sh

Create an alias to make it easier to access. You may want to put this into your ".bashrc" profile.

alias workwith="source ~/bin/workwith.sh"

Modify your "workwith.sh" file and paste

#! /bin/bash

if [ "$1" = "git" ]; then
        alias d="git diff | vim -"
        alias commit='git commit -m'
elif [ "$1" = "svn" ]; then 
        alias d="svn diff | vim -"
        alias commit='svn commit -m'
elif [ "$1" = "bzr" ]; then
        alias d="bzr diff --diff-options='-U 5 -p' | vim -"
        alias commit='bzr commit -m'
else
        echo "Unknown command"
fi

Now, whenever you want to switch between the commands, type

workwith svn

workwith git

workwith bzr

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog