Tag Archive for Shell

Remame files to lowercase in Linux

rename 'y/A-Z/a-z/' *

source

Grep for files that do not match a pattern

grep -L "the pattern" *

# to recurse into subdirectories

grep -RL "the pattern" *

source

comparing the checksums of two files with Perl and cksum

cksum file1 file2 | perl -ane '$x ||= $F[0]; warn if $x != $F[0]'

# This can be shortened slightly, to:

cksum file1 file2 | perl -ane 'warn if $F[0] != ($x ||= $F[0])'

# If the checksum value we need to match is already known, that can be
# shortened to:

cksum file1 file2 | perl -ane 'warn if "664253628" != $F[0]'

# on Mac, if you have copied a checksum value into the clipboard, use
# pbpaste to do the comparison

cksum file1 file2 | perl -ane 'warn if `pbpaste` != $F[0]'

source

*nix – basic commands

# For more details on a command see the man page for the command. For example, to see more information about the command rm, enter the command
man rm

# To display a list of commands related to a topic enter
man -k topic
# or
apropos topic

# Remember: UNIX COMMANDS ARE CASE SENSITIVE!

#  |||||||||||| ACCOUNT |||||||||||||||||

passwd            # ... set a new password for your account
assets            # ... view information on resource use by your account, including connectivity time, cpu use, and disk space. (Availableon C&C systems only.)

# ||||||||||||| DIRECTORIES ||||||||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
pwd               # ... find out what your current working directory is
mkdir playdir     # ... create a new subdirectory called 'playdir'
rmdir junkdir     # ... remove subdirectory 'junkdir' (must be empty of files and subdirectories)
cd /usr/bin       # ... change to '/usr/bin' directory
cd ..             # ... change to directory one level above current working directory
cd                # ... change to home directory for the account you are using
du                # ... display the number of disk blocks in use (the total combined size of all files) in each directory and subdirectory.

# |||||||||||||||| FILES |||||||||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
ls                # ... list files in the current working directory
ls notes          # ... list file in the subdirectory named 'notes'
ls -l             # ... list all files in the current working directory, along with each file's permission, owner, size in bytes and date of last modification.
ls -a             # ... list files in the current working directory, including dot files (those files with names beginning with a period)
ls -F             # ... list files in the current working directory, indicating executable files with an asterisk (*) and subdirectories with a /
find docs -name *.memo -print # ... list all files in the directory 'docs' and any subdirectories of 'docs' with filenames ending in '.memo'. The  is necessary before the * to insure that is is interpreted as a wild character.
cp file1 file2    # ... copy the file 'file1' to a file named 'file2' (original file remains intact with the same name)
mv oldf newf      # ... move the file 'oldfile' to a file named 'newfile' (equivalent to renaming a file)
rm badfile        # ... remove the file 'badfile' (the file is permanently removed and cannot be recovered)
rm -i *.c         # ... remove all files in the current directory with the suffix '.c' and be asked for confirmation on each file
chmod a+r resul   # ... grant read access of the file 'resul' to all users
cat shortfile     # ... display the file 'shortfile'
more longfile     # ... display the file 'longfile', a screenful at a time
head big          # ... display the first ten lines of file 'big'
head -25 big      # ... display the first 25 lines of file 'big'
tail big          # ... display the last ten lines of file 'big'
tail -25 big      # ... display the last 25 lines of file 'big'
grep done tasks   # ... display all lines within file 'tasks' containing the string 'done'

# |||||||||||||||||||| JOBS ||||||||||||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
ps                # ... list the status of your jobs by process identifier (PID)
ps -aux           # ... list the status of all jobs by process identifier (PID)
jobs              # ... list the status of all jobs by job number
z                 # ... suspend the job currently running in the foreground
bg                # ... resume the most recently suspended job into the background
bg %2             # ... resume job number 2 in the background
fg %2             # ... resume job number 2 in the foreground
fg %3             # ... bring job number 3, which is running in the background, into the foreground
kill %2           # ... kill job number 2
kill 1234         # ... kill job with PID 1234

# ||||||||||||||||||||| NETWORKING |||||||||||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
ssh becker.u      # ... establish an interactive session on computer 'becker'

ftp sun.latin.washington.edu # ... transfer a file to or from computer 'sun.latin.washington.edu'

# |||||||||||| COMMS WITH OTHERS |||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
who               # ... find out who is logged on to the computer
w                 # ... find out who is logged on to the computer and what they are doing
finger suzz       # ... display information about user 'suzz'
biff y            # ... be notified if mail arrives. To turn off notification, enter the command 'biff n'
pine              # ... start pine mailer.  Pine is an alternative to 'mail' that many people find easier to use than 'mail'

# ||||||||||||||| MISC ||||||||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
cal 6 1990        # ... display a calendar of June, 1990.
date              # ... display current date and time
script            # ... start recording of all screen interactions
exit              # ... stop script recording (text from recorded session will be in a file named typescript )
alias dir ls -Fal # ... alias 'dir' to represent the command 'ls -Fal'
unalias dir       # ... remove the alias for 'dir'
alias             # ... show all current aliases

