Working with files and directories

Output file and directory information

Output filename portion of file path

basename <path/file>

Output directory portion of file path

dirname <path/file>

Open and edit files

Open a file

open <file>

Open the current directory in finder

open .

Open a file with the specified application (Mac OSX)

open -a /Applications/<app> <file>

Open file in the nano text editor

nano <file>

Show line endings for files (Unix: ASCII, DOS: ASCII + CRLF)

file <file>

File Globbing

Using * for path value will not match hidden files and folders beginning with a .

List all non-hidden files and folders

Remove all non-hidden files from inside <folder>

Work with remote mounts and external hard drives

Access remote mounts (such as Samba shares and external hard drives)

Copy, move and delete files

Copy a file (recursive copy / preserve symlinks)

Move or rename a file

Remove a file

Remove files and folders recursively without confirmation

Symbolic link from source file to destination file

Rename files

Requires rename via brew install rename

Rename files (-n or --dry-run to test rename output with making changes)

Replace search string with replace string for matching files

Rename all txt files with a sequential number in the format text-1.txt

Uppercase first letter of zip file name (-f to force rename on macOS when changing case)

Create and delete directories

Create directories

Create multiple directories and subdirectories in one step

Alternative syntax for multiple directories using array of items via {}

Multiple directory syntax can be nested

Remove a directory

Examples of advanced file actions

Create a sequence of files from a sigle copy

Create, read and output files

Output the contents of the file

Join files together

Create or overwrite a file and add content via the command line (end with CTRL + d on a fresh line)

Append text via the command line to the file (end with CTRL + s)

Overwrite the contents of the file with text

Append the contents of file with the text

Create an empty file

Set modified date to current time

Set modified and accessed date to specific time (Time format: YYYYMMDDHHMM)

Replicate modified date of source file and copy to target file

Set creation date of file (SetFile requires xcode command line tools)

File Status

Output status information for file (use -f for selective metadata and output format)

Output file size followed by the file name

Tail and Head

Display the first or last part of a file

Less and More

Paginate through a text file output directly in the terminal

Paginate through a text files output directly in the terminal (+G to jump to end of file on open)

Less commands

Go to end of file

Go to end of file

Back one page

Forward one page

Last updated