Delete files by filename with “find”

When you upload photos multiple times, the computer can start numbering the photos. IMG_A becomes IMG_A 1 and IMG_A 2 and IMG_A 3.

Using a wildcard * and specifying the ending space and number will help you select the files. For example:

Find all the files that end in "space"1.JPG

find -name '* 1.JPG'

* Have a look at the returned files to make sure you want to delete these

Find AND DELETE all the files that end in "space"1.JPG

find -name '* 1.JPG' -delete

gist

Set up your programming environment

*wip

Must have for your Mac

Get homebrew – no longer drag an icon to install

Homebrew – “the missing package manager for macOS” will make it so much easier to download, upgrade and maintain your packages. It will install packages super transparently in /usr/local/Cellar/* and symlink commands to /bin.

Must have for node.js

nvm

a version manager for node. You will likely use many different node versions if you are pulling down different production repos, nvm will help you install and switch the the correct versions.

node.js

use nvm to install the latest version of node.js

$ nvm install node

 

TODO rust, iOS, python, scala, screen sharing

Weather reports from your terminal with curl

What is curl?

curl is a program that is included in your Mac OSX distribution used for transferring data from or to a server. It uses many supported protocols (HTTP, HTTPS, FTP, SMTP and more).

Do you have curl?

Find the location of the curl program (executable) with $ which curl. This will return /usr/bin/curl if you’re on Mac.

How do I use curl?

A simple http GET request can be made like this:

$ curl google.com

How to deal with 301 redirect 

Notice that you get a 301 message that is address has been “moved”. You can add the flag -L, --location (choose either) to “follow” redirects

$ curl -L google.com

$ curl --location google.com

How do I use curl to get the weather?

There is a service called wttr that is set up to return weather reports when you curl their service. This time we will use http for added security.

For a full local weather report:

$ curl https://wttr.in

Should I use http?

For todays weather report:

$ curl https://wttr.in?0

You can also specify a city or ski resort.

For todays weather report for New York:

curl https://wttr.in/~NewYork?0

Check if its snowy in your favorite ski resort – Charlotte Pass, Snowy Monaro, NSW, Australia

* Notice that wttr is returning special terminal characters to change the color. You can save these to examine this in your text editor.

Congrats! You got the weather!

Note that you can curl any web service, even ones you build yourself.

Want to build your own weather service? We’ll do that next in the Part II of this series.

Bonus Section:

Can I curl my own websites?

Yes! If you followed our github pages tutorial you’ll find you can curl your own website AND YOUR STATIC FILES

$ curl -L theptrk.github.io  <- website

$ curl -L theptrk.github.io/api/message.txt <- static txt file

 

*Update – this post got onto the hackernews front page and since then the API for wttr.in has been down. **Update, the API is back up!

How to protect your files from deletion

I wrote about this before, but its important enough to write twice

How to revoke write permissions to a directory

How to revoke your own user permissions 

$ chmod u-w dirname

How to delete all permissions from anyone on your system 

$ chmod a-w dirname

removing write access on a file does not stop it from being removed,

you must remove write access to the directory to protect the file from deletion – Ice Bear

How to test this

To test this, simple change the permissions and try to $ rm any_file_name. You can even rmdir dirnameand it will protect the directory from deletion

Intro to OSX File Management Commands

Filenames can have spaces!

So when you deal with them either surround the filename with apostrophes like this: "my file name.txt" or escape the spaces my\ file\ name.txt. Otherwise, don’t create new file names with spaces or escape characters to keep your sanity.

Wildcards

* greedy wildcard

? single character wildcard

[] single character list of wildcards

{} pattern list of wildcards

Say you have files:

kale1.txt, kale1a.txt, kale2.txt, kale2a.txt, kale3a.kale

$ ls kale?.txt – would match kale1.txt, kale2.txt

$ ls kale[1-3].txt – would match kale1.txt, kale2.txt

$ ls kale[1-3].txt – would match kale1.txt, kale2.txt

$ ls kale{2, 2a}.txt – would match kale2.txt, kale2a.txt

$ ls *.txt – would match all the txt files

$mv *.kale Kale\ Files – would move all .kale files to another directory

reading files and finding within files – cat, less, grep

cat – reading files

you can cat multiple files and even cat multiple files and pipe them into another

$ cat file1.txt file2.txt > files3.txt

less – reading files in a scrollable way

less is actually more, more is actually less! Note how this two executables are the exact same size!

$ ls -l /usr/bin/{more,less}

How to make less more bearable

$ less -M filename – the M flag will show you where you are in the file and what percentage you are in.

During less:

Space – advance to the next page

b – advance back one page

v – starts vim

g – goes to beginning of file

G – goes to end of file

/word searches forward for a word

?word searches backwards for a word

grep – finding within files

grep uses a different, more sophisticated regular expressions system then the wildcards from above

$ grep secrets *.txt – searches for the text “secrets” in all the .txt files

moving and copying files with wildcards!

mkdir – making directories

mkdir takes wildcards! So if you wanted to make a different directory for the next three years, try:

$ mkdir taxes20{18,19,20}

cp – copying

cp takes wildcards! So if you wanted to move those new directories into a sub directory

$ touch taxes20{18,19,20}.txt # creates these files
$ cp taxes20{18,19,20}.txt ./taxes # copies those files into a new “taxes” directory

mv – moving

more wildcards!

$ mv -i taxes20{18,19,20}.txt ./my_taxes # moves those files into a new “my_taxes” directory interactively

rm – removing

$ rm -i .[^.]* removes all hidden files (., ..)

$ rm -i taxes*.txt removes all files with that wildcard

compressing

gzip for single files

$ ls -l to check the size of the file you are going to compress

Then use $ gzip -v to compress (verbose), $ gunzip -v to uncompress (verbose)

tar for directories, tar+gzip for the ultimate combo

$ du -s Kale\ Recipes to check the size in 512 byte blocks

$ tar -czvf my_kale_recipes.tgz "Kale Recipes" – to compress

$ tar -xvzf my_kale_recipes.tgz – to uncompress

where:

-c is to designate you are creating an archive

-x is to designate you are extracting an archive

-z is for using gzip,

-v is for verbose,

-f is to designate you are providing a filename

.tgz is the file extension for tarballs that are gzipped, sometimes you will see .tar.gz

Intro to your OS X Filesystem and Permissions

How to find home

note: as a user of the system you will be given a user directory under the Users directory

$ echo $HOME

How to go to the home directory choices:

$ cd $HOME$ cd ~$ cd, and

$ cd then drag your home directory from your Finder window

How to go to the root directory

note: when you see pathnames as such Users/patrick/Documents, this is an “absolute” path name starting at the root

$ cd /

How to print your working/current directory

note: every terminal window operates an independent working directory

$ pwd

How to list your files

$ ls

$ ls -F – this will append a forward slash (/) to directories, an asterisk (*) to executables or scripts, an (@) to symbolic links

$ ls -al – this will show the “long” version of “all” files in the directory

file permissions are shown on the first column in a super cryptic way like this (feel free to skip this portion: 

drwxr-xr-x

these are:

file type (1 char) – here “d” is directory, “-” would’ve meant file

owners file permission (3 char) – the owner has read, write, executable permissions on this directory

group file permission (3 char) – everyone in this users group has read and executable permissions

others file permission (3 char) – everyone on the computer in any group has read and execute permissions

How to see the disk usage of:

$ du Documents/Outline.doc (size of a file in 512-byte blocks)

$ du -s Documents (summary size of the directory in 512 byte blocks)

$ du -sh Documents (summary size of the directory in human readable format)

How to see the remaining free disk space

$ df -H (human readable disk free space using base 10 to calculate sizes)

How to change the permissions on files and directories

$ chmod "categories" "add/subtract" "permissions" "filename"

The categories permission options are:

u  owner, g  groups, o others, and

a – all permissions, owner, groups and others

The add/subtract options are:

+  adding, -  subtracting

The permission options are:

r  read, w  write, x  execute

Examples:

How to delete your user permission from removing the files within a directory (write access)

$ chmod u-w dirname

How to delete all permissions from removing the files within a directory (write access)

$ chmod a-w dirname

removing write access on a file does not stop it from being removed, you must remove write access to the directory to protect the file from deletion – Ice Bear

How to change the owner of a file or directory

$ chown some_name filename

 

Part II – Intro to OSX file management

Mac Unix Command Basics for Beginners

These are useful unix commands that are entered through a Terminal shell prompt. These commands are actually names of Unix programs that usually sit in a folder called /bin

where am i? pwd

How to check the current directory $ pwd

learn more about anything – man

How to bring up the manual page for a command: $ man ls or $ man pwd

go somewhere –  cd

How to go to user root:  simply $ cd

How to go to root: $ cd ~ or $ cd /

list things ls

How to list files: $ ls

How to list files, one per line: $ ls -a1 (thats a one)

How to list hidden files also: $ ls -a

How to list only hidden files: $ ls -d .?*

How to list files in some other directory: $ ls /path/to/directory

reading a file cat

How to read a file to terminal: $ cat filename

copy things cp

How to copy a file: $ cp target_file copied_file

How to copy a directory: $ cp -R original_directory copied_directory

How to copy a directory contents into the current directory

$ cp -R original_directory/ .

remove/delete things rm

*** WARNING this bypasses the Trash bin and things cannot be undeleted ***

How to delete a file: $ rm target_file

How to carefully delete a directory: $ rm -r target_directory

How to FORCE delete a directory: $ rm -rf target_directory

open chrome (open)

open /Applications/Google\ Chrome.app/

open /Applications/<Tab><Tab> (to get a list of available options)

abort the current command (Ctrl – C)

delete the everything to the left of the cursor (Ctrl – U)

Basics Plus: bonus section

open files with open

Do you ever download PDFs on Chrome and the browser insists on opening the pdf IN BROWSER?

copy the url path file://path/to/pdf and simply run open on it

$ open file://path/to/pdf

run multiple commands ;

How to clear the screen, print the current working directory, print the date, print the weather from “wttr.in”

clear;pwd;date;curl wttr.in?0

pipe stdout to stdin |

How to pipe the contents of a file (“my_file.txt”) to your system clipboard

cat my_file.txt | pbcopy

pipe stdout ot stdin |

How to pipe the result of ls to vim

ls -1 | vim -

multiline commands \

rm -rf \

that/super/long/path/to_a_directory

Top 10 bash_profile aliases

You are operating with limited time and limited brain power to finish your programs with enough time to go outside and breath fresh air

Knowing this, its way too tedious to type out commands like git diff --cached which can be shortcut to gdc with an alias. An alias will allow you to create shortcuts with your own command names that map to another command and options.

These are my WIP favorite aliases. Put these lines into your bash_profile and skip the riff-raff.

Number 1 – git status

alias gs="git status"

This alias is so key to my workflow that it wins the number 1 spot in this list.

It was a tough competition with so many other git focuses alias’s but nothing beats the frequency of gs

Heres a sample of my other favorite git alias’s

Number 2 – open bash_profile; reload bash_profile

alias bp="vim ~/.bash_profile"
alias bpr="source ~/.bash_profile"

When you add an alias to your ~/.bash_profile it will not work immediately in your shell because your shell only runs that config script on startup. In order, the re-config your bash, you will need to source that file. But nobody has time for that. bp into your bash profile, make changes, bpr and enjoy your new configs

 TODO: the other 8 alias’s

Pro alias tips:

See all your aliases with this command $ alias

Symbolic Links, symlinks, files that point to files

Sometimes, when working with directories and files, we want to reference the same file in a different directory.

This is most apparent when you want to upload dotfiles and config on github so you can backup and pull down these onto any computer.

Lets get started and you’ll see how my setup will help your setup.

Working with files

You can create an “original” file and create as many “links” pointing to the original as desired.

1. Create a txt file

``$ echo "test" > tmp1.txt

2. Create a symbolic link and new txt file

$ ln -s tmp1.txt tmp2.txt

You now have this output on ls -lA

Working with config files

1 – Download your config files directories.

I use a ~/.vim directory to store all my relevant vim related configs and plugins.

cd && git clone https://github.com/theptrk/.vim

2 – Symlink your ~/.vim/.vimrc to the system ~/.vimrc dsfdsaf

You system will look for ~/.vimrc when booting up your vim and doesn’t know about your fancy new config folder. If you symlink these, then your system will boot your version controlled config!

ln -s ~/.vim/.vimrc .vimrc

3 – Double check your symlink

ls -lA

This should show .vimrc ->/Users/YourName/.vim/.vimrc

Gotchas:

Links reference abstract filenames/directories and NOT physical locations on disk.

Links will not reference the original if either is moved

If you delete the original, the linked files still exist as “orphaned” links that point to nothing. However, if you then save to the linked file, the “original” will be recreated.