# |||||||||||| CONTROL CODES (USED WHEN AT UNIX COMMAND LINE) ||||||||||||

# DO:                        TO:
# ---------------   ---------------------------------------------
u           # Delete entire line
w           # Delete the preceding word
h           # Delete the preceding character
c           # Abort the program currently running
z           # Suspend the program currently running (use fg or bg to resume the program in the foreground or background, respectively)

# Redirecting and Piping

cc myprog.c > listing    # ... run C compiler on 'myprog.c' source file, redirecting compilation messages to a file named 'listing'. The resulting file would NOT include any diagnostic messages.
cc myprog.c >& listing   # ... run C compiler on 'myprog.c' source file, redirect compilation messages, including diagnostic messages, into a file named 'listing'.
ps -aux | more   # ... list all current jobs on the system, piping the result to more for viewing one screen at a time
cat frog >> rat    # ... concatenate the contents of file 'frog' onto the end of file 'rat'

source

Special variables in Windows Batch files

rem special variables for batch files

@echo off

echo fully qualified name of this batch file:

echo %~fn0

echo path to here:

echo %~dp0

echo arguments passed (if any):

echo %*

source

Interactive CLI promt with PHP without echoing to terminal

/**
* Interactively prompts for input without echoing to the terminal.
* Requires a bash shell or Windows and won't work with
* safe_mode settings (Uses `shell_exec`)
*/
function prompt_silent($prompt = "Enter Password:") {
if (preg_match('/^win/i', PHP_OS)) {
$vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
file_put_contents(
$vbscript, 'wscript.echo(InputBox("'
. addslashes($prompt)
. '", "", "password here"))');
$command = "cscript //nologo " . escapeshellarg($vbscript);
$password = rtrim(shell_exec($command));
unlink($vbscript);
return $password;
} else {
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return;
}
$command = "/usr/bin/env bash -c 'read -s -p ""
. addslashes($prompt)
. "" mypassword && echo $mypassword'";
$password = rtrim(shell_exec($command));
echo "n";
return $password;
}
}

source

GPSBabel upload routes and waypoints to Garmin

on run
tell application "Finder"
set sel to selection
set output to ""
repeat with x in sel
set p to POSIX path of (x as text)
set AppleScript's text item delimiters to "."
set ext to last text item of p
set output to output & (do shell script "/Applications/gpsbabel -w -r -i " & ext & " -f " & quoted form of p & " -x transform,rte=trk -x nuketypes,tracks -x simplify,count=50 -o garmin -F /dev/cu.PL2303-0000101D ") & "
"
end repeat
choose from list paragraphs of output with title "GPSBabel result" with empty selection allowed
end tell
end run

source

shell script to download webkit nightly build

#!/bin/sh

tmpdir=/tmp/webkit-nightly
URL=$(curl <a href="http://nightly.webkit.org/" >http://nightly.webkit.org/</a> | grep dmg | head -1 | perl -pe 's/.*(http.*dmg).*/$1/')
FILE=$(basename $URL)
mkdir -p $tmpdir &&
cd $tmpdir &&
curl $URL --remote-name &&
hdiutil attach -quiet $tmpdir/$FILE &&
mv /Applications/WebKit.app ~/.Trash/WebKit-`date +%Y-%m-%d_%H-%M-%s`.app/ &&
cp -R /Volumes/WebKit/WebKit.app /Applications/ &&
hdiutil detach -quiet /Volumes/WebKit &&
echo Installed $FILE

source

Shell command for removing all TortoiseSVN .svn files (placed into registry)

Create a new key:
HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellDeleteSVN

Text of the default REG_SZ string should be:
Delete SVN Folders

Create a new key:
HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellDeleteSVNcommand

Text of the default REG_SZ string should be:
cmd.exe /C  "TITLE Removing SVN Folders in %1 && FOR /r "%1" %%f IN (.svn) DO RD /s /q "%%f" "

source

Get exaile’s current song through dbus from commandline

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Simple script to get the current song being played by
# exaile.
# Author: Santiago Zarate <santiago [at] zarate [dot] net [dot] ve>
# Blog: <a href="http://blog.santiago.zarate.net.ve" >http://blog.santiago.zarate.net.ve</a>
#

import sys, dbus

bus = dbus.SessionBus()

try:
remote_object = bus.get_object("org.exaile.DBusInterface","/DBusInterfaceObject")
iface = dbus.Interface(remote_object, "org.exaile.DBusInterface")
if(iface.status() == 'playing'):
message = '%s - %s - %s ' % (iface.get_title(), iface.get_album(), iface.get_artist())
else:
message = 'Exaile is not playing'

except dbus.exceptions.DBusException, e:
message = 'Exaile is not running: %s' % e

print message

source