Command line notes


pushd and popd

pushd pushes a dir to a stack.

$ cd Downloads
$ mkdir sandbox
$ cd sandbox
$ pushd ~/Documents
$ ~/Documents ~/Downloads/sandbox ~/Downloads ~

This will add ~/Documents to a stack. By using popd, it will move back to the previous
dir.

$ popd
$ ~/Downloads/sandbox ~/Downloads ~

which, file and basename

which: locate a program file in the user's path

file: determine file type

basename, dirname -- return filename or directory portion of pathname

$ file `which vim`
$ /usr/bin/vim: Mach-O 64-bit executable x86_64

$ basename `pwd`
$ sandbox
$ basename /Users/admin
$ admin
$ basename ~/sandbox/document.pdf
$ document.pdf

cmd+k will clear all.

clear will move to the new line and hide all the previous.

Working with Archives

changing mydoc.txt to mydoc.txt.gz. This will change the original to gz.

$ gzip mydoc.txt 

cat mydoc.txt.gz does not work. Use gzip -d. This will change gz file to the original file.

$ gzip -d mydoc.txt.gz

If you want to zip files, use zip. zip is good for smaller archive.

$ zip myZip.zip document.pdf file.txt mydoc.txt

This will create a myZip.zip to the same dir.

To unzip

$ unzip myZip.zip

Use tar archive for bigger archive. You need to use tar command as follows (syntax of tar command):
tar -zcvf archive-name.tar.gz directory-name
Where,

-z: Compress archive using gzip program
-c: Create archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name

For example, you have directory called /home/jerry/prog and you would like to compress this directory then you can type tar command as follows:

$ tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog

To unarchive

$ tar -zxvf prog-1-jan-2005.tar.gz

-x: Extract files

If you wish to extract files in particular directory, for example in /tmp then you need to use following command:

$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp

-C directory

In c and r mode, this changes the directory before adding the
following files. In x mode, change directories after opening the
archive but before extracting entries from the archive.

How do I Compress a Whole Linux or UNIX Directory?

Inputs, outputs and file descriptors

Standard file descriptors

file descitor 0: Standard Input stdin

file descitor 1: Standard Output stdout

file descitor 2: Standard Error stderr

// standard out
$ ls -l // this is an example of standard out. Output to the terminal

// example of standard err
$ ls NoFolder 
$ ls: NoFolder: No such file or directory

Redirecting outputs

// redirect outputs to listing.txt by creating listing.txt file
$ ls -l > listing.txt
// check the content
$ cat listing.

// redirect the standard out of the root to listing.txt by adding
// >> will append the content to the file in stead of over-writing it.
// > will over-write the file.
$ ls -l / >> listing.txt

Redirect output to another command

$ ls -l | grep Document and it is piped to grep Document. This means it treats the output of ls -l as input of grep Document. 
// ls -l printing outputs, in this case it will be on the terminal
// grep will search Document
$ ls -l | grep Document > listing.txt 
// This will overwrite outputs to listing.txt
// output is
$ drwx------+ 77 teacher  staff     2618 Aug 15 17:41 Documents 

$ ls NoDir > dirs.txt
// this will not write on dirs.txt since there is no standard outputs
// in order to output standard error use 2>
$ ls NoDir 2> dirs.txt
//ls: NoDir: No such file or directory will on dirs.txt file
// 2 is file descriptor number 2 which is standard error.

$ ls NoDir Documents
// this will output 
$ ls: NoDir: No such file or directory
$ Documents:
$ 2013travel
$ //... more after this

// The first part is a standard error and the second part is a standard output.

$ ls NoDir Documents &> dirs.txt
// this will overwrite dirs.txt with the outputs.

$ ls NoDir Documents > dirs.txt 2>&1
// this is the long version of the previous one.
// redirect the list of standard out of NoDir and home dir to dirs.txt and standard error to standard out.

Redirecting outputs with cat command

$ cat > file.txt
// this will wait your inputs.
$ this is the first line of my file.
$ second line.

Bye!
^C // ctrl+C
// this will create file.txt file with the above content.

[HowTo: Use grep Command In Linux / UNIX – Examples](http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/) 

Sorting lines of text

Create a file file.txt with a list of app.

$   cat > file.txt
finder
terminal
textedit
screenflow
chrome
alfre
vim
ominfocus
soulver
acorn
levelator
textexpander
tweetbot
^C

cat file.txt // to see the content
$ sort file.txt
// this will output the standard out with sorted list on the terminal.

$ cat file.txt | sort > file_sorted.txt
// this will output to file_sorted.txt with the sorted list.

Removing duplicates

$ vim file.txt // add a couple of the same entries (duplicates)
// use yy to copy the line and p to paste in vim.

$ uniq file.txt
// this does not delete the duplicates. because uniq commands expect the input to be sorted.
$ sort file.txt | uniq
// this will sort and output unique items.

$ sort -u file.txt
// shortcut of the previous command.

-u: unique

Working with Mac cripboard

pbcopy: paste board copy

pbpaste: paste board paste

$ echo "This is a line of text from the terminal" | pbcopy
// copy to the crip board. Paste it to a file or sublime.

Copy a line of text from a text editor.

$ pbpaste // this will paste to the terminal.
$ pb paste > paste.txt
$ vim paste.txt