Skip to content

Instantly share code, notes, and snippets.

@beaucharman
Created June 30, 2014 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaucharman/8d59bc6a7cc0c3df59fc to your computer and use it in GitHub Desktop.
Save beaucharman/8d59bc6a7cc0c3df59fc to your computer and use it in GitHub Desktop.

Terminal Notes and Snippets

Configuration

Show full path in finder

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

Restart Finder

killall Finder

Alter the bash profile

sudo nano ~/.bash_profile

Alter the terminal home marker [⌘ ~/]

export PS1='\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33mgit branch 2> /dev/null | grep -e ^* | sed -E s/^\\*\ (.+)$/(\\\1)\ /\e[m'

Awesome Aliases

alias showhidden='defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder'

alias hideshown='defaults write com.apple.finder AppleShowAllFiles FALSE; killall Finder'


Output Commands

echo command

echo {{command/item/string}}

Suppress new line

-n

See the manual for a particular command - q to exit

man {command}


Navigation

Move to the start and end of a line

CTRL A / CTRL E

Access command history

up down keys

Use tilde to go to the root of the system

~

Push the current directory to the directory stack

pushd {path/string}

Pop the last entry off the directory stack and step back one entry

popd

Show current directory stack

dirs

echo the system root

echo ~

Tab to print out the contents of a directory

/Users/{tab}

Up one directory

../

Current directory

./ or .

Clear the terminal

clear

Run last command (Handy to use with sudo)

!!

List the files in the current working directory

ls

List the files with details

ls -l

List all of the files, even hidden files

ls -a

List the files, sorted by time

ls -t

print working directory

pwd

Set working directory

cd

Escape spaces in file names

ls file\ name

open directory or files

open {directory_or_filename}

open files in a particular application

open -a {application} {filename}

Reveal a file in finder

open -R {filename}

Open a URL

open {url}

Create a file

touch {filename}

Create a file and edit

{editor: nano} {filename}

Create a directory

mkdir {directory_name}

Create a file within a directory

touch {directory}/{filename}

Copy a file

cp {file} {newfile.bak}

Copy a directory

cp -r {directory} {newdirectory}

Move a file

mv {directory/filename} {directory/}

Move a file and rename

mv {directory/filename} {directory/newfile.ext}

Rename a file

mv {filename} {newfile.ext}

Move multiple files

mv {word_to_match}* {directory}/

Delete files

rm {filename}

Delete directory

rm -r {directory}

Echo out the contents of a file

cat {filename}

Symbolic link - soft link, will break

ln -s {source-filename} {shortcut-filename}

Symbolic link - hard link, won't break

ln {source-filename} {shortcut-filename}


Finding Files

Find files by Type - File (Case Sensitive)

find {directory} -type f

Find files by Name (Case Sensitive)

find {directory} -name "{filename}"

Find files which filename contains… (Case Sensitive)

find {directory} - name "{filename_part}"

Find files with case insensitive search

find {directory} -iname "{filename}"

Find files by file size (512 byte blocks)

find {directory} -size +{blocks}

Find by modified time (less than 1 day)

find {directory} -mtime -1

Find by accessed time (more than 1 day)

find {directory} -atime +1

Find by created time (less than one day)

find {directory} -ctime -1

with combined conditions (or, and)

-or -and

Exclude directories from search

-print -or -iname "{directory}" -prune

search content within files, return the respective lines

grep "{string}" {file}

search content within files, case insensitive

grep -i "{string}" {file}

search content within files, return the respective filename

grep -il "{string}" {file}

search content all files, return the respective filename

grep -il "{string}" *

search content recursively, return the respective filename

grep -ilr "{string}" *

find by file type, name, and then by contents

find {directory} -name "*{filename_part}" -exec grep -il "{string}" {} ;

Show the manual for the given command

man {command}

Show the location of the binary for the given executable

which {executable}

Display the file type details of the given file

file {file}

Show the base name for the given argument

basename {File / directory}

Tidy the Terminal window (Maintains history)

clear

Clean out the Terminal window

CMD + K


Combinations

Use back ticks to combine commands

file which vim``


Archiving

gzip a file

gzip {file}

Un-gzip a file

gzip -d {file.gz}

Zip files and folders

zip {archive.zip} {folders/files}

Un-zip an archive

unzip {archive.zip}

Tar Archive [c:create;v:show output; z:gzip; f:name it]

tar -cvzf {archive.tar.gz} {files/folders}

Un-tar and archive

tar -cvzf {archive.tar.gz}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment