Shorts
Files and Folders
SHORT
COMMAND COVERED
ls - list directory contents
List files in long format with human-readable sizes
This example navigates, or changes the working directory, to
/bin and runs ls -lh to list its contents in long format. The results are piped to head to limit the output to the first 10 lines. The -l flag shows permissions, ownership, size, and modification date for each file. The -h flag formats file sizes in human-readable units like KB and MB. Together they give a clear, readable snapshot of a directory's contents. Running ls without any flags is a shorter way of listing files, but it will not show file sizes or hidden files.
Files and Folders
SHORT
COMMAND COVERED
ls - list hidden files
List all files including hidden files in long format
This example navigates to the
/bin directory and runs ls -la to list all files, including hidden ones. The -a flag reveals entries that start with a dot, which are normally omitted. The first two entries are always . and .., representing the current and parent directories, respectively. The output is piped to head to keep the results manageable.
Files and Folders
SHORT
COMMAND COVERED
cd - change to home directory
Navigate to the home directory using ~
This example uses the
cd ~ command to navigate to the current user's home directory. Running the pwd command immediately after confirms the new working directory. The tilde character ~ is a Unix shell shorthand that always expands to the path of the current user's home directory. It works regardless of where you currently are in the filesystem.
Files and Folders
SHORT
COMMAND COVERED
cd - change to previous directory
Navigate back to the previous directory using cd -
This example starts by using the
pwd command to return the working directory, which in this case is /Users/chip/code/www.unixcasts.com/lib/vhs/work. Next, the command cd /bin is issued and followed up by pwd to verify the change in directory occurred. Finally, it navigates using the syntax cd - to switch back to the previously visited directory. The - shorthand that is passed to the cd command is a handy way to toggle between two directories without retyping the full path.
Files and Folders
SHORT
COMMAND COVERED
mkdir - make directories
Create a new directory using mkdir
This example runs
mkdir projects to create a new directory called projects in the current location. Running the ls -l command afterward confirms the directory was created with its permissions and timestamp. The default permissions are determined by the shell's umask setting (run man -k umask for more details). If a directory already exists, mkdir will return an error.
Files and Folders
SHORT
COMMAND COVERED
rmdir - remove empty directories
Remove an empty directory using rmdir
This example creates a
projects directory and confirms it exists with the file listing command, ls -l. The rmdir command is then used to remove it. A second ls -l confirms the directory is gone. Unlike the command rm -r, rmdir will refuse to remove a directory that still contains files, making it a safer option.
Files and Folders
SHORT
COMMAND COVERED
touch - change file access and modification times
Create an empty file using touch
This example runs the command
touch file.txt to create a new empty file in the current directory. Running ls -lh file.txt confirms the file exists and shows its size as zero bytes (0B). If the file already existed, touch would update its access and modification timestamps instead. It's commonly used to quickly create placeholder files.
Files and Folders
SHORT
COMMAND COVERED
cat - concatenate and print files
Print file contents using cat
This example first uses the
echo command to write the word hello into the file named file.txt using standard output redirection. Standard output is commonly referred to as STDOUT or file descriptor 1. To view the file, the cat (concatenate and print files) command is used. Running cat file.txt then prints the contents of the file to the terminal. The cat command reads the file from top to bottom and outputs every line. It's one of the most direct ways to inspect the contents of a small text file.
Files and Folders
SHORT
COMMAND COVERED
less - display output one screenful at a time
Page through file contents using less
This example uses
seq 15 > nums.txt to generate the numbers 1 through 15 and saves them to the nums.txt file. Running less nums.txt opens the file in an interactive pager (the seq and less commands could not be shown here due to how the pager works). You can scroll up and down through the content and press q to quit. Unlike cat, the less command is ideal for reading longer files without flooding the terminal.
Files and Folders
SHORT
COMMAND COVERED
head - display first lines of a file
Display the first 10 lines of a file using head
This example generates 20 lines of numbers using the
seq command and saves them to the nums.txt file. Running head nums.txt prints to the terminal only the first 10 lines of a file by default. Future examples will demonstrate how the -n count option allows the number of lines shown by the head command to be customizable.
Files and Folders
SHORT
COMMAND COVERED
tail - display the last part of a file
Display the last 5 lines of a file using tail
This example generates 20 lines of numbers using the
seq command and saves them to the file named nums.txt. Running tail -n 5 nums.txt prints only the last 5 lines of the file. Similar to the head command, the -n flag for tail controls how many lines are shown. The default for using tail without the -n option is the last 10 lines of the file. The tail command is especially useful for checking the end of log files where the most recent entries appear. See man tail for various options, more speficially the -f option.
Files and Folders
SHORT
COMMAND COVERED
cp - copy files
Copy a file using cp
This example creates an empty file called
file.txt using the touch command. The cp command is then used to copy file.txt to a new file called copy.txt. Running the ls command confirms that both files now exist in the directory. The original file is left untouched, while the copy is an independent duplicate.
Files and Folders
SHORT
COMMAND COVERED
mv - move files
Rename a file using mv
This example creates an empty file called
file.txt using the touch command. The mv command (also known as the move files command) is then used to rename file.txt to renamed.txt. Running ls -l confirms the original name is gone and the new name is present. When the source and destination are in the same directory, the mv command performs a rename rather than a move.
Files and Folders
SHORT
COMMAND COVERED
rm - remove directory entries
Delete a file using rm
This example creates a file named
file.txt using the touch command and confirms it exists using ls to list the current files present in the directory. The rm command is then used to delete the file permanently. A second listing of files with ls shows the file is no longer present. Unlike moving a file to the trash, the rm command removes it immediately with no built-in way to recover it.
Files and Folders
SHORT
COMMAND COVERED
grep - file pattern searcher
Search file contents for a pattern using grep
This example uses the
printf command to write three fruit names to the fruits.txt file, with each fruit listed on its own line. Running the command grep an fruits.txt searches the file and prints only lines containing the pattern an. The result returns banana since it's the only match. Using the grep command is one of the most frequently used tools for filtering text output. There are many similar text filtering tools, such as egrep, fgrep, and others. For more details, try man -k grep to review alternatives.
Files and Folders
SHORT
COMMAND COVERED
find - walk a file hierarchy
Find files by name pattern using find
This example creates three files: two files with a .rb file suffix and one file with a .txt suffix. Running
find . -name '*.rb' searches the current directory recursively for files ending in only .rb. Only foo.rb and bar.rb are returned, since baz.txt doesn't match the pattern. The -name flag supports shell glob patterns for flexible matching. There are many other flags the grep command supports for things like file types, sizes, last modification time, and many other examples. See man find for more details.
Files and Folders
SHORT
COMMAND COVERED
which - locate a program file in the user's path
Locate the cat executable using which
This example runs the command
which cat to find the full path of the cat command executable file. The shell searches each directory listed in the current user's $PATH, in order, and prints the first match it finds. The result shows exactly which version of cat will run when you type the command in the terminal. This is useful for diagnosing issues when multiple versions of a tool are installed.