query Internet name servers interactively
e.g.,
nslookup rf.tamus.edu yields:
Server: 10.200.2.220
Address: 10.200.2.220#53
Non-authoritative answer:
rf.tamus.edu canonical name = vpr-wp.tamu.edu.
Name: vpr-wp.tamu.edu
Address: 128.194.89.176
sudo chown -R $USER /usr/local
from lynda.com: This recursively changes the owner of the user local folder to the current logged in user so you can run the node package manager or npm and also Ruby gems
enhanced version of grep
you don't need -rn; it automatically lists line numbers and searches recursively. e.g.: ack ".classname" *
e.g., ifconfig | ack "10\." shows only segments of the return value containing '10.' with those instances highlighted
install here
sips -Z 1600 *.jpg OS X only; scale jpg(s) to 1600 longest dimension. more
site cross platform
svg image optimizer on github
svgo image.svg or svgo -f directory-of-svgs/
Ctrl-X then Ctrl-E (you may set this editor to vim in .bash_profile by adding export EDITOR="vim")
to create a private and public RSA key: ssh-keygen -t rsa (for more details, and for use with github, see here)
to set it up on a remote server (from here):
- secure copy (
scp) your public key to your remote server:
localuser@Local:~$ scp ~/.ssh/id_rsa.pub remoteuser@Remoteserver:localuser.pub.tmp
-
secure shell (
ssh) into the remote server, copy the key into its directory, and delete the original:localuser@Local:~$ ssh remoteuser@Remoteserver
mkdir .ssh # (if this directory doesn't already exist)
chmod 700 .ssh
cat localuser.pub.tmp >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
rm localuser.pub.tmp
echo umask 0012 >> ~/.bash_profile sets new files as: -rw-rw-r--
"a fast, versatile, remote (and local) file-copying tool"
from here (for OS-X):
rsync can be used to make a bootable clone. In addition to basic file copying, rsync also offers the ability to synchronize the source and target volumes — it can copy only the items that have changed, thus subsequent clones, or backups, are much faster:
cd /Volumes/Macintosh\ HD
sudo rsync -xrlptgoEv --progress --delete / /Volumes/DroboTres
That will backup your entire main drive, deleting anything from the target that is not on the source drive (synchronizing, that is). Rsync also preserves resource forks (that's what the "E" argument is for) and will give you a bootable backup
can also sync directories: rsync -av --delete /Volumes/mac2/client-projects/ /Volumes/DroboDos/client-projects/
more smart copy options for OS X
recursively walks file structure. -r/-R not needed (or allowed).
Syntax:
find . -name '*.js'
for files modified on a certain date:
find /path/to/dir -newermt "yyyy-mm-dd"
find . -mtime +60 : modified more than 60 days ago
find . –mtime -2 : find files modified in the last 2 days
find . -name "*.pdf" -exec rm -f {} \; CAREFUL!!! first do:
find . -name "*.pdf" -exec ls -l {} \; to check there are no inadvertant files being deleted
sudo find . -type f -exec chmod 644 {} +
sudo find . -type d -exec chmod 755 {} +
find . -newermt "2015-08-01" -type f -name "backup-*" -exec cp {} ./2015-08-backups/ \;
syntax is:
zip -r newzipname.zip directory_name/
You can automatically unzip a file at another path into your current directory by supplying the .zip files's path:
cd ~/.vim
unzip /path/to/vimpress_x.x.x.zip
to keep certain files out of a zip archive:
zip -r archive.zip * -x *.git*
gzip -d test.sql.gz
to uncompress tar files:
tar xvzf package.tar.gz (or tar xvjf package.tar.bz2)
or
tar -xzvf latest.tar.gz
^ matches beginning of line (unless it's first in square brackets; then it negates the expression)
* matches zero or more characters (when used in a search term). When following a character, it indicates 0 or more of that character. E.g., .* specifies zero or more instances of any character
? matches end of line
. matches any single character
[...] matches any character/range in the square brackets
- indicates range for chars in square brackets, or the dash char itself if it
grep "keywords" file(s)
case sensitive search
example grep -r "bp_head" *
example cat list1.txt list2.txt | grep p | sort
-i (make case insensitive)
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines
-h - if you search more than one file at a time, the results contain the name of the file from which the string was found. This option
turns off that feature, giving you only the lines without the file name.
see also ack
change spaces to dashes in file names in directory:
for i in *; do mv "$i" "`echo $i| tr ' ' '-'`"; done
change uppercase to lowercase in file names in directory:
for i in *; do mv "$i" "`echo $i| tr [A-Z] [a-z]`"; done
(though with both of the above you can use rename rather that mv if the OS/distribution supports it — see next)
cp -v * directoryname 1>../success.txt 2>../errors.txt note: the numerals 1 and 2 correspond to the Standard Output, and the Standard Error, respectively. ... &>output.txt combines all output.
from skorks
Ctrl + a– go to the start of the command lineCtrl + e– go to the end of the command lineCtrl - c- cancels (as always) the whole lineCtrl + k– delete from cursor to the end of the command lineCtrl + u– delete from cursor to the start of the command lineCtrl + w– delete from cursor to start of word (i.e. delete backwards one word)Ctrl + y– paste word or twoext that was cut using one of the deletion shortcuts (such as the one above) after the cursorCtrl + xx– move between start of command line and current cursor position (and back again)Ctrl + f– move forward one characterCtrl + b– move backward one characterCtrl + d– delete character under the cursorCtrl + h– delete character before the cursorCtrl + t– swap character under cursor with the previous one
following not OS-X by default (IN OS-X go to iTerm -> Profiles -> Keys -> and choose "Option key acts as +Esc" to map the option [alt] key to work)
Alt + b– move backward one word (or go to start of word the cursor is currently on)Alt + f– move forward one word (or go to end of word the cursor is currently on)Alt + d– delete to end of word starting at cursor (whole word if cursor is at the beginning of word)Alt + c– capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)Alt + u– make uppercase from cursor to end of wordAlt + l– make lowercase from cursor to end of wordAlt + t– swap current word with previousESC - bmove one word back (must release ESC key each time)ESC - fmove one word forward (must release ESC key each time)
In bash terminals other than iTerm, for the first 3 most commonly used command in the last above section, add this to your ~/.inputrc (from here):
# `Alt-b` to nav back a word
"\xe2\x88\xab":"\eb"
# `Alt-d` to delete back a word
"\xe2\x88\x82":"\ed"
# `Alt-f` to nav forward a word
"\xc6\x92":"\ef"
remove spaces from filenames in current directory
rename -n 's/[\s]/''/g' *
change capitals to lowercase in filenames in current directory
rename 'y/A-Z/a-z/' *
for f in *; do mv "$f" "$f.jpg"; done (adds '.jpg' to each file in directory)
displays past commands, numbered
to redo a specific numbered command: !468
to delete a specific numbered command: history -d 468
CTRL-R to search history by keyword(s)
strength: character by character file and string manipulation
Stream editing; search and replace in file while remaining in the shell. The result is sent to standard output. Results can be saved to a file using output redirection. The editor does not modify the original input.
sed 's/searchterm/replaceterm/g' file.txt (use regex)
to delete leading whitespace/tabs: sed 's/^[ \t]*//' <filename>
e.g., to replace the database password "root" in a wp-config.php file to "differentpw":
sed -i.bak "s/PASSWORD', 'root/PASSWORD', 'differentpw/g" wp-config.php
- use double quotes when there are single quotes in the search and replace terms
- only escape regex characters
- the
-i.bakflag creates a backup of the original file - for the above, could also do:
perl -pi -e "s/PASSWORD', 'root/PASSWORD', 'differentpw/g;" wp-config.php(from here; note semicolon at end of quoted statement)
combine with tr: echo "Safety & Human Factors" | tr A-Z a-z | sed 's/&/and/g' | tr [:blank:] -; echoes safety-and-human-factors
find, search and replace like sed but you enter the program and work with the file on the command line; e.g.:
ed test.txt (enters ed cli mode; then...)
1,$p to view the file
/searchterm or regex/ to search
/ to repeat previous
S&R like vim, sed but using the notation above; e.g.:
1,$s/^/>>/g to add '>>' to beginning of every line; 1,$s/\.$/!/ to change the next period at the end of a sentence to an exclamation point
q to quit
strength: field and record level file manipulation
from devshed: Set the FS to a comma, or whatever else you want to use, then deal with all the quoting headaches as patiently as possible.
$ cat test.data
fred,george,sarah
marcy,wallace,larry
$ awk '{print $3,$2,$1}' FS="," < test.data
sarah george fred
larry wallace marcy
see separate notes file on AWK
ln -s /websites/cleanairforelpaso-dev.tti.tamu.edu/wp-content/themes/cleanairforep ./ep NOTE the ./
now just type cd ep (use pwd -P to see the full phsyical path)
to open a file with its default application
gnome-open file2open.pdf (Ubuntu)
xdg-open file2open.pdf (other Linux desktop environments; this and the previous one from here)
open file2open.pdf (OS-X)
(in Ubuntu can add alias open='gnome-open' to .bashrc to type the shorter OS X command)
takes output of a command and passes it as argument of another command
cat url-list.txt | xargs wget –c
find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
ls *.jpg | xargs -n1 -i cp {} /external-harddrive/directory
shell options
shopt -s cdspell to start spellcheck on cd commands
disk usage see here
du -hcs . shows size of current directory
df -h disk space free, see link above
displays arguments to the standard output
lists the contents of a directory (or folder).
-F distinguish folders
-a show invisibles
-t show by date, newest on top
-l display long info
-r reverse order; e.g. ls -ltr shows by date newest on bottom
-ls adds a column showing the 512-byte blocks file uses on the disk
-lh shows file/dir sizes in B, L, M, etc.
(when installed): shows dir and subdir contents in tree form
makes a new directory.
switches to a different directory.
cdby itself takes you to the home dircd -to return to the previous directory
like cd but pushes the current directory into a "stack", to return to with...
return to the directory/directories "stack" created with pushd
creates a new, empty file (among other uses).
views files (among other uses, like concatenation).
removes files or directories. Use with care!
rm -rf directoryname
seriously, USE. WITH. CARE.
secure remove
copy files/directories
ex.: cp -pRv source/ target (note trailing slash; copies all files/directories within, not the containing directory)
-ppreserves timestamps, flags, modes, and ownerships of files-Rcopies the entire subtree-vmakescpoutput the name of each file that is copied
more for OS X here
print working directory (shows where you are)
pwd -P shows the full, physical directory with symbolic links resolved ( see ln )
(use with the pipe char [ | ], which "pipes" the output of one command to another), lets you page through the output using the space bar
opens a file so you can page through with space bar; them use q to exit
to force line wrapping while in the interface, -S
first 10 lines
last 10 lines
takes the output and writes/overwrites to the file named afterward
same as above, but appends the new output to an existing file
lets you tell a command that it should read its content from a file, rather than from the command line. e.g.: more < data.txt will just let you page through the file, rather than typing cat data.txt | more
word count
--help flags for help
manual
translate; e.g., changing case:
echo 'UPPERCASE TEXT' | tr A-Z a-z shows in standard output
echo 'UPPERCASE TEXT' | tr A-Z a-z | pbcopy sends output to OSX clipboard
echo "Research Support" | tr A-Z a-z | tr [:blank:] - | pbcopy converts string to lowercase and converts spaces to dashes
combine with sed: echo "Safety & Human Factors" | tr A-Z a-z | sed 's/&/and/g' | tr [:blank:] -; echoes safety-and-human-factors
show all the uppercase characters in a file as lowercase: tr A-Z a-z < test.txt
change the uppercase characters in a file to lowercase characters in a new file:
tr A-Z a-z < test.txt > test2.txt
to download from a URL to the current directory
curl -O http://www.theweathernetwork.com//common/images/web/wicons/w.gif
like curl
to clone a site: wget -mk domain.com
from here—
The “-m” puts wget into mirror mode, which follows links on webpages and downloads subsequent pages on the site. The “-k” ensures that wget re-writes links so that they link to your local copy of the website instead of the original destination (the remote web server).
redo previous command as root user