ls # regular
ls -l # detailed
cd [directory] # change to directory
cd.. # move up one directory
cd # change to home directory
--on a file--
reading a file (r) : seeing it's contents
writing a file (w) : changing it's contents
executing a file (x) : run it like a program
--on a directory--
reading a dir (r) : see the list of files
writing a dir (w) : add/remove files
Setting Permissions
There are two ways to set permissions — using words or using numbers.
Using Numbers
chmod 0777 filename.txt # rwxrwxrwx permissions on filename.txt
How to get the numbers:
r = 4
w = 2
x = 1
Using Words
chmod go+rx filename.txt # for [group][other][add permissions][read][execute]
mkdir [dirname]
mkdir -p [dirname] # make intermediate directories as necessary
Open files in the system include disk files, pipes, network sockets and devices opened by all processes.
lsof
rm [file]
rm -r [directory] # delete a directory recursively
rmdir [directory]
Runs a command recursively through all the files in a directory
[command] -r [params]
-f
sudo
su
[command] > filename.txt
filename.txt > [command]
Uses the output of one command as the input of another.
[command] | [command]
-p flag puts a
while (<>) {
# your code goes here
} continue {
print or die "-p destination: $!
";
}
around the entire one-liner.
-i flag makes any edits to the original files.
-e says that the one liner is following this flag.
perl -p -i -e 'insert one liner here' optionalargs.txt
#one-liner to find and replace across a bunch of files in a folder:
perl -pi -e 's/findthisstring/replacewiththis/g' *
df -l
osascript [scriptname]
osascript -e 'tell application "iTunes" activate' # one-liner
open [filename/folder name]
open .
cp [source file] [destination folder]
cp index.html ~/Desktop
# to copy a directory
cp -R [start dir] [end dir] # use the -r flag to copy recursively
mv [source file] [destination folder]
mv index.html ~/Desktop
mv oldname.txt newname.txt
cat filename.txt
nl filename.txt # view with line numbers
less filename.txt # uses the "less" pager. This is much more useful if you have a long file. See here: http://en.wikipedia.org/wiki/Less_(Unix) for key commands. Basics are:
q - quit
space/up/down - navigate
/searchpattern - search (regex)
useful flags:
-N show line numbers
-M show more detailed info while using
View command history with:
history
If you want to run a particular command in your history type:
![command #]
To search through your history, use Ctrl-R and then start typing the command.
You can also grep your history using
history | grep 'searchterm'
wc -l # counts the number of lines in something
Every user has their own crontab. Open your crontab for editing with
crontab -e
Add jobs on individual lines on the file. The format for a job is:
[minutes] [hours] [day of month (1-31)] [months] [weekdays (0-6)] command
the times are all specified in numbers, and they all start at zero. Weekdays can also be specified as Mon, Tue etc For example, here's a cron that says "hello" every minute:
0-59 * * * * osascript -e 'say "hello"'
Another way to do the same thing:
* * * * * osascript -e 'say "hello"'
Other examples:
15 15 * * * osascript -e 'say "hello"' # say hello at 3:15 pm every day.
15 3 * * * osascript -e 'say "hello"' # say hello at 3:15 am every day.
0 17 * * 0 osascript -e 'say "hello"' # say hello at 5pm every sunday.
0 17 * * Fri-Sun osascript -e 'say "hello"' # say hello at 5pm every friday, saturday and sunday.
*/5 * * * * /home/user/test.pl # run test.pl every 5 minutes.
crontab -l
MacPorts provides a very easy way to install software on your mac. Here's a list of available ports.
To search for a particular piece of software:
port search wget
To install:
sudo port install wget
To uninstall:
sudo port uninstall wget
To upgrade:
sudo port upgrade wget
See currently installed ports:
port installed
See which of your installed ports are outdated:
port outdated
port outdated [some port name]
Don't install a port, but make a package out of it that you can just double-click and install:
sudo port package [port name]
See where a package is installed:
port location [port name]
curl -d "name=aditya&value=something" http://www.google.com/google.cgi
You need to encode all characters properly, so spaces need to become %20. If you want curl to do the encoding for you, use:
curl --data-urlencode "name=aditya is here&value=something" http://www.google.com/google.cgi
Download a file:
wget http://something.zip
Switches:
-U [browser name] # sets useragent
-w [#] # waits specified number in seconds
--limit-rate=[#] # limits download speed to specified number in KBps
-c # will resume interrupted download (good for flaky connections)
-r # download recursively
-H # follow links that point away from the source site
-A.[ext] # only follows links that end with the specified ext
-nd # save everything in one directory
-l[number] # how many levels deep a recursive download should go.
-i filename # use filename.txt as input. filename.txt contains a buch of urls, go to all of them.
-k # after downloading, convert links in the document so it's suitable for local viewing
Download all PDFs on a page:
wget -r -l1 -A.pdf --no-parent http://url-to-webpage-with-pdfs/
To download multiple files:
curl "http://www.something.com/img[1-20].jpg" -O; grep -il found img*.jpg | xargs rm
The general format is:
scp [file to copy] [location to copy to]
scp aditya@somesite.com:somefolder/somefile.txt ~/Desktop
# Copy a local file to a remote location
scp ~/Desktop/someotherfile.txt aditya@somesite.com:some_folder
man [command name]
# to view all the man pages for a command that has more than one man page:
man -a [command name] # will show you man pages one at a time, quit out of a man age to view the next one.
# to do a search
man -k [command name]
# specify a section (like section 4 here)
man 4 [command name]
# There's also another command available for GNU software:
info [command name]
export PATH=$PATH:/your/new/dir # temporary, once you quit terminal, the $PATH is reset.
You can use stat or ls -l.
stat
stat [file name]
ls -l
ls -l [file name]
other ls flags
-i : shows iNode number
-a : show all files (including hidden files)
-l : show long listing
-d : show directory info as if it was a file (aka, don't show all the files in the directory)
This file contains a list of people who possibly can log in to this system.
Each line looks like this:
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
The last part of that line tells you what the default shell is that they'll start with.
Use either
clear
or just hit Ctrl-L.
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + R search through history (start typing to start searching)
Ctrl + C to interrupt
Ctrl + D to end standard input (STDIN)
Tab Auto-complete files and folder names
A hard link is a pointer in C++ terms. Even if the original file is deleted, you can still access it using a hard link. You can't do this with a symbolic link.
ln [old filename][new filename]
A symbolic link is a pointer-to-a-pointer in C++ terms. If the original file is deleted, the symbolic link doesn't point to anything.
ln -s [old filename][new filename]
Use the find command:
find [search path - absolute or relative]
The find command is pretty complex. See it's own entry for more details.
Or use the locate command if you don't know where the file is:
locate [filename]
* = anything (even nothing)
? = any some character (but has to be something)
[13] = either 1 or 3 (NOT 13)
{13,15} = either 13 or 15
{13..15} = either 13, 14 or 15
Simple alphabetical sort:
sort
To sort by the second column:
sort -k 2
This will use whitespace as the delimiter by default.
To use something else as the delimiter, use the -t flag with the delimiter after it. For example, here we set a colon as the delimiter:
sort -t: -k 2
Use this for conditional commands. For example, suppose we only want to sort if an ls succeeded. We would use:
ls && sort
Enclose your statement in parentheses if you want to run it in a subshell. The command will create a new subshell, the command will then run, and then the subshell will go away.
Use curly braces to group statements together. For example, this command will only pipe uptime to less:
ls;date;uptime | less
This command will pipe all three commands to less:
{ ls; date; uptime; } | less
Note the spaces after and before the brackets, as well as the semicolon before the closing brace. you absolutely need these. Otherwise you'll get an error.
Use backticks (``).
For example, this supplies the result of which ls as a command line argument to file:
file `which ls`
Note that using backticks is different from using a pipe. When you use backticks, you're sending a command-line argument. When you use a pipe, you're sending to STDIN (standard input).
Redirecting STDOUT:
ls -l > output.txt
This will overwrite the file output.txt. If you just want to append to it, use:
ls -l >> output_append.txt
Redirecting STDERR:
ls -l 2>error_out.txt
Here we have prefaced the > operator with the number of the file descriptor we wanted to redirect. Since standard error is on file descriptor 2, we used 2>.
Redirecting STDIN:
more < my_stdin.txt
diff [file1] [file2]
This creates a file test.tar using the contents of the current directory:
tar cvf test.tar *
This is known as creating a tarball.
Flags used:
c = create
v = verbosely
f = put output into file
Note that we don't put a '-' in front of the flags.
You can also compress it using gzip at the same time with the 'z' flag:
tar czvf test.tar.tgz *
tar tvf test.tar
aka unzip, aka uncompress.
tar xvf test.tar
gzip test.tar
You can also compress it using gzip while making a tarball if you use the 'z' flag while making the tarball:
tar czvf test.tar.tgz *
Grep does a regular expression search on whatever you give it. Egrep is called "extended grep" — it's grep with a few more options. In most cases it won't matter which you use, but in some cases, egrep will be the wiser choice. As a safety measure, always use egrep.
Here's an example that searches for the term 'perl' in all the files in the directory:
grep 'perl' *
Grep uses regular expressions. See the regular expressions page for special symbols.
Grep Flags
-l : print out file name
-v : reverse the grep (show stuff that doesn't match the grep)
-c : print out # of lines matched.
-o : show what matched
-i : ignore case.
To Make a New Alias
alias d = 'ls'
Now d is set to ls. So if you enter d in your shell and hit return, it will execute ls.
To Delete An Alias
unalias d
Show Currently Set Aliases
alias
Aliases are temporary. They are deleted when you quit your session. If you want them to live on, put them in your .bashrc file.
echo 'some text' >> somefile.txt
See Current Jobs:
jobs
Manage Jobs:
Ctrl-Z = pause job (you'll get taken straight back to the command line)
fg = return to job
fg %1 = return to job number 1
kill %1 = stop job # 1
Put Jobs In The Background:
put a & at the end of the command to have it run in the background. It will still continue to output to the screen though, so if you don't want that, redirect STDOUT and STDERR like this:
[somejob] >stoutanderr.txt 2>&1 &
A job is not the same thing as a process. A job is a program running inside a process.
If you have a job running and you exit the shell, that job is killed.
Use the script command. Start it by typing
script filename.txt
From then on, until you stop it, all your terminal activity will be placed in filename.txt. Stop it by hitting Ctrl + D.
who
w # more technical, shows who is logged on and what they are doing.
finger [user]
echo 'here's my body.' | mutt -s "here's my subject" -a .someattachment.zip someone@example.com
If you're adding attachments, use Mutt instead of Mail. Here's how mail is used:
echo 'message body' | mail someone@example.com
mail someone@example.com < message_body.txt
mail -s "message subject" someone@example.com < message_body.txt
This will compare the files in two directories and tell you if one or the other directory is missing files or if some files are different. It also does the same thing recursively through all folders in those folders.
diff -ry dir1 dir2 | grep '[<>]|Only|diff'
What this line does
-r : recursive flag
-y : side-by-side comparison in case of differences
dir1, dir2 : the directories to compare
It's all passed to a grep that prints lines either with arrows (< or >) indicating differences or any line that tells if a file is missing in a directory ('Only') or statements telling which files are being compared ('diff').
ldd is a utility in linux that allows you to see which shared libraries are used by a program.
Here's how to use it:
ldd [programname]
The mac os has something similar called otool:
otool -L [programname]
Use the nm command:
nm myfile.o
The properties of a file (what you see when you run an "ls -l" on a file) are stored in a file's Inode, a special block of data in the file system that also contains the length of the file and where on the disk it's stored. Each file has a unique Inode number, and the system uses the number of the file's Inode; the actual name is just for our benefit
You can see a file's inode number with:
ls -i
touch filename.txt
Unix's calculator can handle some big numbers. Try typing in 2^1000.
bc
GNU stands for "GNU's not Unix".
GNU is a Unix style operating system composed entirely of free software. It was created by Richard Stallman 1983-84 because around this time there was a move to copyright various flavors of Unix and make the code proprietary and unavailable. Software development began on January 5, 1984, when Stallman quit his job at the Massachusetts Institute of Technology's Artificial Intelligence laboratory so that they could not claim ownership or interfere with distributing GNU as free software.
The goal was to bring a wholly free software operating system into existence. Stallman wanted computer users to be "free", as most were in the 1960s and 1970s — free to study the source code of the software they use, free to share the software with other people, free to modify the behaviour of the software, and free to publish their modified versions of the software. This philosophy was later published as the GNU Manifesto in March 1985.
Stallman basically wrote a lot of Unix utilities etc from scratch, like the gcc compiler, and developed new ones, like emacs.
$HOME
The home directory of the current user.
$PATH
A colon-separated list of directories to search for commands like "ls", "less" and "perl".
See the value of an environment variable
echo $PATH
First, we need to generate a public/private key pair (MUCH easier than it sounds) on your local machine. Enter this command at your Terminal:
ssh-keygen -t rsa
Then:
1. Hit Enter to accept the default location to save the key.
2. We don't want a passphrase. Hit Enter twice.
scp the public key file (ending in .pub) into the account you want to ssh and scp without a password form now on. You need to put the public key into a file called authorized_keys, which should be in a folder called .ssh, which should be in your home folder.
For example:
scp /local_folder/.ssh/id_rsa.pub aditya@somesite.com:~/.ssh/authorized_keys
Try ssh'ing into that account now — it shouldn't ask you for your password.
See this URL if you want more explanation:
http://www.linuxjournal.com/article/8600
Use this to find files.
The syntax for find is:
find [path] [options] [tests] [actions]
For example, this command says "start searching at '/' for files named 'test' and print out the name of the file":
find / -name test -print
Options
-follow
Follow symbolic links.
-maxdepths N
Search at most N levels of the directory when searching.
Tests
-atime N
The file was last accessed N days ago.
-mtime N
The file was last modified N days ago.
-name pattern
The name of the file matches the pattern provided. Make sure your pattern is in quotes. If it isn't, it will be evaluated by the shell instead of being passed to find.
-newer otherfile
The file is newer than the file otherfile.
-type C
The file is of type C. Common values for C are "d" for directory and "f" for regular file.
-user username
The file is owned by the user with the given name.
You can also use boolean operators in tests.
! = -not = not.
-a = -and = and.
-o = -or = or.
If you want to use parens for precedence, you have to backslash them:
(-newer X -o name "_*" )
Actions
-print
Print out the name of the file.
-ls
Run ls -dils on the current file.
-exec command
This is one of the most common actions. It is a little more complex. Effectively, -exec commands are executing an embedded command. They take subsequent parameters on the command line as part of their parameters until they hit an \;. For example, here we run an "ls -l" on all the files we find:
find . -type f -exec ls -l {} \;
{} is a special parameter that is replaced with the full path to the current file.
-ok command
This is just like -exec, except it prompts the user for confirmation each time before carrying out the command on a file.
Directories
/ Root
~ Home directory
/bin System programs (binaries)
/etc System config files
/lib System libraries
/dev Contains files that represent physical devices and provide the interface to those devices.
Device Files
/dev/console
This device represents the system console. Error messages and diagnostics are often sent to this device.
/dev/tty
The special file /dev/tty is an alias (logical device) for the controlling terminal (keyboard and screen, or window) of a process, if it has one. (For instance, processes and scripts run automatically by the system won't have a controlling terminal, and therefore won't be able to open /dev/tty).
/dev/null
The /dev/null file is the null device. All output written to this device is discarded. An immediate end of file is returned when the device is read, and it can be used as a source of empty files by using the cp command.
These two commands are actually for Mac OS X.
Copy To Clipboard
Use pbcopy. For example:
echo "hello" | pbcopy
Paste From Clipboard
Use pbpaste. For example:
pbpaste
The umask is a system variable. It's a mask for file permissions that is used when a file is created. It looks like permissions — for example, the umask on my system is 0022. But it works in the exact opposite way.
For example, 0022 as permissions means that:
- User can't do anything.
- Group can write.
- Others can write.
0022 as a umask means that:
- User can do anything.
- Groups can't write.
- Others can't write.
See your umask
See the umask on your system using:
umask
Change your umask
To change your umask to 022, type this in the shell:
umask 022
Parallelism
I have a computer with 4 processors. I have one program running on each processor. Are those programs all running at the same time? YES. They are running in parallel.
Concurrency
A processor can just do one thing at a time. Can you run 4 programs at the same time on only one processor? Sort of. A processor has some registers and that's where it holds the state of a running program. How can you run 4 programs when you only have one set of registers? Modern OSes have a "time-sharing" model. It will run program A for a short period of time, and then stop it and run program B for a short period of time,etc etc.
This applies to Mac OS X. For example, to open a text file with Coda, type:
open -a Coda somefile.txt
mdfind [searchterm];
mdfind -onlyin [location] [searchterm]; # To search only one location (recursively)
Step 1: Create a man page
Man pages have their own format. Here's an example of a man page:
.\" Copyright (c) 2005, O'Reilly Media, Inc.
.\"
.Dd April 15, 2002
.Dt HELLOW 1
.Os Mac OS X
.Sh NAME
.Nm hellow
.Nd Greeting generator
.Sh DESCRIPTION
This command prints a friendly greeting.
The extension for a man page is the section number that it will be put in. So if you're putting a man page in man1, the extension would be '.1'.
'.Dd', '.Dt' etc are font conventions. Here's a page with more:
http://tldp.org/HOWTO/Man-Page/q8.html
Step 2: Install your man page
Now that you have a man page, you need to put it in one of the directories that the man command searches. To see all the directories, type:
echo $MANPATH
Now choose a directory that you have write permissions to and add your man page. Better yet, if you want to keep your man pages separate from the other man pages ( generally a good idea ), create a new directory, add it permanently to the manpath by adding it to .bash_profile, and then put your man pages in there.
You're good to go. You should be able to run:
man [filename of your man page without the extension]
and have your man page show up.
Step 3: Rebuild the whatis index
(Optional) If you want to add your man page to the whatis database, you need to rebuild the whatis index. Use makewhatis to do this:
makewhatis [folder where you added your new man page]
# For example, if you added the man page to /usr/local/man:
makewhatis /usr/local/man
This shows you how to create an installer package for a small script.
Step 1: Write and compile your script.
You will need to create a new folder containing only the items to be installed. For example, if you have a script 'hellow' that you want to install, create a new folder 'stage' that contains only 'hellow'.
Step 2: Open PackageMaker.
Step 3: Open the 'Installer Interface' tab.
Add in a Title and Description. If you want to change how your installer looks / the license you're using etc, click on 'Show Installer Interface Editor'.
Step 4: Open the 'Contents' tab.
For 'Root', choose that folder that contains all the files to be installed. In Step 1, we had made a folder 'stage'.
Step 5: Open the 'Configuration' tab.
For 'Default Location', select where the script will be installed. Then choose the correct Authentication if you need to. For most cases, 'Administrator' will be the correct choice.
Step 6: The 'Scripts' tab (Optional).
If your app has any extra files like READMEs or something, add those here.
Step 7: Open the 'Package Version' tab.
Enter some appropriate info for Identifier, Get Info String, and Version.
Step 8: Save.
It's a good idea to save what you've done so far in case you need to modify these install settings later.
Step 9: Build.
Goto Project -> Build or hit Apple-B.
Step 10: Test package.
If anyone else is going to use this, you will want to test your package. It works like any other package: double-click to start and then follow the directions for installation.
DONE!
Use sc_usage:
sudo sc_usage [program name]
strings [file name]
You need Quicksilver for this.
osascript -e 'tell application "Quicksilver" to show large type "708-555-1212"'
very interesting.
top
In bash:
export DYLD_PRINT_LIBRARIES=1
sudo !!
Usually the patch will contain the name of the file the patch is for, so you can just do:
patch < patchname.diff
If you want to apply it to a particular file, you can use:
patch filename.txt < patchname.diff
Use hd to view a hex dump:
hd [filename]
Including things like architecture, 32-bit vs 64-bit, etc.
getconf -a
Use pipe viewer (pv). It will show you a progress bar.
pv cp test.txt ~/Desktop/test.txt
pv gzip -c access.log > access.log.gz