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.


* 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


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


-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